天天看點

Nginx實戰基礎篇一 源碼包編譯安裝部署web伺服器

實驗步驟:

<b>一、安裝</b><b>nginx</b><b>必須的依賴包</b> 

[root@rhel6u3-7 ~]# yum -y install gcc openssl-devel pcre-devel zlib-devel  //yum建立過程略,安裝略 

<b>二、安裝編譯</b><b>nginx</b><b>,目前系統測試環境為</b><b>rhel6.3  </b><b>軟體版本為</b><b>nginx-1.27</b>

[root@rhel6u3-7 ~]# useradd nginx -s /sbin/nologin //給nginx伺服器建立背景程序管理使用者 

[root@rhel6u3-7 ~]# tar zxvf nginx-1.2.7.tar.gz  //解壓縮 

[root@rhel6u3-7 ~]# cd nginx-1.2.7  

[root@rhel6u3-7 nginx-1.2.7]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-http_ssl_module   

// --user=nginx –group=nginx 設定允許nginx允許的使用者群組為nginx 

// --prefix=/usr/local/nginx/  設定nginx安裝路徑 

// --with-http_stub_status_module 安裝允許狀态子產品 

// --with-http_ssl_module 安裝ssl子產品 

//更多參數請參看 ./configure --help 

……  //安裝顯示略,如果配置正确,最後會顯示以下資訊。 

Configuration summary 

  + using system PCRE library 

  + using system OpenSSL library 

  + md5: using OpenSSL library 

  + sha1: using OpenSSL library 

  + using system zlib library 

  nginx path prefix: "/usr/local/nginx/" 

  nginx binary file: "/usr/local/nginx//sbin/nginx" 

  nginx configuration prefix: "/usr/local/nginx//conf" 

  nginx configuration file: "/usr/local/nginx//conf/nginx.conf" 

  nginx pid file: "/usr/local/nginx//logs/nginx.pid" 

  nginx error log file: "/usr/local/nginx//logs/error.log" 

  nginx http access log file: "/usr/local/nginx//logs/access.log" 

  nginx http client request body temporary files: "client_body_temp" 

  nginx http proxy temporary files: "proxy_temp" 

  nginx http fastcgi temporary files: "fastcgi_temp" 

  nginx http uwsgi temporary files: "uwsgi_temp" 

  nginx http scgi temporary files: "scgi_temp" 

[root@rhel6u3-7 ~]# /usr/local/nginx/sbin/nginx  –V  //安裝完成後檢視Nginx的相關環境配置資訊是否正确 

nginx version: nginx/1.2.7 

built by gcc 4.4.6 20120305 (Red Hat 4.4.6-4) (GCC)  

TLS SNI support enabled 

configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-http_ssl_module 

[root@rhel6u3-7 ~]# 

[root@rhel6u3-7 nginx-1.2.7]# make &amp; make install   //編譯安裝過程略 

<b>三、通過</b><b>nginx</b><b>自身腳本機器</b><b>nginx</b><b>伺服器</b><b>,</b><b>并通過各種指令檢視是否啟動成功</b>

[root@rhel6u3-7 ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf   // -c指向nginx主配置檔案 

[root@rhel6u3-7 ~]# lsof -i :80  //檢視nginx程序号及運作情況 

COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME 

nginx   4080  root    6u  IPv4  21467      0t0  TCP *:http (LISTEN) 

nginx   4081 nginx    6u  IPv4  21467      0t0  TCP *:http (LISTEN) 

[root@rhel6u3-7 ~]# ps -ef | grep nginx  //檢視nginx程序号及運作情況 

root      4080     1  0 22:24 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 

nginx     4081  4080  0 22:24 ?        00:00:00 nginx: worker process                                           

root      4088  1433  0 22:27 pts/0    00:00:00 grep nginx 

[root@rhel6u3-7 ~]# netstat -nltp | grep 80  //檢視nginx程序監聽端口 

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      4080/nginx  

<b>四</b><b>,</b><b>測試</b><b>nginx</b><b>配置的預設</b><b>web</b><b>伺服器是否能正常運作</b><b></b>

<b></b>

通過linux自帶指令links 測試

[root@rhel6u3-7 ~]# links 127.0.0.1  //出現 welcome to nginx!說明nginx服務啟動成功

通過windows伺服器IE浏覽器測試

<a href="http://blog.51cto.com/attachment/201302/211330680.png" target="_blank"></a>

<b>擴充知識</b><b>:</b><b></b>

1、設定nginx開機自動啟動,将nginx啟動指令添加到/etc/rc.local 

[root@rhel6u3-7 ~]# echo "/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf" &gt;&gt; /etc/rc.local  

[root@rhel6u3-7 ~]# cat /etc/rc.local | grep nginx 

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 

2、通過設定System V 腳本,使用server指令啟動nginx服務

[root@rhel6u3-7 init.d]# vim  nginx  //在/etc/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" ] &amp;&amp; exit 0 

    nginx="/usr/local/nginx/sbin/nginx" 

    prog=$(basename $nginx) 

    NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" 

[ -f /etc/sysconfig/nginx ] &amp;&amp; . /etc/sysconfig/nginx 

    lockfile=/var/lock/subsys/nginx 

start() { 

    [ -x $nginx ] || exit 5 

    [ -f $NGINX_CONF_FILE ] || exit 6 

    echo -n $"Starting $prog: " 

    daemon $nginx -c $NGINX_CONF_FILE 

    retval=$? 

    echo 

[ $retval -eq 0 ] &amp;&amp; touch $lockfile 

    return $retval 

stop() { 

    echo -n $"Stopping $prog: " 

    killproc $prog -QUIT 

[ $retval -eq 0 ] &amp;&amp; rm -f $lockfile 

    killall -9 nginx 

restart() { 

    configtest || return $? 

    stop 

    sleep 1 

    start 

reload() { 

    echo -n $"Reloading $prog: " 

    killproc $nginx -HUP 

    RETVAL=$? 

force_reload() { 

    restart 

configtest() { 

    $nginx -t -c $NGINX_CONF_FILE 

rh_status() { 

    status $prog 

rh_status_q() { 

    rh_status &gt;/dev/null 2&gt;&amp;1 

case "$1" in 

    start) 

        rh_status_q &amp;&amp; exit 0 

        $1 

    ;; 

    stop) 

        rh_status_q || exit 0 

    restart|configtest) 

    reload) 

        rh_status_q || exit 7 

    force-reload) 

        force_reload 

    status) 

        rh_status 

    condrestart|try-restart) 

    *) 

        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 

        exit 2 

esac 

[root@rhel6u3-7 init.d]# chmod 755 nginx   //修改腳本檔案nginx的權限 

[root@rhel6u3-7 init.d]# chkconfig --add nginx  //将腳本檔案加入chkconfig中 

[root@rhel6u3-7 init.d]# chkconfig --level 35 nginx on  //設定nginx開機在3和5級别自動啟動 

[root@rhel6u3-7 init.d]# chkconfig --list | grep nginx 

nginx           0:off   1:off   2:off   3:on    4:off   5:on    6:off 

本文轉自淩激冰51CTO部落格,原文連結:http://blog.51cto.com/dreamfire/1140965,如需轉載請自行聯系原作者

繼續閱讀