nginx在CentOs下的安裝及配置

來源:互聯網
上載者:User

標籤:

   前言:

   先介紹一下nginx:

    Nginx是一款輕量級的Web 伺服器/反向 Proxy伺服器及電子郵件(IMAP/POP3)Proxy 伺服器,並在一個BSD-like 協議下發行。其特點是佔有記憶體少,並發能力強,事實上nginx的並發能力確實在同類型的網頁伺服器中表現較好。

  下面介紹具體的安裝細節:

1、在安裝nginx之前要安裝好依賴和軟體工具包:(如果系統存在工具包可忽略此步驟,可在命令列下輸入命令測試命令是否可用)

  (1)安裝make:

      # yum -y install gcc automake autoconf libtool make

(2)安裝g++:

      # yum install gcc gcc-c++

   其他的比如tar等命令工具包沒有的自行下載安裝。

2、安裝開發組和所依賴的軟體包(開發工具、伺服器平台開發)

 

   #  yum groupinstall -y "Development Tools" "Server Platform Development"
   #  yum install -y openssl-devel pcre-devel

3、開始安裝nginx:

 首先添加使用者nginx,實現以之運行nginx服務進程:

    # groupadd -r nginx

    # useradd -r -g nginx nginx

 然後使用wget從網上直接下載nginx壓縮包:

    # yum install wget

    # wget http://nginx.org/download/nginx-1.4.7.tar.gz

 解壓後,進行編譯:先進入目錄下

    # tar xf nginx-1.4.7.tar.gz

    # cd nginx-1.4.7

 接著開始編譯和安裝:

# ./configure   --prefix=/usr   --sbin-path=/usr/sbin/nginx   --conf-path=/etc/nginx/nginx.conf   --error-log-path=/var/log/nginx/error.log   --http-log-path=/var/log/nginx/access.log   --pid-path=/var/run/nginx/nginx.pid    --lock-path=/var/lock/nginx.lock   --user=nginx   --group=nginx   --with-http_ssl_module   --with-http_flv_module   --with-http_stub_status_module   --with-http_gzip_static_module   --http-client-body-temp-path=/var/tmp/nginx/client/   --http-proxy-temp-path=/var/tmp/nginx/proxy/   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi   --http-scgi-temp-path=/var/tmp/nginx/scgi   --with-pcre

  然後:

      # make && make install

4、為nginx提供SysV init指令碼

   建立檔案/etc/rc.d/init.d/nginx,內容如下:

#!/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 0nginx="/usr/sbin/nginx"prog=$(basename $nginx)NGINX_CONF_FILE="/etc/nginx/nginx.conf"[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginxlockfile=/var/lock/subsys/nginxmake_dirs() {   # make required directories   user=`nginx -V 2>&1 | grep "configure arguments:" | sed ‘s/[^*]*--user=[]∗.*/\1/g‘ -`   options=`$nginx -V 2>&1 | grep ‘configure arguments:‘`   for opt in $options; do       if [ `echo $opt | grep ‘.*-temp-path‘` ]; then           value=`echo $opt | cut -d "=" -f 2`           if [ ! -d "$value" ]; then               # echo "creating" $value               mkdir -p $value && chown -R $user $value           fi       fi   done}start() {    [ -x $nginx ] || exit 5    [ -f $NGINX_CONF_FILE ] || exit 6    make_dirs    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}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 2esac
View Code

    而後為此指令碼賦予執行許可權:

     # chmod +x /etc/rc.d/init.d/nginx

   添加至服務管理列表,並讓其開機自動啟動: 

     # chkconfig --add nginx

     # chkconfig nginx on

5、加入環境變數並測試

     # yum install vim
     # vim /etc/profile.d/nginx.sh
     export PATH=/usr/local/nginx/sbin/:$PATH

  然後啟動服務並測試:

     # service nginx start

  在網站輸入伺服器IP地址:http://172.16.16.173/

 

6、將php配置進人nginx中:

   首先配置nginx的nginx.conf檔案:

在終端下輸入: # find / -name nginx.conf

找到nginx.conf的設定檔,利用vim進入修改:(如果沒有vim的話  yum install vim進行下載安裝)

    然後在server裡面加入如下內容:

location ~ \.php$ {              root         /usr/html;              fastcgi_pass   127.0.0.1:9000;              fastcgi_index  index.php;              fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;              include        fastcgi_params;          }

       黃色部分為web的存放的目錄,在瀏覽器裡可直接存取的。

還要注意的是:編譯php時必須確保系統中擁有libxml2與libxml2-dev這倆個軟體包,因為php預設會將xml的功能編譯進去,所以xml的支援是必不可少的。

所以使用: # yum install libxml2  

                 #  yum install libxml2-dev    下載安裝好依賴包。

   然後進入/usr/html的目錄下,vim test1.php,進行編輯:

<?php    echo "TEST BY STILL";    phpinfo();?>

重新啟動nginx服務:

    # service nginx restart 

然後在瀏覽器裡查看結果:

 

 

nginx在CentOs下的安裝及配置

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.