云服务器 Linux CentOS7 安装配置 nginx

安装 nginx 所需环境

  1. gcc 安装:
shell> yum install gcc-c++
  1. PCRE pcre-devel 安装:
shell> yum install -y pcre pcre-devel
  1. zlib 安装:
shell> yum install -y zlib zlib-devel
  1. OpenSSL 安装
shell> yum install -y openssl openssl-devel

下载 nginx

下载地址:http://nginx.org/en/download.html

开始安装

  1. 将下载好的文件 nginx-1.15.1.tar.gz 上传到 CentOS 服务器 /home/service 目录

  2. 解压文件 nginx-1.15.1.tar.gz

shell> cd /home/service

shell> tar -xzvf nginx-1.15.1.tar.gz
  1. 安装(依次执行如下命令):
shell> cd /home/service/nginx-1.15.1

shell> ./configure --prefix=/home/service/nginx

shell> make

shell> make install
  1. 启动 nginx
shell> /home/service/nginx/sbin/nginx
  1. 检测 nginx 配置文件是否正确
shell> /home/service/nginx/sbin/nginx -t
  1. 重启 nginx 服务
shell> /home/service/nginx/sbin/nginx -s reload
  1. 配置文件所在目录
/home/service/nginx/conf/nginx.conf
  1. 创建软链接
shell> ln -s /home/service/nginx/conf/nginx.conf /home/conf/nginx/
  1. 开放 80 端口
shell> firewall-cmd --zone=public --add-port=6379/tcp --permanent

shell> firewall-cmd --reload

nginx 配置开机启动

  1. 在系统服务目录里创建nginx.service文件
shell> vi /lib/systemd/system/nginx.service
  1. 文件内容如下(注意替换目录)
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/home/service/nginx/sbin/nginx
ExecReload=/home/service/nginx/sbin/nginx -s reload
ExecStop=/home/service/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
  1. 设置开机启动
shell> systemctl enable nginx.service
  1. 其他命令
1> 启动nginx服务
shell> systemctl start nginx.service

2> 设置开机自启动
shell> systemctl enable nginx.service

3> 停止开机自启动
shell> systemctl disable nginx.service

4> 查看服务当前状态
shell> systemctl status nginx.service

5> 重新启动服务
shell> systemctl restart nginx.service

6> 查看所有已启动的服务
shell> systemctl list-units --type=service

参考网址:https://www.cnblogs.com/piscesLoveCc/p/5867900.html


猜你喜欢

转载自blog.csdn.net/qq_24913485/article/details/82114336