返回列表

转载:CentOS-6.3安装配置Nginx

默认分类 2014/10/21 02:46

原文地址:http://www.cnblogs.com/zhoulf/

安装说明

系统环境:CentOS-6.3

软件:nginx-1.2.6.tar.gz
安装方式:源码编译安装 
安装位置:/usr/local/nginx 
下载地址:http://nginx.org/en/download.html

安装前提

在安装nginx前,需要确保系统安装了g++、gcc、openssl-devel、pcre-devel和zlib-devel软件。安装必须软件:

[root@admin /]#yum install gcc-c++
yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel </td>

检查系统安装的Nginx:

[root@admin    local]# find -name nginx
./nginx
./nginx/sbin/nginx
./nginx-1.2.6/objs/nginx </td>

卸载原有的Nginx

[root@admin    /]# yum    remove nginx </td>

安装

将安装包文件上传到/usr/local中执行以下操作:

[root@admin    local]# cd /usr/local
[root@admin    local]# tar -zxv -f nginx-1.2.6.tar.gz
[root@admin    local]# rm -rf nginx-1.2.6.tar.gz
[root@admin    local]# mv nginx-1.2.6 nginx
[root@admin    local]# cd /usr/local/nginx
[root@admin    nginx]# ./configure --prefix=/usr/local/nginx
[root@admin    nginx]# make
[root@admin    nginx]# make install </td>

配置

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

#添加配置项 
-A INPUT -m state --state NEW -m    tcp -p tcp --dport 80 -j ACCEPT

#重启防火墙 
[root@admin nginx-1.2.6]# service    iptables restart </td>

启动

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

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

停止

#查询nginx主进程号 
ps -ef | grep nginx

#停止进程 
kill -QUIT 主进程号 

#快速停止 
kill -TERM 主进程号 

#强制停止 
pkill -9 nginx

重启

[root@admin local]#    /usr/local/nginx/sbin/nginx -s reload

测试

#测试端口 
netstat –na|grep 80

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

nginx.conf 完整配置文件:

user  root;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remoteuser [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;
    gzip_types text/plain application/javascript text/css text/javascript application/json;

    server {
        listen       80;
        server_name  localhost;

        charset utf-8;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            #root /root/web/fe_api/public; 
            index  index.html index.htm;
        }

        location ~ \.(jpg|png|gif|jpeg)$ {
            expires 30d;
        }
        location ~ \.(js|css)?$ {
            expires 1h;
        }

        location ^~ /test/ {
            expires 30d;
            alias /root/web/fe_api/public/;
        }

        location ^~ /ue_api/ {
            #root C:\campus\dashboard\mockup;
            #alias /mockup;
            proxy_pass   http://127.0.0.1:3000/ue_api/;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}