CentOS 安装Nginx
准备工作,需要先下载pcre库,因为nginx的rewrite模块需要pcre库
这里使用的版本分别为:
pcre下载地址: ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/, 或直接下载最新 pcre-8.34.tar.gz
nginx下载地址:http://nginx.org/en/download.html,或直接下载最新 nginx-1.4.7.tar.gz
copy压缩包至linux的相应目录,例如:opt下的software,需要确认当前登录用户有权限进行解压和安装。
0)安装zlib库:
官方下载: http://www.zlib.net, 最新版为 zlib-1.2.8.tar.gz
tar zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure --prefix=/opt/zlib-1.2.8/
make && make install
1)安装pcre库:
tar zxvf pcre-8.34.tar.gz
cd pcre-8.34
./configure # 默认安装(推荐)
./configure --prefix=/opt/pcre-8.34/
make && make install
在这里可能会遇到出错,显示configure: error: newly created file is older than distributed files!
同步更新一下当前的系统时间即可,操作:
ntpdate 210.72.145.22
或
ntpdate 0.centos.pool.ntp.org
然后进行安装
make && make install
cd ../
2)安装Nginx:
tar zxvf nginx-1.4.7.tar.gz
cd nginx-1.4.7
在这里需要对nginx的源码做一下小的处理,默认nginx是不支持静态文件的POST提交。一般浏览器默认的设置是缓存静态资源的,而有时候却需要对静态文件进行更新,这就需要使用post提交了,而此时nginx却返回405,一般处理方法是在配置的时候这样写:
error_page 405 =200 @405;
location @405
{
root /opt/htdocs;
}
重定向了405->200了,并且给405这个错误指定了doc_root,就是正常的doc_root的配置。
有兴趣可以参考这里:Nginx的405错误(已解决)
也可以对源码进行一些小的改动,使用vim或是copy下来修改都可以。
这里copy下来进行修改的,文件是src/http/modules/ngx_http_static_module.c
[root@homeros nginx-1.4.7]# vim src/http/modules/ngx_http_static_module.c
找到下图中的那一行,并将其注释掉:
大致意思是静态资源请求的处理方法中,如果发现请求方法为post提交则拒绝
接下来就是安装了
./configure # 默认安装(推荐)
./configure --with-pcre=/opt/pcre-8.34/ --with-zlib=/opt/zlib-1.2.8/
./configure --with-pcre=../pcre-8.34/ --with-zlib=/opt/zlib-1.2.8/ # 指定 pcre 源目录
make && make install
默认安装提示:
Configuration summary + using PCRE library: /opt/pcre-8.34/ + OpenSSL library is not used + using builtin md5 code + sha1 library is not found + using zlib library: /opt/zlib-1.2.8/ nginx path prefix: "/usr/local/nginx" nginx binary file: "/usr/local/nginx/sbin/nginx" 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"
nginx默认被安装在/usr/local/nginx,其二进制文件和配置文件分别在 /usr/local/nginx/sbin/nginx 和 /usr/local/nginx/conf/nginx.conf
安装完 nginx 后,需要验证安装是否成功:
1) 启动 nginx 服务:
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
打开浏览器,输入: http://localhost:80 ,启动成功会显示如下页面:
2) 测试 nginx 配置文件:
/usr/local/nginx/sbin/nginx -t
3)开机自启动nginx
这里使用的是编写shell脚本的方式来处理
方法1: 最简单的实现 start 和 stop
#!/bin/bash
# Startup script for the nginx Web Server
# description: nginx is a World Wide Web server. It is used to serve
# HTML files and CGI.
# processname: nginx
# pidfile: /usr/local/nginx/logs/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
NGINX_HOME=/usr/local/nginx/sbin
NGINX_CONF=/usr/local/nginx/conf
if [ ! -f "$NGINX_HOME/nginx" ]
then
echo "nginxserver startup: cannot start"
exit
fi
case "$1" in
'start')
$NGINX_HOME/nginx -c $NGINX_CONF/nginx.conf
echo "nginx start successful"
;;
'stop')
killall -TERM nginx
;;
esac
方法2: 功能完善的 nginx 启动脚本(推荐)
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# 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/logs/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 /usr/local/nginx/logs/nginx.pid
}
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
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
方法3:自己研究脚本,且行且珍惜
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# bin /usr/local/nginx/sbin/nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
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
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -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
:wq 保存并退出
设置文件的访问权限
chmod a+x /etc/init.d/nginx (a+x ==> all user can execute 所有用户可执行)
这样在控制台就很容易的操作nginx了,例如查看Nginx当前状态、启动Nginx、停止Nginx、重启Nginx,如下图:
开机自启动方法
方法1: 同样的修改了nginx的配置文件nginx.conf,也可以使用上面的命令重新加载新的配置文件并运行,可以将此命令加入到rc.local文件中,这样开机的时候nginx就默认启动了
vi /etc/rc.local
加入一行 /etc/init.d/nginx start 保存并退出,下次重启会生效。
方法2: 开机自启动更好的办法是添加到 chkconfig 配置里。
nginx 添加进 chkconfig,步骤如下:
1) 由于 /usr/local/nginx/sbin/nginx 并不在默认的环境变量里,因此需要手动添加PATH 或 软链接到默认环境变量里
系统默认环境变量: echo $PATH
/opt/hadoop-2.2.0/sbin:/opt/hadoop-2.2.0/bin:/opt/zookeeper-3.4.5/bin:/opt/jdk1.6.0_22/bin:/opt/jdk1.6.0_22/jre/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/homeros/bin
此处建议把 /usr/local/nginx/sbin/nginx 软链接添加进环境变量:
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
2) 软链接到系统环境变量后,然后才可以 chkconfig 添加到开机自启动:
chkconfig nginx on
输入命令 chkconfig 查看添加结果:
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
nfs 0:off 1:off 2:off 3:off 4:off 5:off 6:off
nfslock 0:off 1:off 2:off 3:on 4:on 5:on 6:off
nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
ntpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
ntpdate 0:off 1:off 2:off 3:off 4:off 5:off 6:off
Nginix 安装问题分析:
错误1: 提示没有安装 PCRE ,错误信息如下:
./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.
解决方法: 按照步骤1,先安装pcre库,然后根据提示加上参数:
错误2: 提示没有安装 zlib 库,错误信息如下:
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
解决方法: 去 zlib 官方下载最新 zlib-1.2.8.tar.gz,安装步骤如下:
tar xvf zlib-1.2.8.tar.xz
cd zlib-1.2.8
./configure --prefix=/opt/zlib-1.2.8
make && make install
错误3:启动 nginx 服务时,提示 libpcre.so.1 找不到,具体错误信息如下:
[root@homeros nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf # 启动 nginx 服务
/usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
解决方法:
1) 查看那个lib缺少: ldd $(which /usr/local/nginx/sbin/nginx)
[root@homeros nginx]# ldd $(which /usr/local/nginx/sbin/nginx)
linux-vdso.so.1 => (0x00007fff9b2b0000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003574200000)
libcrypt.so.1 => /lib64/libcrypt.so.1 (0x000000357ee00000)
libpcre.so.1 => not found
libc.so.6 => /lib64/libc.so.6 (0x0000003573a00000)
/lib64/ld-linux-x86-64.so.2 (0x0000003573600000)
libfreebl3.so => /lib64/libfreebl3.so (0x000000357f200000)
libdl.so.2 => /lib64/libdl.so.2 (0x0000003573e00000)
2) 发现在 /lib64/ 库(注:32位系统为/lib/)下缺少 libpcre.so.1 因此查询 libpcre.so.1 在哪儿,手动软链接上
[root@homeros nginx]# find / -name libpcre.so.1 # 全局搜索 libpcre.so.1
/home/homeros/Downloads/tool-server/nginx/pcre-8.34/.libs/libpcre.so.1
/usr/local/lib/libpcre.so.1
/opt/pcre-8.34/lib/libpcre.so.1
[root@homeros nginx]# ln -s /usr/local/lib/libpcre.so.1 /lib64/libpcre.so.1 # 软链接到 /lib64/libpcre.so.1
[root@homeros nginx]# ll /lib64/libpcre.so.1 # 验证是否软链接上
lrwxrwxrwx. 1 root root 27 Apr 2 17:03 /lib64/libpcre.so.1 -> /usr/local/lib/libpcre.so.1
3) 然后再启动 nginx ,成功!
[root@homeros nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf # 重启 nginx
[root@homeros nginx]# ps -ef | grep nginx # 验证重启 nginx 成功
root 17591 1 0 17:07 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 17592 17591 0 17:07 ? 00:00:00 nginx: worker process
root 17596 3294 0 17:07 pts/0 00:00:00 grep nginx
参考推荐:
linux(centos)上配置nginx、mysql、php-fpm、redis开机启动
LNMP(CentOS+Nginx+Mysql+PHP)服务器环境配置
版权所有: 本文系米扑博客原创、转载、摘录,或修订后发表,最后更新于 2017-09-06 23:53:02
侵权处理: 本个人博客,不盈利,若侵犯了您的作品权,请联系博主删除,莫恶意,索钱财,感谢!
转载注明: CentOS 安装Nginx (米扑博客)

