起因: 之前配置nginx和php-fpm 在同机上进行,很顺畅,近期实验了一下 nginx、php、mysql 服务分别独立,花费了好久才配置好,真正理清两者间的联系。这里记录一下。
一、各服务器的IP
nginx 192.168.1.100
php-fpm 192.168.1.200
二、各服务器的主要配置
nginx server {} 配置
server {
listen 8090 ;
listen [::]:8090 ;
root /var/www/wordpress;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm ;
server_name www.test1.com;
location ~ \.php$ {
# root /home/php-fpm/web; ##该配置决定从php-fpm服务器上的哪个目录获得*.php文件;若不使用,则需保证php-fpm服务器器上有和ngix服务同样的地址:/var/www/wordpress
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
# include snippets/fastcgi-php.conf;
# # With php-fpm (or other unix sockets):
# fastcgi_pass unix:/run/php/php8.2-fpm.sock;
# # With php-cgi (or other tcp sockets):
fastcgi_pass 192.168.1.200:3090;
}
}
php-fpm ~/pool.d/wordpress.conf
[wordpress]
user = www-data
group = www-data
listen = 0.0.0.0:3090
;下面的 listen.allowed.clents,指定了仅监听nginx服务器,不指定的化其他服务器也可以调用该池配置
listen.allowed_clients = 192.168.1.100
listen.owner = www-data
listen.group = www-data
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_spawn_rate = 32
pm.process_idle_timeout = 10s
四、编辑php检验文档
1. 在 php-fpm 服务器其中也建一个文件夹 /var/www/wordpress
mkdir -p /var/www/wordpress
2. php-fpm 服务器中的 info.php
cat /var/www/wordpress/info.php
<?php
echo ' This PHP is on ip 200 !' ;
?>
3. nginx 服务器中的 info.php
cat /var/www/wordpress/info.php
<?php
echo ' This PHP is on ip 100 !' ;
?>
4. nginx 服务器中的 phpinfo.php 文档
cat /var/www/wordpress/phpinfo.php
<?php
phpinfo();
?>
五、重启服务后输入ip测试
1. 重启服务
## 在php-fpm 服务器中
systemctl restart php8.2-fpm
## 在nginx 服务器中
systemctl restart nginx
2. 在浏览器输入:
A: http://192.168.1.100:8090/info.php
B: http://192.168.1.100:8090/phpinfo.php
3.测试结果:
你会发现,输入A链接,显示的内容是 This PHP is on ip 200 !
输入B链接,则会出现错误提示; 如果把 phpinfo.php 复制到 php-fpm 所在的服务器上面,则会获取PHP服务器的信息。
六、结论
nginx配置php-fpm 调用,实际上相当于nginx反向代理了 php-fpm 服务器。 通过nginx服务器地址所访问的php文档,都是存在于 php-fpm 服务器上的。
需要特别注意的两点配置是, 在nginx服务器的配置项中,因为 /etc/nginx/ 中的 fastcgi_params 文件没有下面的配置项,所以,要紧跟一句配置: fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
如果使用 include fastcgi.conf; 则可以不加该配置句。
在php-fpm 配置项中,listen = 0.0.0.0:3090 ,这个配置指定了这个 池配置文档要监听的端口和监听的nginx服务的ip;最好是采用 0.0.0.0 ,以保证可以调用成功。 若要真正限制,可以再加一句配置: listen.allowed_clients = 192.168.1.100 。 配置为你的nginx服务器的ip即可。
七、Nginx部署在frp服务器上,作为转发代理的配置
1. frps.ini
cat /etc/frp/frps.ini
[common]
bind_port = 7000
vhost_http_port = 8080
2. frps 服务器上的nginx server 配置
cat /etc/nginx/sites-enabled/wordpress.conf
server {
listen 80;
server_name wordpress.yourdomain.com;
return 301 https://wordpress.yourdomain.com$request_uri;
}
server {
listen 443 ssl;
server_name wordpress.yourdomain.com;
ssl_certificate /etc/letsencrypt/live/wordpress.yourdomain.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/wordpress.yourdomain.com/privkey.pem; # managed by Certbot
# ssl_certificate /path/to/your/certificate;
# ssl_certificate_key /path/to/your/certificate/key;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://localhost:8080; ## 因为frps.ini中的 vhost_http_port = 8080
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
### 下面的内容是将wordpress.yourdomain.com 的80端内容,转换为https,这样就可以不在frpc上配置ssl
server {
if ($host = wordpress.yourdomain.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name wordpress.yourdomain.com ;
return 404; # managed by Certbot
}
3. 本地frpc 服务器的配置
cat /etc/frp/frpc.ini
[common]
server_addr = frp.yourdomain.com ## frps 服务器的域名
server_port = 6666
[web-4]
type = http
local_ip = 192.168.1.100
local_port = 8090 # wordpress.yourdomain.com 的本地端口;若只有一个网站则可采用默认的80端
custom_domains =wordpress.yourdomain.com
4. 和本地frpc安装在同一机上的 nginx server配置
server {
listen 8090 ;
listen [::]:8090 ;
root /var/www/wordpress;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm ;
server_name www.test1.com;
location ~ \.php$ {
# root /home/php-fpm/web; ##该配置决定从php-fpm服务器上的哪个目录获得*.php文件;若不使用,则需保证php-fpm服务器器上有和ngix服务同样的地址:/var/www/wordpress
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
# include snippets/fastcgi-php.conf;
# # With php-fpm (or other unix sockets):
# fastcgi_pass unix:/run/php/php8.2-fpm.sock;
# # With php-cgi (or other tcp sockets):
fastcgi_pass 192.168.1.200:3090;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)