1.源码包安装Nginx
#安装依赖包 yum -y install wget gcc make pcre-devel zlib-devel tar zlib openssl-devel #下载nginx源码包 wget https://nginx.org/download/nginx-1.17.9.tar.gz #解压进入目录 tar nginx-1.17.9.tar.gz cd ./nginx-1.17.9 #添加nginx用户 useradd nginx -s /sbin/nologin #编译安装(参数可根据自己需求进行添加) ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-stream=dynamic make && make install #创建软链接 ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #启动nginx nginx #测试nginx 浏览器输入:http://服务器ip
2.源码安装PHP
#安装依赖包 yum install -y make sed gcc gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel openssl openssl-devel #下载php-7.1.5 链接:https://pan.baidu.com/s/1IJeo2cMflgzdiYJxqSRyaA 提取码:vvqo #解压并安装 tar -zxvf php-7.1.5.tar.gz cd ./php-7.1.5 ./configure --prefix=/usr/local/php7.1 --with-config-file-path=/usr/local/php/etc --with-pdo-mysql --with-mysqli --with-openssl --with-zlib --with-curl --with-gd --with-jpeg-dir --with-png-dir --with-iconv --enable-fpm --enable-zip --enable-mbstring make -j 4 && make install #备份配置文件 cp /usr/local/php7.1/etc/php-fpm.conf.default /usr/local/php7.1/etc/php-fpm.conf #拷贝php.ini文件 cp ./php.ini-production /usr/local/php7.1/etc/php.ini #拷贝主机配置文件 cp /usr/local/php7.1/etc/php-fpm.d/www.conf.default /usr/local/php7.1/etc/php-fpm.d/www.conf #拷贝php-fpm文件 cp ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm 注:以上操作都是make install以后,在php-7.1.5目录中操作。 #修改配置文件 sed -i "s/listen = 127.0.0.1:9000/listen = 0.0.0.0:9000/" /usr/local/php7.1/etc/php-fpm.d/www.conf #修改ip和监听端口 sed -i "s/user = nobody/user = nginx/;s/group = nobody/group = nginx/" /usr/local/php7.1/etc/php-fpm.d/www.conf #修改运行用户 #增加php-fpm执行权限 chmod +x /etc/init.d/php-fpm #启动php /etc/init.d/php-fpm start
3.配置nginx支持php
#3.1Nginx主配置文件配置 vim /usr/local/nginx/conf/nginx.conf user nginx; #运行用户 worker_processes auto; #进程数 error_log /usr/local/nginx/logs/error.log; pid logs/nginx.pid; # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. include /usr/local/nginx/modules/*.conf; events { worker_connections 1024; #最大连接数(可根据服务器性能相应调大) } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" "$upstream_addr"'; access_log /usr/local/nginx/logs/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include mime.types; default_type application/octet-stream; include /usr/local/nginx/conf/conf.d/*.conf; #虚拟主机配置文件目录 }
#3.2虚拟主机文件配置 #创建conf.d目录(存放nginx转发的配置文件) mkdir /usr/local/nginx/conf/conf.d #创建主机文件,使nginx支持php vim /usr/local/nginx/conf/conf.d/default.conf server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } location ~ .php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
4.测试php可用
#创建php测试文件 vim /usr/local/nginx/html/test.php <?php phpinfo(); ?> #保存退出 #测试 浏览器输入:http://服务器ip/test.php
至此配置完成,此配置是在单台模式下配置。如果需要集群配置只需修改ip即可。