一、下载码源包
1. 获得源码包途径
官方网站,可以获得最新的软件包
Nginx: www.nginx.org
2.具体实例展示(tengine)
下载源码包,准备软件包
准备编译环境如编译器gcc、make
# yum -y install gcc make zlib-devel pcre pcre-devel openssl-devel
(pcre: 支持正则表达式,地址重写rewrite)
解压(下载在真机上的,rz上传,rz在vm虚拟机不可用,在xsheel、final shell可用)
# useradd www(用root用户的话权限太大)
# tar xvf tengine-2.2.0.tar.gz
# cd tengine-2.2.0
配置
./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' --add-module=/root/nginx-1.16.1/src/nginx-auth-ldap
编译
# make
安装
# make install
二、码源安装过程错误
error1:
the HTTP XSLT module requires the libxml2/libxslt
libraries. You can either do not enable the module or install the libraries.
解决方案:
# yum -y install libxml2 libxml2-dev
libxslt-devel
error2:
perl module ExtUtils::Embed is required
解决方案:
#yum -y install perl-devel perl-ExtUtils-Embed
error3:
the GeoIP module requires the GeoIP library.
You can either do not enable the module or install the library.
解决方案:
# yum -y install GeoIP GeoIP-devel GeoIP-data
缺少GD库换成163的yum源
#yum install gd-devel pcre-devel libcurl-devel
#yum install gperftoo
安装ldap模块
编译nginx-auth-ldap模块需要ldap.h头文件,所以需要先安装ldap库
#yum -y install openldap-devel
到nginx的src目录下下载ldap模块
#cd src/
#git clone https:
//github.com/kvspb/nginx-auth-ldap.git
[root@localhost ~]# /usr/sbin/nginx
nginx: [emerg] getpwnam("nginx") failed
[root@localhost ~]# useradd -s /sbin/nologin -M nginx
[root@localhost ~]# id nginx
uid=1000(nginx) gid=1000(nginx) 组=1000(nginx)
[root@localhost ~]# /usr/sbin/nginx -s reload
nginx: [error] open() "/run/nginx.pid" failed (2: No such file or directory)
[root@localhost ~]# mkdir -p /var/lib/nginx/tmp/client_body
[root@localhost ~]# /usr/sbin/nginx
三、把nginx加入systemd管理
#vim /lib/systemd/system/nginx.service
[Unit] Description=The NGINX HTTP and reverse proxy server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/run/nginx.pid ExecStartPre=/usr/sbin/nginx -t ExecStart=/usr/sbin/nginx ExecReload=/usr/sbin/nginx -s reload ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
注意:将上面内容中的路径改为实际的路径。
重载systemd配置文件
systemctl daemon-reload
启动服务
systemctl start nginx.service
开机启动
systemctl enable nginx.service
如果报错:
systemctl status nginx
● nginx.service - The NGINX HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since 五 2020-01-03 11:18:56 CST; 12min ago
1月 03 11:18:54 localhost.localdomain nginx[18252]: nginx: [emerg] bind() to 0.0.0.0:80 ...e)
1月 03 11:18:54 localhost.localdomain nginx[18252]: nginx: [emerg] bind() to 0.0.0.0:80 ...e)
1月 03 11:18:55 localhost.localdomain nginx[18252]: nginx: [emerg] bind() to 0.0.0.0:80 ...e)
1月 03 11:18:55 localhost.localdomain nginx[18252]: nginx: [emerg] bind() to 0.0.0.0:80 ...e)
1月 03 11:18:56 localhost.localdomain nginx[18252]: nginx: [emerg] bind() to 0.0.0.0:80 ...e)
1月 03 11:18:56 localhost.localdomain systemd[1]: nginx.service: control process exited,...=1
1月 03 11:18:56 localhost.localdomain nginx[18252]: nginx: [emerg] still could not bind()
1月 03 11:18:56 localhost.localdomain systemd[1]: Failed to start The NGINX HTTP and rev...r.
1月 03 11:18:56 localhost.localdomain systemd[1]: Unit nginx.service entered failed state.
1月 03 11:18:56 localhost.localdomain systemd[1]: nginx.service failed.
Hint: Some lines were ellipsized, use -l to show in full.
查看日志
[root@localhost nginx]# cat error.log
2020/01/03 10:55:33 [emerg] 18010#0: getpwnam("nginx") failed
2020/01/03 10:55:41 [emerg] 18011#0: getpwnam("nginx") failed
2020/01/03 10:56:03 [emerg] 18013#0: getpwnam("nginx") failed
2020/01/03 10:57:24 [notice] 18021#0: signal process started
2020/01/03 10:57:24 [error] 18021#0: open() "/run/nginx.pid" failed (2: No such file or directory)
2020/01/03 10:57:28 [emerg] 18022#0: mkdir() "/var/lib/nginx/tmp/client_body" failed (2: No such file or directory)
2020/01/03 10:59:39 [error] 18026#0: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.40.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.40.137"
2020/01/03 11:11:03 [emerg] 18135#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2020/01/03 11:11:03 [emerg] 18135#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2020/01/03 11:11:03 [emerg] 18135#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2020/01/03 11:11:03 [emerg] 18135#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2020/01/03 11:11:03 [emerg] 18135#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2020/01/03 11:11:03 [emerg] 18135#0: still could not bind()
2020/01/03 11:12:38 [emerg] 18213#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2020/01/03 11:12:38 [emerg] 18213#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2020/01/03 11:12:38 [emerg] 18213#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2020/01/03 11:12:38 [emerg] 18213#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2020/01/03 11:12:38 [emerg] 18213#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2020/01/03 11:12:38 [emerg] 18213#0: still could not bind()
2020/01/03 11:13:02 [error] 18026#0: *2 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.40.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.40.137"
2020/01/03 11:13:06 [error] 18026#0: *2 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.40.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.40.137"
2020/01/03 11:13:07 [error] 18026#0: *2 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.40.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.40.137"
2020/01/03 11:13:07 [error] 18026#0: *2 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.40.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.40.137"
2020/01/03 11:13:08 [error] 18026#0: *2 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.40.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.40.137"
2020/01/03 11:13:22 [error] 18026#0: *3 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.40.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.40.137"
2020/01/03 11:14:05 [error] 18026#0: *2 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.40.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.40.137"
2020/01/03 11:18:54 [emerg] 18252#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2020/01/03 11:18:54 [emerg] 18252#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2020/01/03 11:18:54 [emerg] 18252#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2020/01/03 11:18:54 [emerg] 18252#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2020/01/03 11:18:54 [emerg] 18252#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2020/01/03 11:18:54 [emerg] 18252#0: still could not bind()
解决:
[root@localhost nginx]# ss -ntlp|grep 80
LISTEN 0 128 *:80 *:* users:(("nginx",pid=18026,fd=6),("nginx",pid=18025,fd=6))
[root@localhost nginx]# kill -9 18025
[root@localhost nginx]# kill -9 18026
[root@localhost nginx]# systemctl restart nginx
[root@localhost nginx]# systemctl status nginx
● nginx.service - The NGINX HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since 五 2020-01-03 11:39:29 CST; 7s ago
Process: 18357 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 18356 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Main PID: 18359 (nginx)
CGroup: /system.slice/nginx.service
├─18359 nginx: master process /usr/sbin/nginx
└─18361 nginx: worker process
四、安装完成还有一堆麻烦事(另一次解决记录,与上面关系不大)
1、启动测试报错
# /srv/nginx/sbin/nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
2、systemctl 查看状态
# systemctl status nginx (restart也不行)
Unit nginx.service could not be found.
# chmod +x /usr/lib/systemd/system/nginx.service
#vim /usr/lib/systemd/system/nginx.service
[Unit] //对服务的说明
Description=nginx - high performance web server //描述服务
After=network.target remote-fs.target nss-lookup.target //描述服务类别
[Service] //服务的一些具体运行参数的设置
Type=forking //后台运行的形式
PIDFile=/srv/nginx/logs/nginx.pid //PID文件的路径
ExecStartPre=/srv/nginx/sbin/nginx -t -c /srv/nginx/conf/nginx.conf //启动准备
ExecStart=/srv/nginx/sbin/nginx -c /srv/nginx/conf/nginx.conf //启动命令
ExecReload=/srv/nginx/sbin/nginx -s reload //重启命令
ExecStop=/srv/nginx/sbin/nginx -s stop //停止命令
ExecQuit=/srv/nginx/sbin/nginx -s quit //快速停止
PrivateTmp=true //给服务分配临时空间
[Install]
WantedBy=multi-user.target //服务用户的模式
4、启动服务
#systemctl daemon-reload
5、还是报错
# systemctl status nginx.service -l
6、# ps aux|grep nginx
root 11675 0.0 0.1 46088 1156 ? Ss 02:20 0:00 nginx: master process /srv/ngin/sbin/nginx
nobody 11676 0.0 0.2 46548 2180 ? S 02:20 0:00 nginx: worker process
root 28181 0.0 0.0 112660 968 pts/0 R+ 02:35 0:00 grep --color=auto nginx
# kill -9 11675
# kill -9 11676
# ps aux|grep nginx
root 28183 0.0 0.0 112660 964 pts/0 R+ 02:35 0:00 grep --color=auto nginx
# systemctl restart nginx
# systemctl status nginx
# /srv/nginx/sbin/nginx -v
nginx version: nginx/1.14.2
至此大功告成