环境说明
1、系统:CentOS最小化安装;升级软件补丁,内核和系统版本不升级;关闭SELinux和防火墙。系统安装可参考CentOS 7系统部署
2、软件:安装包统一放置在/usr/src目录下,安装位置统一在/usr/local目录下。
开始安装
1、安装Nginx所需pcre库,官方网站(www.pcre.org)。安装pcre库是为了使Nginx支持具备URL重写功能的Rewite模块,如不安装则Nginx无法使用此模块功能(典型功能为伪静态)。
[root@localhost ~]# yum -y install pcre-devel
Loaded plugins: fastestmirror
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package pcre-devel.x86_64 0:8.32-17.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
===============================================================================================================================================================================================================
Package Arch Version Repository Size
===============================================================================================================================================================================================================
Installing:
pcre-devel x86_64 8.32-17.el7 base 480 k
Transaction Summary
===============================================================================================================================================================================================================
Install 1 Package
Total download size: 480 k
Installed size: 1.4 M
Downloading packages:
pcre-devel-8.32-17.el7.x86_64.rpm | 480 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : pcre-devel-8.32-17.el7.x86_64 1/1
Verifying : pcre-devel-8.32-17.el7.x86_64 1/1
Installed:
pcre-devel.x86_64 0:8.32-17.el7
Complete!
2、安装编译需要的组件
[root@localhost ~]# yum -y install zlib-devel openssl-devel
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package openssl-devel.x86_64 1:1.0.2k-8.el7 will be installed
--> Processing Dependency: krb5-devel(x86-64) for package: 1:openssl-devel-1.0.2k-8.el7.x86_64
---> Package zlib-devel.x86_64 0:1.2.7-17.el7 will be installed
--> Running transaction check
---> Package krb5-devel.x86_64 0:1.15.1-8.el7 will be installed
--> Processing Dependency: libkadm5(x86-64) = 1.15.1-8.el7 for package: krb5-devel-1.15.1-8.el7.x86_64
--> Processing Dependency: libverto-devel for package: krb5-devel-1.15.1-8.el7.x86_64
--> Processing Dependency: libselinux-devel for package: krb5-devel-1.15.1-8.el7.x86_64
--> Processing Dependency: libcom_err-devel for package: krb5-devel-1.15.1-8.el7.x86_64
--> Processing Dependency: keyutils-libs-devel for package: krb5-devel-1.15.1-8.el7.x86_64
--> Running transaction check
---> Package keyutils-libs-devel.x86_64 0:1.5.8-3.el7 will be installed
---> Package libcom_err-devel.x86_64 0:1.42.9-10.el7 will be installed
---> Package libkadm5.x86_64 0:1.15.1-8.el7 will be installed
---> Package libselinux-devel.x86_64 0:2.5-11.el7 will be installed
--> Processing Dependency: libsepol-devel(x86-64) >= 2.5-6 for package: libselinux-devel-2.5-11.el7.x86_64
--> Processing Dependency: pkgconfig(libsepol) for package: libselinux-devel-2.5-11.el7.x86_64
---> Package libverto-devel.x86_64 0:0.2.5-4.el7 will be installed
--> Running transaction check
---> Package libsepol-devel.x86_64 0:2.5-6.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
===============================================================================================================================================================================================================
Package Arch Version Repository Size
===============================================================================================================================================================================================================
Installing:
openssl-devel x86_64 1:1.0.2k-8.el7 base 1.5 M
zlib-devel x86_64 1.2.7-17.el7 base 50 k
Installing for dependencies:
keyutils-libs-devel x86_64 1.5.8-3.el7 base 37 k
krb5-devel x86_64 1.15.1-8.el7 base 266 k
libcom_err-devel x86_64 1.42.9-10.el7 base 31 k
libkadm5 x86_64 1.15.1-8.el7 base 174 k
libselinux-devel x86_64 2.5-11.el7 base 186 k
libsepol-devel x86_64 2.5-6.el7 base 74 k
libverto-devel x86_64 0.2.5-4.el7 base 12 k
Transaction Summary
===============================================================================================================================================================================================================
Install 2 Packages (+7 Dependent packages)
Total download size: 2.3 M
Installed size: 4.6 M
Downloading packages:
(1/9): keyutils-libs-devel-1.5.8-3.el7.x86_64.rpm | 37 kB 00:00:00
(2/9): libcom_err-devel-1.42.9-10.el7.x86_64.rpm | 31 kB 00:00:00
(3/9): libkadm5-1.15.1-8.el7.x86_64.rpm | 174 kB 00:00:00
(4/9): krb5-devel-1.15.1-8.el7.x86_64.rpm | 266 kB 00:00:00
(5/9): libselinux-devel-2.5-11.el7.x86_64.rpm | 186 kB 00:00:00
(6/9): libsepol-devel-2.5-6.el7.x86_64.rpm | 74 kB 00:00:00
(7/9): libverto-devel-0.2.5-4.el7.x86_64.rpm | 12 kB 00:00:00
(8/9): zlib-devel-1.2.7-17.el7.x86_64.rpm | 50 kB 00:00:00
(9/9): openssl-devel-1.0.2k-8.el7.x86_64.rpm | 1.5 MB 00:00:00
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 4.2 MB/s | 2.3 MB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : libkadm5-1.15.1-8.el7.x86_64 1/9
Installing : libsepol-devel-2.5-6.el7.x86_64 2/9
Installing : libselinux-devel-2.5-11.el7.x86_64 3/9
Installing : libcom_err-devel-1.42.9-10.el7.x86_64 4/9
Installing : libverto-devel-0.2.5-4.el7.x86_64 5/9
Installing : keyutils-libs-devel-1.5.8-3.el7.x86_64 6/9
Installing : krb5-devel-1.15.1-8.el7.x86_64 7/9
Installing : zlib-devel-1.2.7-17.el7.x86_64 8/9
Installing : 1:openssl-devel-1.0.2k-8.el7.x86_64 9/9
Verifying : zlib-devel-1.2.7-17.el7.x86_64 1/9
Verifying : keyutils-libs-devel-1.5.8-3.el7.x86_64 2/9
Verifying : libverto-devel-0.2.5-4.el7.x86_64 3/9
Verifying : libselinux-devel-2.5-11.el7.x86_64 4/9
Verifying : krb5-devel-1.15.1-8.el7.x86_64 5/9
Verifying : libcom_err-devel-1.42.9-10.el7.x86_64 6/9
Verifying : libsepol-devel-2.5-6.el7.x86_64 7/9
Verifying : 1:openssl-devel-1.0.2k-8.el7.x86_64 8/9
Verifying : libkadm5-1.15.1-8.el7.x86_64 9/9
Installed:
openssl-devel.x86_64 1:1.0.2k-8.el7 zlib-devel.x86_64 0:1.2.7-17.el7
Dependency Installed:
keyutils-libs-devel.x86_64 0:1.5.8-3.el7 krb5-devel.x86_64 0:1.15.1-8.el7 libcom_err-devel.x86_64 0:1.42.9-10.el7 libkadm5.x86_64 0:1.15.1-8.el7 libselinux-devel.x86_64 0:2.5-11.el7
libsepol-devel.x86_64 0:2.5-6.el7 libverto-devel.x86_64 0:0.2.5-4.el7
Complete!
3、创建软件配套专用的用户(禁止登录及不创建家目录)
[root@localhost ~]# useradd www -s /sbin/nologin -M
4、进入/usr/src目录下载并解压软件包
[root@localhost src]# cd /usr/src/
[root@localhost src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz && tar -zxf nginx-1.12.2.tar.gz
--2018-03-02 13:05:56-- http://nginx.org/download/nginx-1.12.2.tar.gz
Resolving nginx.org (nginx.org)... 206.251.255.63, 95.211.80.227, 2606:7100:1:69::3f, ...
Connecting to nginx.org (nginx.org)|206.251.255.63|:80... connected.
HTTP request sent, awAIting response... 200 OK
Length: 981687 (959K) [application/octet-stream]
Saving to: ‘nginx-1.12.2.tar.gz’
100%[=====================================================================================================================================================================>] 981,687 8.26KB/s in 2m 10s
2018-03-02 13:08:07 (7.36 KB/s) - ‘nginx-1.12.2.tar.gz’ saved [981687/981687]
5、进入解压的软件目录并编译配置文件 ./configure –user=www –group=www –prefix=/usr/local/nginx-1.12.2 –with-mail_ssl_module –with-http_stub_status_module –with-http_ssl_module –with-http_v2_module –with-http_gzip_static_module –with-http_sub_module
[root@localhost src]# cd nginx-1.12.2
[root@localhost nginx-1.12.2]# ./configure --user=www --group=www --prefix=/usr/local/nginx-1.12.2 --with-mail_ssl_module --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module
checking for OS
+ Linux 3.10.0-327.el7.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
checking for gcc -pipe Switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
......此处省略......
checking for PCRE library ... found
checking for PCRE JIT support ... found
checking for OpenSSL library ... found
checking for zlib library ... found
creating objs/Makefile
Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ using system zlib library
nginx path prefix: "/usr/local/nginx-1.12.2"
nginx binary file: "/usr/local/nginx-1.12.2/sbin/nginx"
nginx modules path: "/usr/local/nginx-1.12.2/modules"
nginx configuration prefix: "/usr/local/nginx-1.12.2/conf"
nginx configuration file: "/usr/local/nginx-1.12.2/conf/nginx.conf"
nginx pid file: "/usr/local/nginx-1.12.2/logs/nginx.pid"
nginx error log file: "/usr/local/nginx-1.12.2/logs/error.log"
nginx http access log file: "/usr/local/nginx-1.12.2/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp
6、编译及安装软件
[root@localhost nginx-1.12.2]# make && make install
make -f objs/Makefile
make[1]: Entering directory `/usr/src/nginx-1.12.2'
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs
-o objs/src/core/nginx.o
src/core/nginx.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs
-o objs/src/core/ngx_log.o
src/core/ngx_log.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs
-o objs/src/core/ngx_palloc.o
src/core/ngx_palloc.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs
-o objs/src/core/ngx_array.o
src/core/ngx_array.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs
-o objs/src/core/ngx_list.o
src/core/ngx_list.c
......此处省略......
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -I src/http -I src/http/modules -I src/http/v2
-o objs/src/http/modules/ngx_http_upstream_least_conn_module.o
src/http/modules/ngx_http_upstream_least_conn_module.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -I src/http -I src/http/modules -I src/http/v2
-o objs/src/http/modules/ngx_http_upstream_keepalive_module.o
src/http/modules/ngx_http_upstream_keepalive_module.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -I src/http -I src/http/modules -I src/http/v2
-o objs/src/http/modules/ngx_http_upstream_zone_module.o
src/http/modules/ngx_http_upstream_zone_module.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -I src/http -I src/http/modules -I src/http/v2
-o objs/src/http/modules/ngx_http_stub_status_module.o
src/http/modules/ngx_http_stub_status_module.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs
-o objs/ngx_modules.o
objs/ngx_modules.c
cc -o objs/nginx
objs/src/core/nginx.o
objs/src/core/ngx_log.o
objs/src/core/ngx_palloc.o
objs/src/core/ngx_array.o
objs/src/core/ngx_list.o
......此处省略......
objs/src/http/modules/ngx_http_upstream_least_conn_module.o
objs/src/http/modules/ngx_http_upstream_keepalive_module.o
objs/src/http/modules/ngx_http_upstream_zone_module.o
objs/src/http/modules/ngx_http_stub_status_module.o
objs/ngx_modules.o
-ldl -lpthread -lcrypt -lpcre -lssl -lcrypto -ldl -lz
-Wl,-E
sed -e "s|%%PREFIX%%|/usr/local/nginx-1.12.2|"
-e "s|%%PID_PATH%%|/usr/local/nginx-1.12.2/logs/nginx.pid|"
-e "s|%%CONF_PATH%%|/usr/local/nginx-1.12.2/conf/nginx.conf|"
-e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx-1.12.2/logs/error.log|"
< man/nginx.8 > objs/nginx.8
make[1]: Leaving directory `/usr/src/nginx-1.12.2'
make -f objs/Makefile install
make[1]: Entering directory `/usr/src/nginx-1.12.2'
test -d '/usr/local/nginx-1.12.2' || mkdir -p '/usr/local/nginx-1.12.2'
test -d '/usr/local/nginx-1.12.2/sbin'
|| mkdir -p '/usr/local/nginx-1.12.2/sbin'
test ! -f '/usr/local/nginx-1.12.2/sbin/nginx'
|| mv '/usr/local/nginx-1.12.2/sbin/nginx'
'/usr/local/nginx-1.12.2/sbin/nginx.old'
cp objs/nginx '/usr/local/nginx-1.12.2/sbin/nginx'
test -d '/usr/local/nginx-1.12.2/conf'
|| mkdir -p '/usr/local/nginx-1.12.2/conf'
cp conf/koi-win '/usr/local/nginx-1.12.2/conf'
cp conf/koi-utf '/usr/local/nginx-1.12.2/conf'
cp conf/win-utf '/usr/local/nginx-1.12.2/conf'
test -f '/usr/local/nginx-1.12.2/conf/mime.types'
|| cp conf/mime.types '/usr/local/nginx-1.12.2/conf'
cp conf/mime.types '/usr/local/nginx-1.12.2/conf/mime.types.default'
test -f '/usr/local/nginx-1.12.2/conf/fastcgi_params'
|| cp conf/fastcgi_params '/usr/local/nginx-1.12.2/conf'
cp conf/fastcgi_params
'/usr/local/nginx-1.12.2/conf/fastcgi_params.default'
test -f '/usr/local/nginx-1.12.2/conf/fastcgi.conf'
|| cp conf/fastcgi.conf '/usr/local/nginx-1.12.2/conf'
cp conf/fastcgi.conf '/usr/local/nginx-1.12.2/conf/fastcgi.conf.default'
test -f '/usr/local/nginx-1.12.2/conf/uwsgi_params'
|| cp conf/uwsgi_params '/usr/local/nginx-1.12.2/conf'
cp conf/uwsgi_params
'/usr/local/nginx-1.12.2/conf/uwsgi_params.default'
test -f '/usr/local/nginx-1.12.2/conf/scgi_params'
|| cp conf/scgi_params '/usr/local/nginx-1.12.2/conf'
cp conf/scgi_params
'/usr/local/nginx-1.12.2/conf/scgi_params.default'
test -f '/usr/local/nginx-1.12.2/conf/nginx.conf'
|| cp conf/nginx.conf '/usr/local/nginx-1.12.2/conf/nginx.conf'
cp conf/nginx.conf '/usr/local/nginx-1.12.2/conf/nginx.conf.default'
test -d '/usr/local/nginx-1.12.2/logs'
|| mkdir -p '/usr/local/nginx-1.12.2/logs'
test -d '/usr/local/nginx-1.12.2/logs'
|| mkdir -p '/usr/local/nginx-1.12.2/logs'
test -d '/usr/local/nginx-1.12.2/html'
|| cp -R html '/usr/local/nginx-1.12.2'
test -d '/usr/local/nginx-1.12.2/logs'
|| mkdir -p '/usr/local/nginx-1.12.2/logs'
make[1]: Leaving directory `/usr/src/nginx-1.12.2'
7、创建软链接
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx-1.12.2/ /usr/local/nginx
8、检查Nginx配置文件语法
[root@localhost nginx-1.12.2]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.12.2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.12.2/conf/nginx.conf test is successful
9、把Nginx服务添加进启动自定义文件内并启动Nginx软件
echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.local && /usr/local/nginx/sbin/nginx
10、检查端口情况判断Nginx软件是否运行正常
[root@localhost nginx-1.12.2]# ss -lnat|grep 80
LISTEN 0 128 *:80 *:*
Web访问验证
调整参数
1、当我们安装完Nginx程序后首先有几个参数需要调整下,需要调整的参数为user、worker_processes、worker_connections;Nginx主配置文件位于/usr/local/nginx/conf目录下的nginx.conf
[root@localhost conf]# vim nginx.conf
user www www; #指定运行Worker进程的用户和组,根据编译安装时指定的用户及组修改,默认为nobody;例如:user www www;
worker_processes auto; #常规调优选项 Worker线程的个数;通常应该为物理CPU核心个数减1;自动为auto
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 51200; #常规调优选项 设定单个Worker进程所能够处理响应的最大并发请求数(worker_connections * worker_processes);默认为1024,建议为51200,最大为65535。
}
2、调整参数后验证配置文件是否有误
[root@localhost conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.12.2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.12.2/conf/nginx.conf test is successful
3、重新加载配置文件或重启系统均可
[root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload
原文出处:ityww -> https://www.ityww.cn/1142.html