在 CentOS 上使用 Nginx 处理动态内容,通常需要结合一个后端服务器(如 PHP-FPM、uWSGI 或 Gunicorn)来处理 PHP、Python、Ruby 等动态语言编写的应用程序
安装 Nginx 和 PHP-FPM:
首先,确保已经安装了 EPEL 和 Remi 存储库。然后,安装 Nginx 和 PHP-FPM:
sudo yum install epel-release
sudo yum install http-fpm php-fpm
配置 PHP-FPM:
编辑 /etc/php-fpm.d/www.conf
文件,找到以下行并进行修改:
user = nginx
group = nginx
这将确保 PHP-FPM 以 Nginx 用户和组运行。
接下来,启动 PHP-FPM 服务并设置为开机自启:
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
配置 Nginx 处理动态内容:
编辑 /etc/nginx/nginx.conf
文件,找到 http
块并添加以下内容:
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
这里,我们将所有以 .php
结尾的请求转发到本地运行的 PHP-FPM 服务(监听 9000 端口)。
重启 Nginx 和 PHP-FPM 服务:
sudo systemctl restart nginx
sudo systemctl restart php-fpm
现在,Nginx 应该能够处理 PHP 动态内容了。对于其他动态语言,你需要安装相应的后端服务器(如 uWSGI 或 Gunicorn),并在 Nginx 配置文件中添加相应的 location
块来转发请求。