文章转载:http://blog.csdn.net/tao_627/article/details/45249267
根据工作需要,现在需要安装nginx服务器,本来可以直接安装别人制作好的rpm包的,但是本着爱折腾和时刻尝鲜的精神,我决定从官网下载最新的nginx源码来安装,下面记录了我的安装过程。
下面的安装假定是以root用户登录并执行
1.安装依赖库
这些依赖库主要有g++、gcc、openssl-devel、pcre-devel和zlib-devel
yum -y install make gcc gcc-c++ glibc glibc-devel lsof
yum -y install pcre pcre-devel
yum -y install zlib zlib-devel
yum -y install openssl openssl--devel
2.下载源码包
cd /usr/local/src
wget -d "http://nginx.org/download/nginx-1.8.0.tar.gz"
tar zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_realip_module --with-http_ssl_module
make -j 4
make install
3.验证性启动nginx
注意,在没有设置nginx为系统服务之前,这里仅是验证性的启动测试。使用下面的命令查看nginx可执行程序的路径
whereis nginx
一般默认在/usr/local/nginx目录下面
ulimit -SHn 65535
/usr/local/nginx/sbin/nginx
4.添加nginx为系统服务
添加如下文件
vim /etc/init.d/nginx
[html] view plain copy
- #!/bin/sh
- #
- # nginx - this script starts and stops the nginx daemon
- #
- # chkconfig: - 85 15
- # description: Nginx is an HTTP(S) server, HTTP(S) reverse \
- # proxy and IMAP/POP3 proxy server
- # processname: nginx
- # config: /etc/nginx/nginx.conf
- # config: /etc/sysconfig/nginx
- # pidfile: /var/run/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"
- [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
- 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
- killall -9 nginx
- }
- restart() {
- configtest || return $?
- stop
- sleep 1
- 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
chmod +x /etc/init.d/nginx
注册 nginx 服务
chkconfig --add nginx
chkconfig --list nginx
假如查看到nginx服务在各种级别下,都没有打开,可以再次使用下面的命令明确指出在不同级别下打开:
chkconfig --level 2345 nginx on
再次采用
chkconfig --list nginx
查看,是否在上述各级别下已经打开。这里之所以要如此强调,是希望在断电后nginx服务能自动重启。
4.修改配置文件nginx.conf
主要是根据业务逻辑来配置nginx.conf,这里忽略具体内容
配置完成后,重新测试并加载nginx.conf
service nginx configtest
service nginx reload
让nginx在新的配置下运行就可以了
5.安装中出现的问题和错误
因为nginx.conf中出现问题,显示错误如下:
nginx: [emerg] unknown directive "lua_shared_dict" in /usr/local/nginx/conf/nginx.conf:11
nginx: [emerg] unknown directive "rewrite_by_lua_file" in /usr/local/nginx/conf/nginx.conf:136
解决方法
这两个报错,是因为nginx.conf中添加了这两个指令,将对应配置及逻辑删除就可以了。
参考链接
https://www.centos.bz/2012/12/openresty-nginx-block-cc-attack-deploy/
我暂时将它们注释掉