Nginx 安装(源码编译安装)

一、官网上下载版本

下载地址:nginx: download

选择稳定版,下载

Nginx 安装(源码编译安装)_第1张图片

1.22.0的tar.gz包见下:

nginx-1.22.0.tar.gz

二、解压安装包

cd /root/
mkdir -p /root/nginx/nginx-1.22.0/
cd nginx
# 将nginx包解药到指定目录下
tar -zxvf nginx-1.22.0.tar.gz -C /root/nginx/nginx-1.22.0/
 
  

三、编译安装ngixn

cd /root/nginx/nginx-1.22.0/nginx-1.22.0/
ls -lrt
./configure --prefix=/usr/local/nginx-1.22.0/
 
  

报错1:

./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= option.


解法:
yum install -y pcre pcre-devel
 
  

报错2:

./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= option.

解法:
yum install -y zlib zlib-devel

继续编译安装

# 指定安装路径--prefix
./configure --prefix=/usr/local/nginx-1.22.0/

# 编译
make
# 安装
make install

./configure --prefix=/usr/local/nginx-1.22.0/后截图:

Nginx 安装(源码编译安装)_第2张图片

make后截图:

Nginx 安装(源码编译安装)_第3张图片

make install截图:

Nginx 安装(源码编译安装)_第4张图片

四、启动nginx

# 启动前 创建nginx的软连接
ln -s nginx-1.22.0/ nginx
cd /usr/local/nginx/sbin

# 启动
./nginx
# 快速停止
。/nginx -s stop
# 优雅关闭,在退出去完成已经接受的连接请求
./nginx -s quit
# 重新加载配置
./nginx -s reload

五、启动后验证:

ss -tanp | grep 80

浏览器键入服务器ip,访问下:

Nginx 安装(源码编译安装)_第5张图片

六、关于防火墙

# 关闭防火墙
systemctl stop firewalld.service
# 禁止防火墙开机自启
systemctl disable firewalld.service
# 放行端口
firewall-cmd --zone=public -add-port=80/tcp --permanent
# 重启防火墙
firewall-cmd --reload

七、安装成系统服务

创建服务脚本

vim /usr/lib/systemd/system/nginx.service

服务脚本内容

[Unit]
Description=nginx - web server by chaitc
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
重新加载系统服务

systemctl daemon-reload

八、用systemctl启动

# 停止
systemctl stop nginx

# 查看状态
systemctl start nginx

#启动
systemctl start nginx

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

Nginx 安装(源码编译安装)_第6张图片

你可能感兴趣的:(ctc的运维学习笔记,实验手册,工具类,nginx,服务器,运维)