###环境:CentOS Linux release 7.5.1804 (Core) 新系统
0.首先使用wget命令下载nginx的源码包,并解压
[root@localhost ~]# wget http://nginx.org/download/nginx-1.15.5.tar.gz
--2018-11-06 16:59:08-- http://nginx.org/download/nginx-1.15.5.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: 1024791 (1001K) [application/octet-stream]
Saving to: ‘nginx-1.15.5.tar.gz’
100%[======================================================================================>] 1,024,791 245KB/s in 4.1s
2018-11-06 16:59:13 (245 KB/s) - ‘nginx-1.15.5.tar.gz’ saved [1024791/1024791]
[root@localhost ~]# tar -xf nginx-1.15.5.tar.gz
[root@localhost ~]# ls
nginx-1.15.5 nginx-1.15.5.tar.gz
[root@localhost ~]# cd nginx-1.15.5/
[root@localhost nginx-1.15.5]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
1.为了安全添加nginx用户和组,以便以后以nginx用户的身份运行nginx程序,而非root
[root@localhost nginx-1.15.5]# useradd nginx -s /sbin/nologin
2.开始执行configure脚本,进行编译前的配置 #configure配置脚本有很多的参数,这里就只列出几个常用的:–prefix:安装目录 –user -group. 获取更多配置使用 ./configure –help命令
[root@localhost nginx-1.15.5]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_ssl_module --with-stream
checking for OS
+ Linux 3.10.0-862.el7.x86_64 x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found
###提示C编译器未找到。我们安装gcc编译器重新尝试
[root@localhost nginx-1.15.5]# yum -y install gcc
[root@localhost nginx-1.15.5]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_ssl_module --with-stream
checking for PCRE library in /usr/local/ ... not found
checking for PCRE library in /usr/include/pcre/ ... not found
checking for PCRE library in /usr/pkg/ ... not found
checking for PCRE library in /opt/local/ ... not found
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
###此时又提示HTTP重定向模块需要PCRE库,你可以使用--without-http_rewirte_module命令把这个默认安装的模块屏蔽掉,或者安装PCRE库文件,或者创建PCRE库使用--with-pcre选项来指定位置,这里我们选择安装库文件
[root@localhost nginx-1.15.5]# yum -y install pcre-devel
[root@localhost nginx-1.15.5]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_ssl_module --with-stream
checking for OpenSSL library ... not found
checking for OpenSSL library in /usr/local/ ... not found
checking for OpenSSL library in /usr/pkg/ ... not found
checking for OpenSSL library in /opt/local/ ... not found
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.
###又提示OpenSSL没有,没办法编译就是这么麻烦,继续装包吧
[root@localhost nginx-1.15.5]# yum -y install openssl-devel
[root@localhost nginx-1.15.5]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_ssl_module --with-stream
Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/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"
###此时配置成功,出现了配置信息
3.进行编译并复制文件到相应目录
[root@localhost nginx-1.15.5]# make && make install
###此时会出现一堆信息,无视就好,warning和error除外!
[root@localhost ~]# ls /usr/local/nginx/
conf html logs sbin
###nginx安装完成!!!
4.启动nginx进行访问测试
[root@localhost nginx]# ./sbin/nginx
[root@localhost nginx]# netstat -nutlp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 44202/nginx: master
###查看到nginx进程正在监听80端口即为成功,下面进行curl访问测试,web页面也可以,更直观
[root@localhost nginx]# curl http://127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
###看到welcome to nginx!即为成功。