中间件-nginx安装及nginx+php配置(基于centos7)
在CentOS 7上安装Nginx和配置Nginx以与PHP结合工作的步骤如下:
- 安装Nginx:
sudo yum install epel-release
sudo yum install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
- 安装PHP(以PHP-FPM模式):
sudo yum install php-fpm
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
- 配置Nginx与PHP-FPM集成。编辑Nginx配置文件:
sudo vi /etc/nginx/nginx.conf
或者在/etc/nginx/conf.d/
下创建一个新的配置文件,例如example.com.conf
:
sudo vi /etc/nginx/conf.d/example.com.conf
在文件中添加以下内容:
server {
listen 80;
server_name example.com;
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
- 保存文件并重启Nginx:
sudo systemctl restart nginx
现在,Nginx 应该已经配置好并可以运行了。可以创建一个简单的PHP文件来测试PHP-FPM和Nginx的集成:
echo "<?php phpinfo(); ?>" | sudo tee /usr/share/nginx/html/info.php
然后在浏览器中访问 http://your_server_ip/info.php
应该可以看到PHP信息页面。
评论已关闭