Skip to content

Instantly share code, notes, and snippets.

@shangdev
Last active November 17, 2017 06:30
Show Gist options
  • Save shangdev/c053fb09678e50073001905a7f3200e1 to your computer and use it in GitHub Desktop.
Save shangdev/c053fb09678e50073001905a7f3200e1 to your computer and use it in GitHub Desktop.
Nginx install and Add-module in centos
############################
## 安装Nginx
############################
首先安装必要的库(nginx 中gzip模块需要 zlib 库,rewrite模块需要 pcre 库,ssl 功能需要openssl库)。选定/usr/local为安装目录,以下具体版本号根据实际改变。
1.安装PCRE库
$ cd /usr/local/
$ wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz
$ tar -zxvf pcre-8.38.tar.gz
$ cd pcre-8.38
$ ./configure
$ make
$ make install
2.安装zlib库
$ cd /usr/local/
$ wget http://zlib.net/zlib-1.2.11.tar.gz
$ tar -zxvf zlib-1.2.11.tar.gz
$ cd zlib-1.2.11
$ ./configure
$ make
$ make install
3.安装ssl
$ cd /usr/local/
$ wget http://www.openssl.org/source/openssl-1.0.1j.tar.gz
$ tar -zxvf openssl-1.0.1j.tar.gz
$ cd openssl-1.0.1j
$ ./config
$ make
$ make install
4.安装ngx_cache_purge
$ wget -c http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
$ tar zxvf ngx_cache_purge-2.3.tar.gz
4.安装nginx
$ cd /usr/local/
$ wget http://nginx.org/download/nginx-1.9.9.tar.gz
$ tar -zxvf nginx-1.9.9.tar.gz
$ cd nginx-1.9.9
$ ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_v2_module --with-ipv6 --with-http_sub_module --with-openssl=/usr/local/openssl-1.0.1j --with-pcre=/usr/local/pcre-8.38 --with-zlib=/usr/local/zlib-1.2.11
$ --add-module=/usr/local/ngx_cache_purge-2.3
$ make
$ make install
5.查看版本
/usr/local/nginx/sbin/nginx -V
6.把Nginx加入service服务:
$ vi /ect/init.d/nginx
内容:
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
#
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
$nginxd -s reload
#if your nginx version is below 0.8, please use this command: "kill -HUP `cat ${nginx_pid}`"
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
############################
## Nginx 可能的错误
############################
1: nginx: [emerg] getpwnam(“www”) failed (系统中没有www这个用户)
解决方案:
$ /usr/sbin/groupadd -f www
$ /usr/sbin/useradd -g www www
############################
## 添加Nginx第三方模块
############################
1.查看Nginx configure:
/usr/local/nginx/sbin/nginx -V
2.重新编译Nginx
$ ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_v2_module --with-ipv6 --with-http_sub_module --with-openssl=/usr/local/openssl-1.0.2j --with-pcre=/usr/local/pcre-8.38 --with-zlib=/usr/local/zlib-1.2.11
$ --add-module=/usr/local/ngx_cache_purge-2.3
make
3.复制编译好的./objs/nginx替换掉/usr/local/nginx/sbin/下的nginx
service nginx stop
cp ./objs/nginx /usr/local/nginx/sbin/
service nginx start
4.查看结果:
/usr/local/nginx/sbin/nginx -V
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment