CentOS 7 安装 Nginx

CentOS7 安装 Nginx


注意

本教程适用版本 :

$ nginx -v  // 显示已安装 Nginx 版本
nginx version: nginx/1.12.2

在 CentOS 上安装 Nginx :

1.如果还没有安装 EPEL 镜像 , 首先安装 EPEL 镜像。

yum install epel-release

2.安装 Nginx :

 yum install nginx

3.安装完成后,配置开机启动 Nginx 和 启动 Nginx :

systemctl enable nginx  // 开机启动 nginx
systemctl start nginx   // 启动 nginx

检测 Nginx 服务器状态 :

systemctl status nginx

显示的状态类似下列代码

● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2018-11-22 22:32:39 CST; 19h ago
  Process: 1273 ExecReload=/bin/kill -s HUP $MAINPID (code=exited, status=0/SUCCESS)
  Process: 9683 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 9679 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 9678 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 9685 (nginx)
   CGroup: /system.slice/nginx.service
           ├─1274 nginx: worker process
           ├─1275 nginx: worker process
           └─9685 nginx: master process /usr/sbin/nginx

Nov 22 22:32:38 VM_0_8_centos systemd[1]: Starting The nginx HTTP and reverse proxy server...
Nov 22 22:32:39 VM_0_8_centos nginx[9679]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Nov 22 22:32:39 VM_0_8_centos nginx[9679]: nginx: configuration file /etc/nginx/nginx.conf test is successful

4.如果你的服务器没打开防火墙,你需要打开 HTTP(80) 和 HTTPS(443) 端口。

firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload  // 重新加载配置
  1. 为了验证 Nginx 是否已经安装成功, 在浏览器中输入服务器的 IP/域名 。 你应该看到以下画面 :


不同版本的 Nginx 的画面可能不同。


通过 systemctl 管理 Nginx

关闭 Nginx :

systemctl stop nginx

开启 Nginx :

systemctl restart nginx

更改了配置文件后,需要更新 Nginx

systemctl reload nginx 

开机时启动 Nginx

systemctl enable nginx

开机时不启动 Nginx :

systemctl disable nginx

普通命令

nginx 修改配置后 , 使配置生效 :

nginx  -s reload

nginx 检测配置是否正确 :

nginx  -t

Nginx 配置文件

  • Nginx 配置文件目录 : /etc/nginx
  • 主要配置文件目录 : /etc/nginx/nginx.conf
  • 为了更好的管理配置为文件,建议一个域名一个配置文件。
  • Nginx 配置文件必须以 conf 结尾 , 可以存储到 /etc/nginx/conf.d 目录 , 可以创建多个配置文件 。
  • 规范命名配置文件是一种好的习惯 , 例如 : /etc/nginx/conf.d/mydomain.com.conf
  • 如果多个配置文件写了重复的代码 , 你可以创建 /etc/nginx/snippets 目录 ,将重复代码抽取到 snippets 下的文件中,然后使用 include 导入 .
  • Nginx 日志文件 (access.log 和 error.log) 位于 /var/log/nginx/ 目录下 。 推荐为不同的域名创建 access 和 error log 文件。
  • 你可以在任何地方设置你的域名文档目录,推荐 :
    • /home/<user_name>/<site_name>
    • /var/www/<site_name>
    • /var/www/html/<site_name>
    • /opt/<site_name>
    • /usr/share/nginx/html

通过 service 管理 nginx

启动 nginx :

service nginx start

停止 nginx , 服务较多的话 ,要花费一些时间 :

service nginx stop

立即杀死 nginx :

killall -9 nginx

重启 nginx :

service nginx restart

修改 nginx 配置后 , 重新加载 nginx :

service  nginx reload

检测 nginx 是否运行 :

service nginx status  
// 或者使用下方代码
ps -aux | grep nginx

引用

https://linuxize.com/post/how-to-install-nginx-on-centos-7/#conclusion

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值