Centos6.5安裝及簡單配置nginx
一、準備事項
(1) 因爲nginx需要訪問80端口所以請先關閉或者開放防火牆端口,和selinux。
參考命令
關閉防火牆: [root@local ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT [root@local ~]# service iptables save 關閉selinux: [root@local ~]# setenforce 0 [root@local ~]# vim /etc/selinux/config 將SELINUX=enforcing改爲SELINUX=disabled |
(2) 如果用到域名請自行構建DNS服務
二、安裝
(1) 因爲nginx的運行需要安裝pcre、zlib等軟件包,因此我們進行安裝
Pcre=Pcre Compatible Regular Expressions(中文pcre兼容正則表達式)
Yum配置請參考:http://liumingyuan.blog.51cto.com/9065923/1712824
[root@local ~] yum -y install pcre* zlib* #或者進行編譯安裝 [root@local ~]# useradd -M -s /sbin/nologin nginx #創建nginx服務 |
啓動用戶
(3) 編譯安裝nginx,下載地址:http://nginx.org/en/download.html 此次安裝爲最新穩定版nginx-1.8.0
[root@local ~]# tar zxf nginx-1.8.0.tar.gz [root@local ~]# cd nginx-1.8.0 [root@local nginx-1.8.0]# ls auto CHANGES.ru configure html Makefile objs src CHANGES conf contrib LICENSE man README [root@local nginx-1.8.0]# ./configure --user=nginx --group=nginx --prefix=/application/nginx-1.8.0 --with-http_stub_status_module --with-http_ssl_module #./configure –help 參數詳解 [root@local nginx-1.8.0]# make [root@local nginx-1.8.0]# make install |
(4) 製作軟連接
[root@local nginx-1.8.0]#ln –s /application/nginx-1.8.0/ /application/nginx |
(5) 基本使用
#語法檢查 [root@local nginx-1.8.0]# /application/nginx/sbin/nginx –t nginx: the configuration file /application/nginx-1.8.0/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.8.0/conf/nginx.conf test is successful #啓動服務 [root@local nginx-1.8.0]# /application/nginx/sbin/nginx #端口檢查 [root@local nginx-1.8.0]# netstat –lnt #檢查進程 [root@local nginx-1.8.0]# ps -ef | grep nginx #端口信息保存在 /application/nginx/logs/ nginx.pid 文件中 #通過端口查看佔用進程 [root@local nginx-1.8.0]# lsof -i :80 #錯誤日誌 /application/nginx/logs/error.log |
三、編寫nginx服務腳本
爲了方便使用習慣,通過server 來啓動、關閉、開啓、重載nginx服務所以我們來編
寫nginx的服務腳本(自己編寫的腳本僅供參考!)
[root@local ~]# vim /etc/init.d/nginx
#!/bin/bash #chkconfig: - 99 20 #description:Nginx Server Contorl Script PROG="/application/nginx/sbin/nginx" PIDF="/application/nginx/logs/nginx.pid" ok=`echo -e "\e[1;31m [ok] \e[0m"` no=`echo -e "\e[1;31m [no] \e[0m"` detection=`/application/nginx/sbin/nginx -t 2>&1` screen_1=`echo $detection | awk '{print $6,$7,$8}'` screen_2=`echo $detection | awk '{print $13,$14,$15}'` if [ "$screen_1" = "syntax is ok" ] && [ "$screen_2" = "test is successful" ]; then case "$1" in start) $PROG echo "Nginx Is starting state $ok" ;; stop) kill -s QUIT $(cat $PIDF) echo "Nginx Is closing state $ok" ;; restart) $0 stop $0 start echo "Nginx Is to restart state $ok" ;; reload) kill -s HUP $(cat $PIDF) echo "Nginx Is overloaded state $ok" ;; *) echo "Usage: $0 (start|stop|restart|reload)" exit 1 esac else echo "Nginx check state $no " echo "Please check the configuration file" echo "$detection" fi exit 0 |
[root@local ~]# chmod +x /etc/init.d/nginx [root@local ~]# chkconfig –add nginx #添加爲系統服務 [root@local ~]# chkconfig nginx on |
四、簡單的nginx web站點
Nginx的默認站點目錄,是安裝目錄下的html這裏是(/application/nginx/html)
在主配置文件/application/nginx/conf/nginx.conf 中查看,對於重新部署web頁面
只需將/application/nginx/html/中的index.html替換即可
主配置文件講解
[root@local ~]# egrep -v "#|^$" /application/nginx/conf/nginx.conf worker_processes 1; #指定Nginx開啓的進程數 events { #設定Nginx的工作模式及連接數上線 worker_connections 1024; } http { include mime.types; #主模塊命令,實現對配置文件所有包含文件的設置 default_type application/octet-stream; #屬於http核心模塊命令,這裏設 置類型爲二進制流,也就是當文件類型未定義時使用這種方式,例如,沒有配置PHP 環境時,nginx是不給予解析的,此時,用瀏覽器訪問PHP文件就會出現下載窗口。 sendfile on; #用於高效文件傳輸模式 keepalive_timeout 65; 設置客戶端請求頭文件讀取超時時間,如果超過這個時 間服務器會關閉該連接。 server { #定義虛擬主機開始的關鍵字 listen 80; #用於指定虛擬主機的服務端口 server_name localhost; 用於指定ip地址或者域名,多個域名用空格隔開 location / { root html; index index.html index.htm; #用於設定訪問的默認首頁 } error_page 500 502 503 504 /50x.html;# 靜態頁面重定向服務器錯誤 頁面,例如攜程的網站崩潰出現的頁面 location = /50x.html { root html; } } } |
過兩天將會發出LNMP和高級配置及調優