源码编译(nginx)
Nginx 编译安装
编译器介绍
css
源码安装需要提前准备标准的编译器,GCC的全称是(GNU Compiler collection),其有GNU开发,并以GPL即LGPL许可,是自由的类UNIX即苹果电脑Mac OS X操作系统的标准编译器,因为GCC原本只能处理C语言,所以原名为GNU C语言编译器,后来得到快速发展,可以处理C++,Fortran,pascal,objective-C,java以及Ada等其他语言,此外还需要Automake工具,以完成自动创建Makefile的工作,Nginx的一些模块需要依赖第三方库,比如: pcre(支持rewrite),zlib(支持gzip模块)和openssl(支持ssl模块)等。
```
1.编译安装 Nginx
官方源码包下载地址:
```http
https://nginx.org/en/download.html
```
范例: 编译安装
```sh
[root@centos7 ~]#yum -y install gcc pcre-devel openssl-devel zlib-devel wget
[root@centos7 ~]#useradd -s /sbin/nologin nginx
[root@centos7 ~]#cd /usr/local/src/
[root@centos7 /usr/local/src]#wget https://nginx.org/download/nginx-1.18.0.tar.gz
[root@centos7 /usr/local/src]#tar xf nginx-1.18.0.tar.gz
[root@centos7 /usr/local/src]#cd nginx-1.18.0
[root@centos7 /usr/local/src/nginx-1.18.0]#ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@centos7 /usr/local/src/nginx-1.18.0]#./configure --prefix=/apps/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module
[root@centos7 /usr/local/src/nginx-1.18.0]#make -j 4 && make -j 4 install
#修改权限
[root@centos7 /usr/local/src/nginx-1.18.0]#chown -R nginx.nginx /apps/nginx
```
Nginx完成安装以后,有四个主要的目录
```sh
#生成目录
[root@centos7 ~]#ll /apps/nginx/
总用量 0
drwxr-xr-x 2 nginx nginx 333 6月 12 11:09 conf
drwxr-xr-x 2 nginx nginx 40 6月 12 11:09 html
drwxr-xr-x 2 nginx nginx 6 6月 12 11:09 logs
drwxr-xr-x 2 nginx nginx 19 6月 12 11:09 sbin
#conf:保存nginx所有的配置文件,其中nginx.conf是nginx服务器的最核心最主要的配置文件,其他的.conf则是用来配置nginx相关的功能的,例如fastcgi功能使用的是fastcgi.conf和fastcgi_params两个文件,配置文件一般都有一个样板配置文件,是以.default为后缀,使用时可将其复制并将default后缀去掉即可。
#html:保存了nginx服务器的web文件,但是可以更改为其他目录保存web文件,另外还有一个50x的web文件是默认的错误页面提示页面。
#logs:用来保存nginx服务器的访问日志错误日志等日志,logs目录可以放在其他路径,比如/var/logs/nginx里面。
#sbin:保存nginx二进制启动脚本,可以接受不同的参数以实现不同的功能。
```
2.验证版本及编译参数
```sh
[root@centos7 ~]#ls /apps/nginx/sbin/
nginx
[root@centos7 ~]#ln -s /apps/nginx/sbin/nginx /usr/sbin/
#查看版本
[root@centos7 ~]#nginx -v
nginx version: nginx/1.18.0
#查看编译参数
[root@centos7 ~]#nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
```
3 启动和停止 nginx 测试访问 web 界面
```sh
#启动nginx
[root@centos7 ~]#nginx
#浏览器可以访问看到下面图示
[root@centos7 ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :80 :*
LISTEN 0 128 :22 :*
LISTEN 0 100 127.0.0.1:25 :
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
#关闭nginx
[root@centos7 ~]#nginx -s stop
[root@centos7 ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :22 :*
LISTEN 0 100 127.0.0.1:25 :
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
[root@centos7 ~]#nginx
```

4 .创建 Nginx 自启动文件
```sh
#复制同一版本的nginx的yum安装生成的service文件
[root@centos7 ~]#cat > /usr/lib/systemd/system/nginx.service << EOF
[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=/apps/nginx/run/nginx.pid #指定pid文件的目录,默认在logs目录下,可选配置
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/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
EOF
#创建pid文件存放的目录
[root@centos7 ~]#mkdir -p /apps/nginx/run/
#修改配置文件
[root@centos7 ~]#vim /apps/nginx/conf/nginx.conf
pid /apps/nginx/run/nginx.pid;
```
5. 验证 Nginx 自启动文件
```sh
#杀死nginx所有进程
[root@centos7 ~]#pkill -9 nginx
[root@centos7 ~]#systemctl daemon-reload
[root@centos7 ~]#systemctl enable --now nginx
[root@centos7 ~]#ll /apps/nginx/run/
总用量 4
-rw-r--r-- 1 root root 6 6月 12 11:22 nginx.pid
[root@centos7 ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :80 :*
LISTEN 0 128 :22 :*
LISTEN 0 100 127.0.0.1:25 :
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
[root@centos7 ~]#systemctl stop nginx
[root@centos7 ~]#systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since 一 2023-06-12 11:23:37 CST; 8s ago
Docs: http://nginx.org/en/docs/
Process: 10550 ExecStop=/bin/sh -c /bin/kill -s TERM $(/bin/cat /var/run/nginx.pid) (code=exited, status=1/FAILURE)
Process: 10538 ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 10539 (code=exited, status=0/SUCCESS)
6月 12 11:23:37 centos7 sh[10550]: -q, --queue <信号> 使用 sigqueue(2) 代替 kill(2)
6月 12 11:23:37 centos7 sh[10550]: -p, --pid 打印 pid 而不向它们发送信号
6月 12 11:23:37 centos7 sh[10550]: -l, --list [=<信号>] 列出信号名,或将一个信号转换为名称
6月 12 11:23:37 centos7 sh[10550]: -L, --table 列出信号名和数值
6月 12 11:23:37 centos7 sh[10550]: -h, --help 显示此帮助并退出
6月 12 11:23:37 centos7 sh[10550]: -V, --version 输出版本信息并退出
6月 12 11:23:37 centos7 sh[10550]: 更多信息请参阅 kill(1)。
6月 12 11:23:37 centos7 systemd[1]: Stopped nginx - high performance web server.
6月 12 11:23:37 centos7 systemd[1]: Unit nginx.service entered failed state.
6月 12 11:23:37 centos7 systemd[1]: nginx.service failed.
[root@centos7 ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :22 :*
LISTEN 0 100 127.0.0.1:25 :
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
```
##