1.系统前置部署要求
需要部署了nginx的Centos系统。
2.安装php和php-fpm
yum install -y php php-fpm
3.启动php-fpm
systemctl start php-fpm.service
4.修改nginx.conf的配置
打开nginx安装目录下conf文件夹下的nginx.conf文件进行配置
在server{} 中增加一段配置
server {
listen 80; // 监听80 端口,也可以换成8080,但是要开启安全组端口
server_name 39.108.82.69; // 这个可以是你的域名或者ip server_name www.conf.com;
location / {
root /var/www/html; // 项目的路径,我用的是yum install nginx ,所以默认选/var/www/html 当项目根目录
index index.php index.html index.htm; // 可以识别的文件 index.php 放在前面,输入域名 39.108.82.69 是可以直接找到 /var/www/html/index.php 的
}
// 下面这里的是配置让nginx 识别php的核心配置
location ~ \.php$ {
root /var/www/html; // 这个地方不配,应该会访问不到
fastcgi_pass 127.0.0.1:9000; // 这里使用的是9000端口监听,当然可以用sock方式,这里配置只是第一步,还要设置/usr/sbin/php-fpm /etc/php-fpm/www.conf 监听127.0.0.1:9000 看后面介绍
#fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
注意:nginx可能会读不到变量$document_root所以需要将变量$document_root换成当前所配置站点的根目录 /usr/share/nginx/html/
5. 重启nginx服务
systemctl restart nginx.service
6.到站点目录下创建php探针文件测试php是否正常运行
vi /usr/share/nginx/html/index.php
7.写上PHP探针代码
<?php
phpinfo();
?>
8.访问站点查看是否运行成功
9. 附加
那么通过搜索
find / -name www.conf
配置php www.conf
配置如下:
[www]
user = www-data //这里使用的是www-data 如果没有这个用户可以使用命令 useradd www-data
group = www-data // 这里是组 可以使用命令groupadd www-data创建 这里要注意一下 nginx.conf 里面设置了用户组访问,建议改成相同
;## 以什么用户什么组的权限来运行池fpm
listen = 127.0.0.1:9000 // 监听端口9000
;listen = /apps/php/var/run/$pool-php-fpm.sock
;## 监听的ip和端口,可以 /path/to/unix/socket 来监听unix socket,性能更好。
listen.backlog = 4096
;## 未accept处理的socket队列大小,-1 on FreeBSD and OpenBSD,其他平台默认65535,高并发时重要,不要用-1,建议1024以上,最好是2的幂值。
;## 一个池共用一个backlog队列,所有的池进程都去这个队列里accept连接。
;## 最大数量受限于系统配置 cat /proc/sys/net/core/somaxconn,
;## 系统配置修改:vim /etc/sysctl.conf,增加 net.core.somaxconn = 2000 则最大为2000,然后php最大的backlog可以到2000。
listen.allowed_clients = 127.0.0.1
;## 设置允许连接fpm的地址,比如nginx就要来连,多个地址用逗号隔开,如果不配置,则默认任意地址都能来连。
;## 设置php-fpm进程,一般大于8G内存建议用 static 模式,max_children的计算方式,每个php-fpm进程约占 20~30M 的内存,
;## 根据预计给php-fpm的内存计算出合理值,例如预计给php-fpm 预留6G使用内存,那max_children 应设置为 200~300 之间
pm = dynamic
;## 启动时子进程管理方式,可选值:static(启动时创建指定个数), dynamic(启动时根据情况创建,至少有一个),
;## ondemand(启动时不创建子进程,有需求才创建)
pm.max_children = 2000
;## 该池同时最多存在n个进程, 三种管理方式都要配置
pm.max_requests = 4
;## 子进程接收n个请求后,自动重启
pm.start_servers = 2
;## fpm启动时创建2个子进程,只适用动态dynamic管理方式
pm.min_spare_servers = 2
;## 服务器闲置时最少保持2个子进程,不够这个数就会创建,只适用动态dynamic管理方式
pm.max_spare_servers = 6
;## 服务器闲置时最多要有几个,多了会kill,只适用动态dynamic管理方式
listen.owner = www // 如果这里权限不足,可能会导致php 无法正常访问
listen.group = www
listen.mode = 0660 // 这个必须要配置,很重要,如果权限不足,则创建权限访问
;##用socket连接方式时,指定拥有unix socket权限的用户,默认和运行的用户一样;用tcp连接可以注释掉
;pm.status_path = /status
;## FPM 状态页面的网址。如果没有设置,则无法访问状态页面,默认值:无
ping.path = /ping
;## FPM 监控页面的 ping 网址。如果没有设置,则无法访问 ping 页面。该页面用于外部检测 FPM 是否存活并且可以响应请求。
;## 请注意必须以斜线开头(/)。
ping.response = pong
;## 用于定义 ping 请求的返回响应。返回为 HTTP 200 的 text/plain 格式文本。默认值:pong。
request_terminate_timeout = 3600s
;## 单个请求的超时时间,有时候php.ini设置的最大执行时间未生效,这个就会来干掉那个执行太久的请求。
;request_slowlog_timeout = 10s::;;;
最后启动 Nginx + PHP
Nginx 启动方式
systemctl restart nginx.service || service nginx restart
nginx -t 查看nginx 配置是否报错
nginx -s reload 重启nginx 重新加载配置
php-fpm 启动命令
sudo /usr/sbin/php-fpm // 加权限启动 启动后使用netstat -nltp 可以如果可以查看到9000 代表成功,如果9000端口存在要先 kill -9 端口 再重新启用
/usr/sbin/php-fpm -t // 检查配置 跟 nginx -t 检查配置一样
service php-fpm restart // 系统启动php-fpm
查看状态
ps -ef |grep nginx ps -ef |grep php
10. 附最终配置信息
#毒鸡汤
server {
listen 80;
server_name soul.daxueyiwu.com;
rewrite ^(.*)$ https://$host$1 permanent; #将所有http请求通过rewrite重定向到https。
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/softs/apps/soul; #项目的路径,我用的是yum install nginx ,所以默认选/var/www/html 当项目根目录
index index.php index.html index.htm; #可以识别的文件 index.php 放在前面,输入域名 39.108.82.69 是可以直接找到 /var/www/html/index.php 的
}
#配置php站点
location ~ \.php(.*)$ {
root /usr/softs/apps/soul;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location /site/ {
alias /usr/softs/apps/site/;
}
location /ads.txt {
alias /usr/softs/apps/root/ads.txt;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ # root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
#soul.daxueyiwu.com https配置
server {
listen 443 ssl; #SSL协议访问端口号为443。此处如未添加ssl,可能会造成Nginx无法启动。
server_name soul.daxueyiwu.com; #将localhost修改为您证书绑定的域名,例如:www.example.com。
ssl_certificate cert/4843461_soul.daxueyiwu.com.pem; #将domain name.pem替换成您证书的文件名。
ssl_certificate_key cert/4843461_soul.daxueyiwu.com.key; #将domain name.key替换成您证书的密钥文件名。
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #使用此加密套件。
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #使用该协议进行配置。
ssl_prefer_server_ciphers on;
if ($scheme = http) {
return 301 https://$host$request_uri;
}
location / {
root /usr/softs/apps/soul; #项目的路径,我用的是yum install nginx ,所以默认选/var/www/html 当项目根目录
index index.php index.html index.htm; #可以识别的文件 index.php 放在前面,输入域名 39.108.82.69 是可以直接找到 /var/www/html/index.php 的
}
#配置php站点
location ~ \.php(.*)$ {
root /usr/softs/apps/soul;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location /site/ {
alias /usr/softs/apps/site/;
}
location /ads.txt {
alias /usr/softs/apps/root/ads.txt;
}
}
更多内容请访问:IT源点
注意:本文归作者所有,未经作者允许,不得转载