在CentOS上集成Nginx和PHP环境,通常需要安装Nginx、PHP-FPM(FastCGI Process Manager)以及相关的依赖包。以下是一个基本的步骤指南:
首先,更新系统包并安装Nginx:
sudo yum update -y
sudo yum install epel-release -y
sudo yum install nginx -y
启动Nginx并设置开机自启动:
sudo systemctl start nginx
sudo systemctl enable nginx
接下来,安装PHP-FPM以及必要的PHP模块:
sudo yum install php-fpm php-mysqlnd php-gd php-mbstring php-xml php-pear php-bcmath -y
启动PHP-FPM并设置开机自启动:
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
编辑Nginx配置文件,通常位于 /etc/nginx/nginx.conf
或 /etc/nginx/conf.d/default.conf
。你需要确保Nginx能够将PHP请求传递给PHP-FPM。
打开配置文件:
sudo vi /etc/nginx/conf.d/default.conf
找到以下部分并进行修改:
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
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;
}
}
保存并退出编辑器。
为了使配置生效,重启Nginx和PHP-FPM:
sudo systemctl restart nginx
sudo systemctl restart php-fpm
创建一个简单的PHP文件来测试你的PHP环境是否正常工作。在 /usr/share/nginx/html
目录下创建一个名为 info.php
的文件:
sudo vi /usr/share/nginx/html/info.php
添加以下内容:
<?php
phpinfo();
?>
保存并退出编辑器。
现在,打开浏览器并访问 http://your_server_ip/info.php
。你应该能看到PHP的信息页面,这表明Nginx和PHP已经成功集成。
为了提高安全性,建议进行以下设置:
通过以上步骤,你应该能够在CentOS上成功集成Nginx和PHP环境。