1. 程式人生 > >阿里雲伺服器安裝 nginx

阿里雲伺服器安裝 nginx

首先我們要先下載nginx的安裝包

  http://nginx.org/en/download.html


我這裡下載的是nginx - 1.8.1

然後是去安裝依賴的庫

命令列
 yum -y install make gcc gcc-c++ glibc glibc-devel lsof   pcre pcre-devel  zlib zlib-devel  openssl openssl--devel
這是很全,主要看你有沒有安裝過
yum install pcre-devel pcre  zlib-devel zlib openssl-devel -y
這個是簡化版少了幾個安裝的庫到這一步準備工作算是做完了

解壓


    # cd /usr/local/src
    # tar zxvf nginx-1.8.1.tar.gz

 進入目錄
cd /usr/local/src/nginx-1.8.1

 1、 配置。通常將軟體安裝在/usr/local/目錄下。
    ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module
./configure 
2、 編譯
make
3、   安裝
make install

  順序不能錯。 

 檢查是否安裝成功

    cd  /usr/local/nginx/sbin
    ./nginx -t 


    結果顯示:
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successfu

這裡安裝就算是告一段落了。

啟動方法:

#方法1
        # /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
        #方法2
        # cd /usr/local/nginx/sbin
        

ps:/usr/local/nginx/conf/nginx.conf 這個 Nginx.conf的配置檔案存放路徑。

重啟方法

 重啟(首次啟動需:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf)
  其後只用      /usr/local/nginx/sbin/nginx -s reload  就可了

這裡我啟動時還碰到了一個問題
報如下錯誤資訊:

nginx: [emerg] getpwnam("www") failed

修改預設的nginx.conf 檔案內容 

user  nobody;
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 - $remote_user [$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;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        #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;
    #    }
    #}

}

其實就是將  #user  nobody;  改為  user  nobody;