【CNMP系列】CentOS7.0下安裝Nginx服務

話步前言,CNMP之路,系統起步:http://www.cnblogs.com/riverdubu/p/6425028.htmlhtml

這回我來說解下CentOS7.0下如何安裝和配置Nginx服務nginx

Nginx的歷史不在此贅述,輕量,快是它的特性。只是由於如今的模塊沒有達到apache的模塊數量級,將來有超越apache的勢頭。c++

首先,咱們要安裝個必要的軟件(上節提到過,可能有人並未安裝)web

#yum install wget正則表達式

 由於Nginx以來與gcc的編譯環境,因此,在mini centos中須要安裝編譯環境來使Nginx可以編譯起來。apache

#yum install gcc-c++小程序

Nginx的http模塊須要使用pcre來解析正則表達式。vim

#yum -y install pcre pcre-devel微信小程序

依賴的解壓包centos

#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

輸入如下內容

[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安裝相關完畢,有問題留言哈,有留必回。

相關文章
相關標籤/搜索