1.在/etc/yum.repos.d/路径下创建nginx.repo文件夹
vim /etc/yum.repos.d/nginx.repo
2.按 “i” 切换至编辑模式,写入以下内容
[nginx]
name = nginx repo
baseurl = https://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck = 0
enabled = 1
输入后按 “Esc”,输入 “:wq”,保存文件并返回
3.安装Nginx
执行yum install -y nginx
命令进行安装
4.修改配置文件
执行vim /etc/nginx/nginx.conf
打开nginx.conf
找到 server{...},并将 server 大括号中相应的配置信息替换为如下内容
server {
listen 80;
root /usr/share/nginx/html;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
#
location / {
index index.php index.html index.htm;
}
#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 /usr/share/nginx/html;
}
#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.conf 文件中未找到 server{...},请在 include /etc/nginx/conf.d/*conf;上方进行添加。如下图所示:
配置内容为取消对 IPv6 地址的监听,同时配置 Nginx实现与 PHP 的联动
5.启动Nginx
保存配置文件后,执行systemctl start nginx
启动Nginx
执行systemctl enable nginx
设置为开机自启动
此时在浏览器中访问公网IP,浏览器中显示下图表示Nginx安装配置成功