Skip to content

二、Nginx安装

1、四大阵营

2、安装

1、二进制安装

以开源版为例

sh
# 进入nginx目录
./configure --prefix=/usr/local/nginx

如果出现警告或报错

sh
 checking for OS
 + Linux 3.10.0-693.el7.x86_64 x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found

安装gcc

sh
yum install -y gcc
sh
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

安装perl库

sh
yum install -y pcre pcre-devel
sh
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.

安装zlib库

sh
yum install -y zlib zlib-devel

接下来执行

sh
make
make install

2、Docker安装

sh
# 注意 外部的/nginx/conf下面的内容必须存在,否则挂载会覆盖
docker run --name nginx-app \
-v /app/nginx/html:/usr/share/nginx/html:ro \
-v /app/nginx/conf:/etc/nginx
-d nginx:1.20.1

# nginx目录放在/opt/docker/下

# 随便启动一个 nginx 实例,只是为了复制出配置
docker run -p 80:80 -p 443:443 --name nginx443 -d nginx:1.20.1

mkdir -p /usr/local/docker/nginx/html
mkdir -p /usr/local/docker/nginx/logs
mkdir -p /usr/local/docker/nginx/conf


# 复制到/opt/docker/temp下
docker container cp nginx:/etc/nginx /opt/docker/temp 
# 比如
docker container cp 8662e037621d:/etc/nginx /usr/local/docker/nginx/conf/

mv /usr/local/docker/nginx/conf/nginx/*  /usr/local/docker/nginx/conf

rm -rf /opt/docker/temp

docker stop nginx

docker rm $Containerid

docker run -p 80:80 --name nginx \
 -v /opt/docker/nginx/html:/usr/share/nginx/html \
 -v /opt/docker/nginx/logs:/var/log/nginx \
 -v /opt/docker/nginx/conf/:/etc/nginx \
 -d nginx:1.10
 
 docker run -p 80:80 -p 443:443 --name nginx \
 -v /usr/local/docker/nginx/html:/usr/share/nginx/html \
 -v /usr/local/docker/nginx/logs:/var/log/nginx \
 -v /usr/local/docker/nginx/conf/:/etc/nginx \
 -d nginx:1.20.1

3、启动Nginx

进入安装好的目录/usr/local/nginx/sbin

sh
./nginx 启动
./nginx -s stop 快速停止
./nginx -s quit 优雅关闭,在退出前完成已经接受的连接请求 
./nginx -s reload 重新加载配置
./nginx -t 检查语法
sh
# 做软链接
ln -sv /usr/local/nginx/sbin/nginx /usr/bin/nginx

nginx # 启动nginx
nginx -t # 检查语法
nginx -s stop  # 停止nginx
nginx -s reload 重新加载配置

4、关于防火墙

1、关闭防火墙

sh
systemctl stop firewalld.service

2、禁止防火墙开机启动

sh
systemctl disable firewalld.service

3、放行端口

sh
firewall-cmd --zone=public --add-port=80/tcp --permanent

4、重启防火墙

sh
firewall-cmd --reload

5、安装成系统服务

创建服务脚本

sh
vi /usr/lib/systemd/system/nginx.service

服务脚本内容

sh
 [Unit]
Description=nginx -  web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target

重新加载系统服务

sh
systemctl daemon-reload

启动服务

sh
systemctl start nginx.service

开机启动

sh
systemctl enable nginx.service