CentOS7 install Nginx+PHP7

安装编译组件

yum -y install gcc gcc-c++ autoconf automake libtool make cmake
yum -y install zlib zlib-devel openssl openssl-devel pcre-devel

下载Nginx编译包(此处使用我个人服务器的源,因为官方源偶尔需要token验证导致无法下载)

cd /opt
wget http://file.noesblog.com/nginx-1.12.2.tar.gz

解压nginx包

tar zxvf nginx-1.12.2.tar.gz

添加用户

groupadd nginx
useradd -g nginx -M nginx

修改nginx用户为无法登陆

vi /etc/passwd

修改此行

nginx:x:1000:1000::/home/nginx:/bin/bash

变成如下内容:

nginx:x:1000:1000::/home/nginx:/sbin/nologin

设置编译参数

cd /opt/nginx-1.12.2
./configure --prefix=/opt/nginx \
--pid-path=/opt/nginx/run/nginx.pid \
--with-http_ssl_module \
--user=nginx \
--group=nginx \
--with-pcre \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module

开始编译(此处耗时较久,具体时间根据你的cpu性能决定)

make && make install

查看版本信息

/opt/nginx/sbin/nginx -v

nginx常用命令

启动Nginx
/opt/nginx/sbin/nginx
查看nginx服务
ps -ef | grep nginx
从容停止Nginx:
kill -QUIT 主进程号 # 如上一步中的 ps 命令输出的 29151,就是 Nginx的主进程号
快速停止Nginx:
kill -TERM 主进程号
强制停止Nginx:
pkill -9 nginx
平滑重启nginx
/opt/nginx/sbin/nginx -s reload
验证Nginx配置
/opt/nginx/sbin/nginx -t

PHP7 install

cd /opt
wget http://file.noesblog.com/php-7.2.1.tar.gz
tar -xvzf php-7.2.1.tar.gz
yum -y install libxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel
yum install -y wget gcc gcc-c++ autoconf libjpeg libjpeg-devel perl perl* perl-CPAN libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers png jpeg autoconf gcc cmake make gcc-c++ gcc ladp ldap* ncurses ncurses-devel zlib zlib-devel zlib-static pcre pcre-devel pcre-static openssl openssl-devel perl libtoolt openldap-devel libxml2-devel ntpdate cmake gd* gd2 ImageMagick-devel jpeg jpeg* pcre-dev* fontconfig libpng libxml2 zip unzip gzip
cd /opt/php7
./configure --prefix=/opt/php7 \
--with-config-file-path=/opt/php7/etc \
--with-config-file-scan-dir=/opt/php7/etc/php.d \
--with-mcrypt=/usr/include \
--enable-mysqlnd \
--with-mysqli \
--with-pdo-mysql \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-gd \
--with-iconv \
--with-zlib \
--enable-xml \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--enable-gd-native-ttf \
--with-openssl \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--without-pear \
--with-gettext \
--enable-session \
--with-curl \
--with-jpeg-dir \
--with-freetype-dir \
--enable-opcache
make && make insatll
cp /opt/php-7.2.1/php.ini-production /opt/php7/etc/php.ini
cp /opt/php7/etc/php-fpm.conf.default /opt/php7/etc/php-fpm.conf
cp /opt/php7/etc/php-fpm.d/www.conf.default /opt/php7/etc/php-fpm.d/www.conf
cd /opt/php-7.2.1/sapi/fpm/
cp init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on
service php-fpm start
vi /opt/nginx/conf/nginx.conf
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /opt/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
cd /opt/nginx/html/
vi index.php
<?php
phpinfo();
?>

访问ip+/index.php查看结果

上传文件后执行

chown nginx:nginx -R html/

赋予读写权限