centos 6.4 server 安裝nginx

來源:互聯網
上載者:User
1.環境準備yum -y install gcc gcc-c++ autoconf automake makeyum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel  yum install nginx 2.下載 nginx wget   http://nginx.org/download/nginx-1.2.1.tar.gz   tar –xzvf nginx-1.2.1.tar.gzcd nginx-1.0.2./configure--sbin-path=/root/soft/nginx configure 支援下面的選項: 

--prefix=<path> - Nginx安裝路徑。如果沒有指定,預設為 /usr/local/nginx。 

--sbin-path=<path> - Nginx可執行檔安裝路徑。只能安裝時指定,如果沒有指定,預設為<prefix>/sbin/nginx。 

--conf-path=<path> - 在沒有給定-c選項下預設的nginx.conf的路徑。如果沒有指定,預設為<prefix>/conf/nginx.conf。 

--pid-path=<path> - 在nginx.conf中沒有指定pid指令的情況下,預設的nginx.pid的路徑。如果沒有指定,預設為 <prefix>/logs/nginx.pid。 

--lock-path=<path> - nginx.lock檔案的路徑。 

--error-log-path=<path> - 在nginx.conf中沒有指定error_log指令的情況下,預設的錯誤記錄檔的路徑。如果沒有指定,預設為 <prefix>/logs/error.log。 

--http-log-path=<path> - 在nginx.conf中沒有指定access_log指令的情況下,預設的訪問日誌的路徑。如果沒有指定,預設為 <prefix>/logs/access.log。 

--user=<user> - 在nginx.conf中沒有指定user指令的情況下,預設的nginx使用的使用者。如果沒有指定,預設為 nobody。 

--group=<group> - 在nginx.conf中沒有指定user指令的情況下,預設的nginx使用的組。如果沒有指定,預設為 nobody。 

--builddir=DIR - 指定編譯的目錄 

--with-rtsig_module - 啟用 rtsig 模組 

3.最後安裝 make && make install 我們測試一下nginx是否正常工作。因為我們把nginx的二進位執行檔案配置安裝在 /usr/local/sbin/nginx  (前面的configure參數 --sbin-path=/usr/local/sbin/nginx ),目錄/usr/local/sbin/預設在linux的PATH環境變數裡,所以我們可以省略路徑執行它: [root@fsvps nginx-1.2.1]# nginx它不會有任何資訊顯示。開啟瀏覽器,通過IP地址訪問你的nginx網站,如果看到這樣一行大黑字,就證明nginx已經工作了: Welcome to nginx! 看上去很簡陋,只是因為沒有給它做漂亮的頁面。我們來簡單配置一下它的回合組態。我們configure的參數--conf-path=/usr/local/conf/nginx/nginx.conf 這就是它的設定檔。我們去看看。 [root@fsvps nginx-1.2.1]# cd /usr/local/conf/nginx/[root@fsvps nginx]# lsfastcgi.conf          fastcgi_params.default  mime.types          nginx.conf.default   uwsgi_paramsfastcgi.conf.default  koi-utf                 mime.types.default  scgi_params          uwsgi_params.defaultfastcgi_params        koi-win                 nginx.conf          scgi_params.default  win-utf如果這裡沒有nginx.conf,但有個 nginx.conf.default,我們用它複製個副本nginx.conf (早期版本預設是沒有nginx.conf的) 用你熟悉的編輯器開啟它,vi nginx.conf找到如下的部分,大概在35行, server {        listen       80 default;        server_name  localhost;        root /var/www/html/default;        #charset koi8-r;         #access_log  logs/host.access.log  main;         location / {        #    root   html;            index  index.html index.htm;        }....................}如上紅色加粗顯示部分,做三處修改: listen 80 後插入“空格default”在裡面加入一行 root /var/www/html/default;注釋掉root html;一行上面的server{....}是定義的一台虛擬機器主機,我們剛加入的一行是定義這個虛假主機的web目錄。listen 行的default是表示這一個server{...}節點是預設的虛假主機(預設網站) 執行 nginx -t 測試剛才的nginx設定檔是否有語法錯誤: [root@fsvps nginx]# nginx -tnginx: the configuration file /usr/local/conf/nginx/nginx.conf syntax is oknginx: configuration file /usr/local/conf/nginx/nginx.conf test is successful顯示沒有錯誤,檢測通過。如果不是這樣,請返回仔細檢查你的設定檔是否哪裡寫錯了,注意行尾要有英文分號。 我們在硬碟上建立這個目錄: [root@fsvps nginx]# mkdir -p /var/www/html/default寫一個html檔案index.html 到/var/www/html/default目錄裡,使用你熟悉的編輯器,隨便寫什麼內容、怎麼排版。 然後讓nginx重新載入設定檔,看看剛才創作的html頁面效果如何。 常見錯誤FAQ1) 如果通常訪問時看到還是 Welcome to nginx! ,請返回檢查,重點在這幾處: 請確認/var/www/html/default 目錄下有你創作的index.html 檔案?檢查該檔案許可權,other使用者是否有讀的許可權? 如果不懂linux檔案許可權,請執行 chmod 755 /var/www/html/default/index.html檢查nginx.conf設定檔裡,只否只有一個server{...} 節點,並且該節點裡是否有 listen       80 default;   一行,注意其中要有 default 。檢查上述server{...}節點裡是否有 root /var/www/html/default; 一行,注意路徑是拼字是否正確。檢查其中 location / {...} 節點裡的 #    root   html;  一行,是否注釋掉了。2) 如果看到的是 404 Not Found 或者“找不到該頁面”類似的提示: 檢查上述 location / {...} 節點中是否有這一行 index  index.html index.htm;3) 如果訪問時,顯示“找不到伺服器”、“無法串連到伺服器”這樣的錯誤: 運行檢查nginx進程在運行,運行ps aux |grep nginx 命令,正常情況有如下三條:nginx: master process nginxnginx: worker processgrep nginx如果只有第三條,請運行nginx 重新啟用nginx,如有報錯請照說明檢查。一般是設定檔的語法錯誤。請運行nginx -t 檢查設定檔是否有語法錯誤。[tips] location / {...} 節點裡的 #    root   html;  一行,不注釋掉也可以:請把你創造的index.html 放到/var/www/html/default/html目錄下。 至此,我們的nginx也可以正常工作了。 查看nginx進程ps –ef | grep nginx    4.設定成系統開機服務: 
在 /etc/init.d/  目錄下建立 nginx 檔案 內容如下:

 

#!/bin/bash# nginx Startup script for the Nginx HTTP Server# this script create it by gcec at 2009.10.22.# it is v.0.0.1 version.# if you find any errors on this scripts,please contact gcec cyz.# and send mail to support at gcec dot cc.## 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=/app/nginx/sbin/nginxnginx_config=/app/nginx/conf/nginx.confnginx_pid=/var/run/nginx.pid RETVAL=0prog="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 ]||exit0  # Start nginx daemons functions.start() { if[-e $nginx_pid ];then   echo "nginx already running...."   exit1fi    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 /var/run/nginx.pid}  # reload nginx service functions.reload() {     echo -n $"Reloading $prog: "    #kill -HUP `cat ${nginx_pid}`    killproc $nginxd -HUP    RETVAL=$?    echo } # See how we were called.case "$1" instart)        start        ;; stop)        stop        ;; reload)        reload        ;; restart)        stop        start        ;; status)        status $prog        RETVAL=$?        ;;*)        echo $"Usage: $prog {start|stop|restart|reload|status|help}"        exit1esac exit $RETVAL chkco
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.