Nginx 是一个高性能的 HTTP 和 反向代理服务器,也是一个 IMAP / POP3 / SMTP 服务器。
本文简述在 Linux 环境下的安装 Nginx 的方法.
CentOS 7.0 + Nginx 1.8.0
1. 下载、解压
先去 Nginx 官网 看一下有什么版本, 下载… 解压…
1 2
| wget -c https://nginx.org/download/nginx-1.18.0.tar.gz tar -zxvf nginx-1.18.0.tar.gz
|
Linux 下的 nginx 并非直接解压即可使用, 还需要 安装依赖 + 配置 + 编译 …
2. 安装编译环境及依赖
2.1 安装 gcc 环境
GCC(GNU Compiler Collection,GNU编译器套件)是由GNU开发的编程语言译器。若系统已安装了 gcc 环境则跳过此步骤。
2.2 安装 PCRE 依赖库
PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。
1
| yum install -y pcre pcre-devel
|
2.3 安装 zlib 依赖库
zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip。
1
| yum install -y zlib zlib-devel
|
2.4 安装 OpenSSL 库
若需要 Nginx 支持 https 则需安装 OpenSSL 库
1
| yum install -y openssl openssl-devel
|
2.5 编译配置
有几个常用的配置, 如果需要, 可在此步骤加上. 例如:
1
| ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream
|
--prefix=/usr/local/nginx : Nginx 安装路径, 默认配置即是 /usr/local/nginx
--with-http_stub_status_module : 监控模块
--with-http_ssl_module : https 支持
--with-stream : stream模块
3. 编译并安装
whereis nginx 命令可以查看 nginx 被安装在什么地方. 使用默认配置时应安装在 /usr/local/nginx/
若已经安装好了 Nginx, 需要更改配置重新编译, 则在原 Nginx 解压目录重新执行配置 ( 见2.5节 ), 然后执行 make, 即会在objs目录下生成新的 nginx 文件, 将其拷贝到*/usr/local/nginx/sbin*覆盖即可 ( 覆盖前停止 Nginx, 不要执行 make install, 否则将会覆盖原先已配置好的 Nginx, 切记~ 切记 ~ ).
4. 启动 / 停止
1 2 3 4 5 6 7 8 9 10 11
| cd /usr/local/nginx/sbin/ ./nginx
./nginx -s stop
./nginx -s quit
./nginx -s reload
|
启动后使用浏览器打开 http://localhost 应看到 “Welcome to nginx !” 即说明一切 OK~
5. 修改端口
Nginx 默认监听 80 端口, 若要修改可编辑 conf 下的 nginx.conf 文件, 保存后重启 Nginx.
1 2 3 4 5 6 7 8
| cd /usr/local/nginx/conf cp nginx.conf nginx.conf.back vi nginx.conf
cd /usr/local/nginx/sbin ./nginx -s reload ps aux|grep nginx
|
6. 配置开机自动启动
编辑(创建)/etc/init.d/nginx文件:
在其中添加如下代码 ( 注意 22, 25 行处的路径, 以及第11行路径应与nginx.conf中配置一致 ):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| #!/bin/sh
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx" prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() { user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` if [ -n "$user" ]; then if [ -z "`grep $user /etc/passwd`" ]; then useradd -M -s /bin/nologin $user fi options=`$nginx -V 2>&1 | grep 'configure arguments:'` for opt in $options; do if [ `echo $opt | grep '.*-temp-path'` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then mkdir -p $value && chown -R $user $value fi fi done fi }
start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval }
stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval }
restart() { configtest || return $? stop sleep 1 start }
reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $prog -HUP retval=$? echo }
force_reload() { restart }
configtest() { $nginx -t -c $NGINX_CONF_FILE }
rh_status() { status $prog }
rh_status_q() { rh_status >/dev/null 2>&1 }
case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac
|
开启服务:
1 2 3
| chmod a+x /etc/init.d/nginx chkconfig --add /etc/init.d/nginx chkconfig nginx on
|
搞定, reboot试试…
完成上面的配置后, 还可以如下这样操作:
1 2 3 4
| service nginx status service nginx start service nginx stop service nginx restart
|
若修改了*/etc/init.d/nginx*记得systemctl daemon-reload