二级制安装nginx

0、环境

操作系统版本:centos7.9
nginx版本:nginx-1.12.2

1、下载nginx源码文件

nginx官网
http://nginx.org/en/download.html
选择一个版本进行下载,下载后可以得到一个压缩包

(一)二级制安装nginx_nginx

2、解压源码包

1、将压缩包上传到系统中,并解压到当前目录
[root@nginx ~]# tar zxvf nginx-1.12.2.tar.gz
[root@nginx ~]# ll
total 964
-rw-------. 1 root root 1510 Jun 8 16:50 anaconda-ks.cfg
drwxr-xr-x. 9 1001 1001 186 Sep 19 00:53 nginx-1.12.2
-rw-r--r--. 1 root root 981687 Sep 19 00:51 nginx-1.12.2.tar.gz

3、编译安装

1、配置国内yum源(阿里源)
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo

2、安装编译所需的依赖包(根据./configure命令的报错,安装缺失的软件包)
yum install -y gcc zlib-devel pcre-devel

3、开始安装
[root@nginx ~]# cd /root/nginx-1.12.2
[root@nginx ~]# ./configure --prefix=/usr/local/nginx
[root@nginx ~]# make
[root@nginx ~]# make install

./configure参数:
--user 指定启动nginx的用户
--group 指定启动nginx的用户组
--prefix 指定nginx安装的根路径
--with-stream 支持upstrem负载均衡
--with-http_ssl_module 支持SSL,需要有OpenSSL库(系统默认就有)
--with-http_realip_module 支持传递http头中的客户端IP地址
--with-http_geoip_module 支持设置各种变量以便在配置文件中的区段使用,基于地理位置查找客户端IP地址(需安装geoip-devel,


4、查看软件安装
[root@nginx ~]# ls /usr/local/nginx/
conf html logs sbin

5、使用脚本启动nginx
[root@nginx ~]# cd /usr/local/nginx/sbin
[root@nginx sbin]# ./nginx
[root@nginx sbin]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 21940/nginx: master
······

6、查看到80端口即服务安装成功

4、测试

浏览器访问本地的80端口

(一)二级制安装nginx_nginx_02

通过systemd管理nginx

0、创建服务文件

[root@nginx ~]# cat /lib/systemd/system/nginx.service 
[Unit]
Descriptinotallow=nginx # nginx服务描述
After=network.target # 描述服务类型

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid # 服务PID文件位置
ExecStart=/usr/local/nginx/sbin/nginx # 启动服务
ExecReload=/bin/kill -s HUP $MAINPID # 根据PID号重载服务
ExecStop=/bin/kill -s QUIT $MAINPID # 根据PID号终止进程
PrivateTmp=true

[Install]
WantedBy=multi-user.target

1、启动服务测试

1、重新加载系统内存
[root@nginx ~]# systemcrl daemon-reload

2、停止系统中存在的nginx进程
[root@nginx ~]# ps -aux | grep nginx
root 21940 0.0 0.0 20540 600 ? Ss 01:19 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 21941 0.0 0.0 20984 1548 ? S 01:19 0:00 nginx: worker process
root 22146 0.0 0.0 112808 964 pts/0 R+ 02:03 0:00 grep --color=auto nginx

kill -9 21940 21941

3、使用systemctl启动nginx服务
[root@nginx ~]# systemctl start nginx
[root@nginx ~]# systemctl enable nginx
[root@nginx ~]# systemctl status nginx
● nginx.service - nginx
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2022-09-19 01:19:17 CST; 45min ago
Main PID: 21940 (nginx)
CGroup: /system.slice/nginx.service
├─21940 nginx: master process /usr/local/nginx/sbin/nginx
└─21941 nginx: worker process

Sep 19 01:19:17 nginx systemd[1]: Stopped nginx.
Sep 19 01:19:17 nginx systemd[1]: Starting nginx...
Sep 19 01:19:17 nginx systemd[1]: Can't open PID file /usr/local/nginx/logs/nginx.pid (yet?) after start: No such file or directory
Sep 19 01:19:17 nginx systemd[1]: Started nginx.

ps:没有报错就说明配置成功,之后就可以通过systemd来管理nginx服务