【CNMP系列】CentOS7.0下安裝Nginx服務
話步前言,CNMP之路,系統起步:http://www.cnblogs.com/riverdubu/p/6425028.html
這回我來講解下CentOS7.0下如何安裝和配置Nginx服務
Nginx的歷史不在此贅述,輕量,快是它的特性。只是因為現在的模塊沒有達到apache的模塊數量級,未來有超越apache的勢頭。
首先,我們要安裝個必要的軟件(上節提到過,可能有人並未安裝)
#yum install wget
因為Nginx以來與gcc的編譯環境,所以,在mini centos中需要安裝編譯環境來使Nginx能夠編譯起來。
#yum install gcc-c++
Nginx的http模塊需要使用pcre來解析正則表達式。
#yum -y install pcre pcre-devel
依賴的解壓包
#yum -y install zlib zlib-devel
openssl安裝,Nginx提供http和https協議,https是大勢所趨,坑爹的微信小程序需要支持https,其實https也不難,只需要配置下即可,關鍵是證書麻煩,建議大家去startssl申請證書,免費,只是需要的材料較多,以後有機會給大夥寫一篇關於申請ssl證書的博文。
#yum install -y openssl openssl-devel
好了,現在就輪到主角登場啦!!!
下載Nginx源碼
先去Nginx官網查看最新版的Nginx源碼地址:
https://nginx.org/en/download.html
作為新手,還是下載stable version比較保險,下載前最好將下載目錄定位到自己新建的目錄中去。
https://nginx.org/download/nginx-1.10.3.tar.gz
#wget -c https://nginx.org/download/nginx-1.10.3.tar.gz
下面開始對其解壓
#tar -zxvf nginx-1.10.3.tar.gz
進入Nginx目錄
#cd nginx-1.10.3
這裏就是Nginx的所有源碼啦,感興趣的朋友可以先對源碼多了解,後續我會針對源碼推出一系列博文。
好了,下面就要對Nginx的源碼進行編譯啦!編譯命令三劍客登場!!!
#./configure
creating objs/Makefile這步之後,就成功啦!
開始編譯
#make
#make install
OK,所有的工作都已經做完啦,下面開始啟動Nginx服務並在遠程測試,想想是不是很激動。
一般編譯安裝完的軟件都會放在/usr裏,這不是user,這是Unix System Resource,是Unix系統資源的縮寫。
我們在/user/local/裏面發現了nginx,進入。
#cd /usr/local/nginx/
如果你找不到,試試這條命令吧
#whereis nginx
它會告訴你nginx在哪,nginx的命令在/usr/local/nginx/sbin目錄下
對於nginx的啟動,停止,我簡單的列舉下
./nginx
./nginx -s stop
./nginx -s quit
./nginx -s reload
./nginx -s quit
:此方式停止步驟是待nginx進程處理任務完畢進行停止。./nginx -s stop
:此方式相當於先查出nginx進程id再使用kill命令強制殺掉進程。
查詢nginx進程:
ps aux|grep nginx
root 23045 0.0 0.0 24468 764 ? Ss 23:02 0:00 nginx: master process sbin/nginx
nobody 23046 0.0 0.1 24888 1232 ? S 23:02 0:00 nginx: worker process
看到這兩條進程狀態,你成功了。PS:grep是篩選,|是管道,Linux裏篩選的常用方式。
現在,在你的瀏覽器中輸入你遠端服務器的ip,看看是否有Nginx歡迎你的字樣。
如果沒有,關閉CentOS的防火墻試試。PS:防火墻關閉之後註意配置iptables
CentOS7.0以上默認firewall為防火墻配置,我們這裏改為iptables配置。
停止firewall
#systemctl stop firewalld.service
禁止firewall開機啟動
#systemctl disable firewalld.service
查看默認防火墻狀態(關閉後顯示not running,開啟後顯示running)
#firewall-cmd --state
配置iptables,首先需要安裝iptables服務
#yum install iptables-services
編輯防火墻配置文件
#vim /etc/sysconfig/iptables
加入下面的幾行,22是默認存在的
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -jACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 8080-j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
vim裏面是直接yy然後p的,不懂的朋友去看下vim編輯器的基本操作哈,裏面有具體的詳情。vim裏面撤銷編輯是回到初始頁面,就是按esc,然後點擊u即可。
22端口是供ssh訪問的,80,8080端口是http服務訪問的,以後用到https,也需要打開443端口的訪問權限。
保存,重啟iptables服務
最後重啟防火墻使配置生效
#systemctl restart iptables.service
設置防火墻開機啟動
#systemctl enable iptables.service
再次訪問遠程服務器的ip,是不是有Nginx歡迎你的頁面了?
好了,這節就先說到這裏,關於Nginx服務器的配置,我們下節再說。 ^_^
註
重啟之後firewall又被打開,所以我們要設置禁止firewall開機自啟動
停止firewall
#systemctl stop firewalld.service
禁止firewall開機啟動
#systemctl disable firewalld.service
nginx服務未被加入到開機自啟動列表,重啟服務器後,未發現nginx服務,我們需要手動加入開機自啟動
第一步,添加一個新文件,nginx.service
#vim /lib/systemd/system/nginx.service
輸入以下內容
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart= /usr/local/nginx/sbin/nginx
ExecReload= /usr/local/nginx/sbin/nginx -s reload
ExecStop= /usr/local/nginx/sbin/nginx -s quit
PrivateTmp= true
[Install]
WantedBy=multi-user.target
|
更改文件權限
#chmod 745 /lib/systemd/system/nginx.service
設置開機自啟動
#systemctl enable nginx.service
趕緊開機試試看吧!
註2
設置簡便的開啟和關閉nginx服務
編輯init.d啟動服務文件
#vim /etc/init.d/nginx
輸入一下內容
1 #!/bin/bash 2 # nginx Startup script for the Nginx HTTP Server 3 # it is v.0.0.2 version. 4 # chkconfig: - 85 15 5 # description: Nginx is a high-performance web and proxy server. 6 # It has a lot of features, but it‘s not for everyone. 7 # processname: nginx 8 # pidfile: /var/run/nginx.pid 9 # config: /usr/local/nginx/conf/nginx.conf 10 nginxd=/usr/local/nginx/sbin/nginx 11 nginx_config=/usr/local/nginx/conf/nginx.conf 12 nginx_pid=/var/run/nginx.pid 13 RETVAL=0 14 prog="nginx" 15 # Source function library. 16 . /etc/rc.d/init.d/functions 17 # Source networking configuration. 18 . /etc/sysconfig/network 19 # Check that networking is up. 20 [ ${NETWORKING} = "no" ] && exit 0 21 [ -x $nginxd ] || exit 0 22 # Start nginx daemons functions. 23 start() { 24 if [ -e $nginx_pid ];then 25 echo "nginx already running...." 26 exit 1 27 fi 28 echo -n $"Starting $prog: " 29 daemon $nginxd -c ${nginx_config} 30 RETVAL=$? 31 echo 32 [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx 33 return $RETVAL 34 } 35 # Stop nginx daemons functions. 36 stop() { 37 echo -n $"Stopping $prog: " 38 killproc $nginxd 39 RETVAL=$? 40 echo 41 [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid 42 } 43 # reload nginx service functions. 44 reload() { 45 echo -n $"Reloading $prog: " 46 #kill -HUP `cat ${nginx_pid}` 47 killproc $nginxd -HUP 48 RETVAL=$? 49 echo 50 } 51 # See how we were called. 52 case "$1" in 53 start) 54 start 55 ;; 56 stop) 57 stop 58 ;; 59 reload) 60 reload 61 ;; 62 restart) 63 stop 64 start 65 ;; 66 status) 67 status $prog 68 RETVAL=$? 69 ;; 70 *) 71 echo $"Usage: $prog {start|stop|restart|reload|status|help}" 72 exit 1 73 esac 74 exit $RETVAL
保存退出
修改該文件權限
#chmod a+x /etc/init.d/nginx
現在就可以開心的使用nginx服務啦
開啟:/etc/init.d/nginx start
關閉:/etc/init.d/nginx stop
狀態:/etc/init.d/nginx status
重啟:/etc/init.d/nginx restart
至此,所有nginx安裝相關完畢,有問題留言哈,有留必回。
【CNMP系列】CentOS7.0下安裝Nginx服務