一:官方下载nginx源包
官方下载地址:http://nginx.org/en/download.html
下载最新版本:目前最新版本是 nginx 1.11.4 下载地址是:http://nginx.org/download/nginx-1.11.4.tar.gz
可以先点击CHANGES 查看一下改动,查看一些新版本的新特性,寻找最适合自己的nginx版本
二:编译安装
2.1:解压缩
tar zxvf nginx-1.11.4.tar.gz
2.2:编译安装
2.2.1 编译
编译选项官方提供的有:
--prefix=path 定义一个目录来保存你的nginx的提供功能的文件夹,就这好比我们安装软件的时候软件存放的目录,如果我们在编译的不指定安装位置,那么默认的位置/usr/local/nginx 目录
--sbin-path=path 设置nginx执行脚本的位置,这里如果设置在path变量里面,就可以在bash环境下,任意使用nginx命令,默认位置prefix/sbin/nginx 注意这里的prefix是
在配置文件里面配置的路径
--conf-path=path 配置nginx配置文件的路径,如果不指定这个选项,那么配置文件的默认路径就会是 prefix/conf/nginx.conf
--pid-path =path 配置nginx.pid file的路径,一般来说,进程在运行的时候的时候有一个进程id,这个id会保存在pid file里面,默认的pid file的放置位置是prefix/logs/nginx.pid
--error-log-path=path 设置错误日志的存放路径,如果不指定,就默认 prefix/logs/error.log
--http-log-path= path 设置http访问日志的路径,如果不指定,就默认 prefix/logs/access.log
--user=name 设置默认启动进程的用户,如果不指定,就默认 nobody
--group=name 设置这个用户所在的用户组,如果不指定,依然是nobody
这些是我们常用的编译选项,其他的可以均保持默认,如需特殊指定,可上nginx官网查阅 http://nginx.org/en/docs/configure.html
下面是一些不常用的选项
--with-http_ssl_module -开启HTTP SSL模块,使NGINX可以支持HTTPS请求。需要安装了OPENSSL
--with-http_flv_module
--with-http_stub_status_module - 启用 "server status" 页(可有可无)
--without-http_gzip_module - 禁用 ngx_http_gzip_module. 如果启用,需要 zlib 。
--without-http_ssi_module - 禁用 ngx_http_ssi_module
--without-http_referer_module - 禁用 ngx_http_referer_module
--without-http_rewrite_module - 禁用 ngx_http_rewrite_module. 如果启用需要 PCRE 。
--without-http_proxy_module - 禁用 ngx_http_proxy_module
--without-http_fastcgi_module - 禁用 ngx_http_fastcgi_module
--without-http_memcached_module - 禁用 ngx_http_memcached_module
--without-http_browser_module - 禁用 ngx_http_browser_module
--http-proxy-temp-path=PATH - Set path to the http proxy temporary files
--http-fastcgi-temp-path=PATH - Set path to the http fastcgi temporary files
--without-http - 禁用 HTTP server(用作代理或反向代理)
--with-mail - 启用 IMAP4/POP3/SMTP 代理模块
--with-mail_ssl_module - 启用 ngx_mail_ssl_module
--with-openssl=DIR - Set path to OpenSSL library sources
编译安装步骤如下:
①:sudo ./configure --prefix=/usr --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock
②:make
③:sudo make install
如果编译过程出现the HTTP rewrite module requires the PCRE library.的错误
安装
sudo apt install libpcre3-dev
ssl扩展模块出现错误,就安装
sudo apt install libssl-dev
2.3:使用sysv-rc-conf(chkconfig)的方式管理nginx服务
2.3.1首先添加nginx服务管理的脚本
1 #!/bin/sh
2 #
3 # nginx Start up the nginx server daemon
4 #
5 # chkconfig: 2345 55 25
6 # Description: starts and stops the nginx web server
7 #
8 ### BEGIN INIT INFO
9 # Provides: nginx
10 # Required-Start: $all
11 # Required-Stop: $all
12 # Default-Start: 2 3 4 5
13 # Default-Stop: 0 1 6
14 # Description: starts and stops the nginx web server
15 ### END INIT INFO
16 # To install:
17 # copy this file to /etc/init.d/nginx
18 # shell> chkconfig --add nginx (RedHat)
19 # shell> update-rc.d -f nginx defaults (debian)
20 # To uninstall:
21 # shell> chkconfig --del nginx (RedHat)
22 # shell> update-rc.d -f nginx remove
23 PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
24 NAME=nginx
25 DAEMON=/usr/sbin/$NAME
26 LOCKFILE=/var/lock/subsys/nginx
27 CONFIGFILE=/etc/nginx/$NAME.conf
28 PIDFILE=/var/run/${NAME}.pid
29 ULIMIT=10240
30 set -e
31 [ -x "$DAEMON" ] || exit 0
32
33 do_start() {
34 echo "Starting $NAME ..."
35 ulimit -SHn $ULIMIT
36
37 $DAEMON -c $CONFIGFILE
38 }
39
40 do_stop() {
41 echo "Shutting down $NAME ..."
42 $DAEMON -s stop
43 }
44
45 do_reload() {
46 echo "Reloading $NAME ..."
47 $DAEMON -s reload
48 }
49
50 case "$1" in
51 start)
52 [ ! -f "$PIDFILE" ] && do_start || echo "nginx already running"
53 echo -e ".\ndone"
54 ;;
55
56 stop)
57 do_stop || echo "nginx not running"
58 echo -e ".\ndone"
59 ;;
60
61 reload)
62 do_reload || echo "nginx not running"
63 echo -e ".\ndone"
64 ;;
65
66 *)
67 N=/etc/init.d/$NAME
68 echo "Usage: $N {start|stop|restart|reload}" >&2
69 exit 1
70 ;;
71
72 esac
73
74 exit 0
2.3.2:给脚本添加执行权限
sudo chmod +x /etc/init.d/nginx
2.3.3:添加到开机自启动
sudo sysv-rc-conf nginx on(这一步实际上就是实现了一个链接)
2.3.4:加载安装服务
sudo systemctl enable nginx(如果不愿意重启的话)
2.3.5:启动nginx服务
sudo service nginx start
2.4:测试nginx
curl localhost
来源:https://www.cnblogs.com/dtiove/p/5924385.html