Linux(CentOS7)下Nginx安装

记录一下 Linux(CentOS7) 下 Nginx 安装过程

作者:IT王小二
博客:https://itwxe.com

一、准备工作

版本说明:

  • Linux版本:CentOS 7 64位
  • Nginx版本:nginx-1.20.0

1. 下载安装文件

采用源码包方式安装,当然使用 yum 方式安装也可以,此处使用源码包安装。

进入目录(个人习惯/usr/local),下载安装文件,如果云服务器下载速度过慢也可本地下载后上传。

cd /usr/local
wget http://nginx.org/download/nginx-1.20.0.tar.gz

2. 安装Nginx所需要的依赖

# 安装gcc、gcc-c++
yum -y install gcc
yum -y install gcc-c++

# 安装pcre 、zilb
yum -y install pcre*
yum -y install zlib*

# 安装openssl(若需要支持 https 协议)
yum -y install openssl
yum -y install openssl-devel

二、安装及配置Nginx

1. 安装Nginx

1、解压安装包。

tar -zxvf nginx-1.20.0.tar.gz

2、为编译安装做准备,进入解压目录。

cd nginx-1.20.0

3、为编译安装做准备。

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-pcre

注:
--prefix:设置安装路径。
--with-http_stub_status_module:支持nginx状态查询。
--with-http_ssl_module:支持https。
--with-pcre:为了支持rewrite重写功能,必须制定pcre。

4、编译安装。

make && make install

5、指定配置文件启动 Nginx。

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

在浏览器访问 ip,看到如下信息则安装成功。

Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

2. 一些命令

修改自己的配置之后,验证配置是否正确,重启 Nginx 命令

# 进入目录
cd /usr/local/nginx/sbin

# 验证配置是否正确
./nginx -t
# 如果看到如下内容, 那么配置正确, 可以重启Nginx
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

# 重启Nginx, 之后就可以通过域名访问了, 哈哈
./nginx -s reload

3. Nginx开机自启

# 新建文件
vim /lib/systemd/system/nginx.service

# 添加内容
[Unit]
Description=nginx.server
After=network.target

[Service]
Type=forking
PIDFILE=/var/run/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecRepload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

然后使用 systemctl enable nginx.service 开启 nginx 开机自启,重启 CentOS 查看效果。

# 启动nginx服务
systemctl start nginx.service

# 停止nginx服务
systemctl stop nginx.service

# 重启nginx服务
systemctl restart nginx.service

# 查看nginx服务当前状态
systemctl status nginx.service

# 设置nginx服务开机自启动
systemctl enable nginx.service

# 停止nginx服务开机自启动
systemctl disable nginx.service

结语:当然Nginx除了反向代理,还有负载均衡等功能,这个就不在本文章范围内了。

都读到这里了,来个 点赞、评论、关注、收藏 吧!

posted @ 2021-06-01 10:43  IT王小二  阅读(297)  评论(0编辑  收藏  举报