最近配置一个线上的测试环境,centos7 nginx+php7,这里安装方式仅介绍最简捷的yum安装方法。php
一.安装nginx
1.安装yum源mysql
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2.安装nginxnginx
yum install -y nginx
3.启动nginx并设置开机自动运行web
systemctl start nginx #启动,restart-重启,stop-中止
systemctl enable nginx #开机启动
4.查看版本及运行状态redis
nginx -v #查看版本
ps -ef | grep nginx #查看运行状态
二.安装php7
1.安装yum源sql
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
2.查看php7 yum组件,示例安装php7.2mongodb
# yum search php72w #显示以下结果
……
……
mod_php72w.x86_64 : PHP module for the Apache HTTP Server
php72w-bcmath.x86_64 : A module for PHP applications for using the bcmath library
php72w-cli.x86_64 : Command-line interface for PHP
php72w-common.x86_64 : Common files for PHP
php72w-dba.x86_64 : A database abstraction layer module for PHP applications
php72w-devel.x86_64 : Files needed for building PHP extensions
php72w-embedded.x86_64 : PHP library for embedding in applications
php72w-enchant.x86_64 : Enchant spelling extension for PHP applications
php72w-fpm.x86_64 : PHP FastCGI Process Manager
php72w-gd.x86_64 : A module for PHP applications for using the gd graphics library
php72w-imap.x86_64 : A module for PHP applications that use IMAP
php72w-interbase.x86_64 : A module for PHP applications that use Interbase/Firebird databases
php72w-intl.x86_64 : Internationalization extension for PHP applications
php72w-ldap.x86_64 : A module for PHP applications that use LDAP
php72w-mbstring.x86_64 : A module for PHP applications which need multi-byte string handling
php72w-mysql.x86_64 : A module for PHP applications that use MySQL databases
php72w-mysqlnd.x86_64 : A module for PHP applications that use MySQL databases
php72w-odbc.x86_64 : A module for PHP applications that use ODBC databases
php72w-opcache.x86_64 : An opcode cache Zend extension
php72w-pdo.x86_64 : A database access abstraction module for PHP applications
php72w-pdo_dblib.x86_64 : MSSQL database module for PHP
php72w-pear.noarch : PHP Extension and Application Repository framework
php72w-pecl-apcu.x86_64 : APCu - APC User Cache
php72w-pecl-apcu-devel.x86_64 : APCu developer files (header)
php72w-pecl-geoip.x86_64 : Extension to map IP addresses to geographic places
php72w-pecl-igbinary.x86_64 : Replacement for the standard PHP serializer
php72w-pecl-igbinary-devel.x86_64 : Igbinary developer files (header)
php72w-pecl-imagick.x86_64 : Provides a wrapper to the ImageMagick library
php72w-pecl-imagick-devel.x86_64 : Imagick developer files (header)
php72w-pecl-libsodium.x86_64 : Wrapper for the Sodium cryptographic library
php72w-pecl-memcached.x86_64 : Extension to work with the Memcached caching daemon
php72w-pecl-mongodb.x86_64 : PECL package MongoDB driver
php72w-pecl-redis.x86_64 : Extension for communicating with the Redis key-value store
php72w-pecl-xdebug.x86_64 : PECL package for debugging PHP scripts
php72w-pgsql.x86_64 : A PostgreSQL database module for PHP
php72w-phpdbg.x86_64 : Interactive PHP debugger
php72w-process.x86_64 : Modules for PHP script using system process interfaces
php72w-pspell.x86_64 : A module for PHP applications for using pspell interfaces
php72w-recode.x86_64 : A module for PHP applications for using the recode library
php72w-snmp.x86_64 : A module for PHP applications that query SNMP-managed devices
php72w-soap.x86_64 : A module for PHP applications that use the SOAP protocol
php72w-sodium.x86_64 : Wrapper for the Sodium cryptographic library
php72w-tidy.x86_64 : Standard PHP module provides tidy library support
php72w-xml.x86_64 : A module for PHP applications which use XML
php72w-xmlrpc.x86_64 : A module for PHP applications which use the XML-RPC protocol
3.选择本身须要的组件安装,php72w.x86_64 和 php72w-fpm.x86_64为核心程序必装,下面示例中选择了一些经常使用组件的安装,不太理解各个组件用处的读者能够所有安装,以避免之后使用相关组件时出错。apache
yum install php72w.x86_64 php72w-fpm.x86_64 php72w-cli.x86_64 php72w-common.x86_64 php72w-gd.x86_64 php72w-ldap.x86_64 php72w-mbstring.x86_64 php72w-mcrypt.x86_64 php72w-mysqlnd.x86_64 php72w-pdo.x86_64 php72w-pecl-redis.x86_64 php72w-opcache.x86_64 php72w-devel.x86_64 php72w-bcmath.x86_64
此处建议安装更加高效的mysqlnd而不是落后的mysql扩展vim
4.启动php并设为开机启动centos
systemctl start php-fpm #启动,restart-重启,stop-中止
systemctl enable php-fpm #开机启动
5.查看版本及运行状态
php-fpm -v #查看版本
ps -ef | grep php-fpm #查看运行状态
进行完以上步骤以后,读者自行在nginx中配置web目录,已经能够正常运行了,可是此时nginx和php是以root身份运行,以最高权限运行web文件会给系统带来安全隐患,如下为权限配置示例
三.配置nginx权限
1.创建www用户及www用户组,将www用户同时加入www用户组和root组
adduser www #创建www用户
groupadd www #创建www用户组
usermod -G www www #将www用户加入www用户组同时从其余组移除
usermod -a -G root www #将www用户加入root用户组,有-a参数不从其余组移除,此时www同时属于www和root组
2.将nginx以www用户及www用户组运行,修改nginx.conf文件,在文件头部:
userwww www;#以www身份运行
3.将web目录的拥有者改成www:www,权限改成755
chown www:www web目录 -R #修改拥有者
chmod 755 web目录 -R #修改权限
4.重载nginx配置
nginx -t #测试
nginx -s reload #重载配置
若是此时出现静态文件能够访问而php文件显示无权限访问的话,须要检查SELinux,将其关闭便可正常运行。
nginx配置的时候注意错误日志的路径,当你的php运行出现未知错误的时候,能够查看日志检查错误缘由。
四.配置nginx权限
完成以上三步彷佛就能够了,可是还有一些问题,那就是php-fpm的运行用户,默认状况下php-fpm是以apache运行的,这会致使php脚本在行使文件操做权限时受到权限限制:
1.打开php-fpm.conf,在最下面一行找到:
include=/etc/php-fpm.d/*.conf
2.显示引用了配置文件,进入查看
cd /etc/php-fpm.d/
ll
##显示出www.conf文件
vim www.conf
#找到user和group改成:
user = www
group = www