centos下安装nginx

Nginx只能处理80端口和25端口的负载均衡,Nginx只能做邮件和web服务的负载均衡

1、下载稳定版本的nginx. http://nginx.org/en/download.html

2、按照如下命令安装

#检查系统路径
[root@localhost usr]# pwd
/usr

#解压到当前路径
[root@localhost usr]# tar -zxv -f nginx-1.6.2.tar.gz 

#删除压缩包
[root@localhost usr]# rm -rf nginx-1.6.2.tar.gz 

#进入到解压包下
[root@localhost usr]# cd nginx-1.6.2/

[root@localhost nginx-1.6.2]# 

查看安装时的功能模块信息
[root@localhost nginx-1.6.2]# ./configure --help

#指定安装路径 [root@localhost nginx
-1.6.2]# ./configure --prefix=/usr/local/nginx #编译 [root@localhost nginx-1.6.2]# make #安装 [root@localhost nginx-1.6.2]# make install #回退到解压缩包上级目录 [root@localhost usr]# cd ../ #解除解压缩包 [root@localhost usr]# rm nginx-1.6.2 -rf

上面安装使用默认参数进行配置,如果要自定义安装模块和安装位置可以进行如下参数配置:

[root@localhost nginx-1.6.2]# ./configure 
> --prefix=/usr/local/nginx  
> --sbin-path=/usr/local/nginx/sbin/nginx 
> --conf-path=/usr/local/nginx/conf/nginx.conf 
> --http-log-path=/usr/local/nginx/logs/access.log 
> --error-log-path=/usr/local/nginx/logs/error.log 
> --pid-path=/usr/local/nginx/logs/nginx.pid 
> --lock-path=/usr/local/nginx/lock/nginx.lock 
> --http-client-body-temp-path=/usr/local/nginx/client_body_temp 
> --http-proxy-temp-path=/usr/local/nginx/proxy_temp 
> --http-fastcgi-temp-path=/usr/local/nginx/fastcgi-temp 
> --http-uwsgi-temp-path=/usr/local/nginx/uwsgi-temp 
> --http-scgi-temp-path=/usr/local/nginx/scgi-temp 
> --user=root 
> --group=root 
> --with-http_ssl_module 
> --with-http_flv_module 
> --with-http_mp4_module 
> --with-http_gzip_static_module 
> --with-http_stub_status_module

3、安装缺少包提示

错误提示 ./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.

解决方案

[root@localhost nginx-1.6.2]# yum -y install pcre-devel
错误提示 ./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.

解决方案
[root@localhost nginx-1.6.2]# yum install -y zlib-devel
错误提示:./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library.   You can either disable the module by using
--without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
--with-http_ssl_module --with-openssl=<path> options.

解决方案
[root@localhost nginx-1.6.2]# yum  -y install openssl openssl-devel

上面出现的问题是因为没有安装Nginx相应的编译工具,所以在安装时可以先执行如下命令进行安装

[root@localhost ~]# yum -y install gcc gcc-c++ autoconf automake

[root@localhost ~]# yum -y install zlib zlib-devel openssl openssl-devel pcre-devel
#说明:
#zlib:Nginx提供gzip模块,需要zlib库支持
#openssl:Nginx提供ssl功能
#pcre:支持抵制重写rewrite功能

4、修改防火墙配置:

#修改防火墙配置:
[root@localhost nginx]# vi + /etc/sysconfig/iptables

#添加配置项

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

#重启防火墙
[root@localhost nginx]# service iptables restart

5、启动nginx

#方法1
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 

#方法2
[root@localhost nginx]# cd /usr/local/nginx/sbin [root@admin sbin]# ./nginx

6、监察Nginx配置文件语法

[root@localhost sbin]# ./nginx -t -c /usr/local/nginx/conf/nginx.conf
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

7、查看nginx是否启动

[root@localhost nginx]# netstat -ntlp

或者

#测试端口
[root@localhost nginx]# netstat –na|grep 80

#浏览器中测试
http://ip:80

 附录:

#查询nginx主进程号
[root@localhost sbin]# ps -ef | grep nginx

#停止进程
[root@localhost sbin]# kill -QUIT 主进程号

#快速停止
[root@localhost sbin]# kill -TERM 主进程号

#强制停止
[root@localhost sbin]# pkill -9 nginx

Nginx服务脚本

[root@localhost init.d]# service nginx 
Usage: /etc/init.d/nginx {start|stop|reload|restart|configtest}
[root@localhost init.d]# cat nginx 
#!/bin/bash
#
# chkconfig: - 85 15
# description: nginx is a World Wide Web server. It is used to serve
# Source Function Library
. /etc/init.d/functions

# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"

RETVAL=0
prog="Nginx"

start() {
        echo -n $"Starting $prog: "
        mkdir -p /dev/shm/nginx_temp
        daemon $NGINX_SBIN -c $NGINX_CONF
        RETVAL=$?
        echo
        return $RETVAL
}

stop() {
        echo -n $"Stopping $prog: "
        killproc -p $NGINX_PID $NGINX_SBIN -TERM
        rm -rf /dev/shm/nginx_temp
        RETVAL=$?
        echo
        return $RETVAL
}

reload(){
        echo -n $"Reloading $prog: "
        killproc -p $NGINX_PID $NGINX_SBIN -HUP
        RETVAL=$?
        echo
        return $RETVAL
}

restart(){
        stop
        start
}

configtest(){
    $NGINX_SBIN -c $NGINX_CONF -t
    return 0
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  reload)
        reload
        ;;
  restart)
        restart
        ;;
  configtest)
        configtest
        ;;
  *)
        echo $"Usage: $0 {start|stop|reload|restart|configtest}"
        RETVAL=1
esac

exit $RETVAL
View Code

转载请注明出处:[http://www.cnblogs.com/zhoulf/archive/2013/02/09/2909653.html]

原文地址:https://www.cnblogs.com/dennisit/p/4069521.html