0
点赞
收藏
分享

微信扫一扫

源码编译安装Nginx1.20.2

夏沐沐 2022-07-27 阅读 96

1. 下载源码包

[root@webserver ~]# cd /usr/local/src/
[root@webserver src]# curl -O https://nginx.org/download/nginx-1.20.2.tar.gz

2. 安装依赖相关

[root@webserver ~]# yum install pcre pcre-devel -y  #rewrite正则相关pcre : URL重写软件,实现伪静态\URL跳转等SEO优化,pcre意思就是Perl的正则表达式。
[root@webserver ~]# yum install openssl openssl-devel -y #https加密访问用它。
# 检查下载没问题
[root@webserver ~]# rpm -qa pcre pcre-devel openssl openssl-devel
pcre-8.32-17.el7.x86_64
pcre-devel-8.32-17.el7.x86_64
openssl-devel-1.0.2k-25.el7_9.x86_64
openssl-1.0.2k-25.el7_9.x86_64

3.创建nginx用户

[root@webserver src]# useradd -u 1111 -s /sbin/nologin -M www

4. 编译安装

[root@webserver ~]# cd /usr/local/src/
[root@webserver src]# ls
nginx-1.20.2.tar.gz
[root@webserver src]# tar xf nginx-1.20.2.tar.gz 
[root@webserver src]# cd nginx-1.20.2/
[root@webserver nginx-1.20.2]# ./configure --user=www --group=www --prefix=/application/nginx-1.20.2/ --with-http_stub_status_module --with-http_ssl_module --with-pcre
[root@webserver nginx-1.20.2]# make -j 4
[root@webserver nginx-1.20.2]# make install

安装后操作

5.建立软链接

[root@webserver nginx-1.20.2]# ln -s /application/nginx-1.20.2/ /application/nginx

6.启动nginx

# 查看80端口是否占用
[root@webserver nginx-1.20.2]# lsof -i :80  
# 查看nginx配置是否有问题
[root@webserver nginx-1.20.2]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.20.2//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.20.2//conf/nginx.conf test is successful
# 将nginx路径添加到PATH变量
[root@webserver ~]# echo 'export PATH="/application/nginx/sbin/:$PATH"' >> /etc/profile
[root@webserver ~]# . /etc/profile

7.将nginx注册到系统服务单元

# 在另外一台装有nginx的主机webserver2上查看yum安装的nginx相关文件位置,找到nginx.service文件vim打开并复制其内容
[root@webserver2 ~]# rpm -ql nginx
/etc/logrotate.d/nginx
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/mime.types
/etc/nginx/modules
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx
/usr/lib64/nginx/modules
/usr/libexec/initscripts/legacy-actions/nginx
/usr/libexec/initscripts/legacy-actions/nginx/check-reload
/usr/libexec/initscripts/legacy-actions/nginx/upgrade
/usr/sbin/nginx
/usr/sbin/nginx-debug
/usr/share/doc/nginx-1.22.0
/usr/share/doc/nginx-1.22.0/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/cache/nginx
/var/log/nginx
# /usr/lib/systemd/system/nginx.service,就是这个文件,vim打开并复制所有内容,保存到webserver两样的路径和文件名上
[root@webserver2 ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"

[Install]
WantedBy=multi-user.target
# 创建文件并修改内容,如下
[root@webserver ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/application/nginx/logs/nginx.pid
ExecStart=/application/nginx/sbin/nginx -c /application/nginx/conf/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /application/nginx/logs/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /application/nginx/logs/nginx.pid)"

[Install]
WantedBy=multi-user.target
[root@webserver ~]# nginx -s stop
[root@webserver ~]# netstat -anput|grep nginx
[root@webserver ~]# netstat -anput|grep 80
[root@webserver ~]# ps -ef|grep nginx
root       7086   6632  0 14:04 pts/0    00:00:00 grep --color=auto nginx
[root@webserver ~]# systemctl enable --now nginx.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
# 下面的三条命令结果均表示systemctl 启动nginx成功
[root@webserver ~]# ps -ef|grep nginx
root       7115      1  0 14:04 ?        00:00:00 nginx: master process /application/nginx/sbin/nginx -c /application/nginx/conf/nginx.conf
www        7116   7115  0 14:04 ?        00:00:00 nginx: worker process
root       7118   6632  0 14:04 pts/0    00:00:00 grep --color=auto nginx
[root@webserver ~]# netstat -anput|grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7115/nginx: master
[root@webserver ~]# lsof -i :80
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   7115 root    6u  IPv4  43103      0t0  TCP *:http (LISTEN)
nginx   7116  www    6u  IPv4  43103      0t0  TCP *:http (LISTEN)
举报

相关推荐

0 条评论