Linux安裝nginx

centos 7.0根目錄 的目錄構成



[root@localhost src]# wget http://nginx.org/download/nginx-1.7.9.tar.gz
--2015-01-24 12:55:21--  http://nginx.org/download/nginx-1.7.9.tar.gz
正在解析主機 nginx.org (nginx.org)... 206.251.255.63
正在連接 nginx.org (nginx.org)|206.251.255.63|:80... 已連接。
已發出 HTTP 請求,正在等待迴應... 200 OK
長度:827263 (808K) [application/octet-stream]
正在保存至: “nginx-1.7.9.tar.gz”

100%[======================================>] 827,263      154KB/s 用時 6.5s

2015-01-24 12:55:28 (125 KB/s) - 已保存 “nginx-1.7.9.tar.gz” [827263/827263])

[root@localhost src]# ls
nginx-1.7.9.tar.gz

解壓壓縮包 .tar.gz

解壓壓縮包 到指定的目錄 



[root@localhost src]# tar zxvf nginx-1.7.9.tar.gz -C /usr/local/bin

結果查看

[root@localhost src]# cd /usr/local/bin
[root@localhost bin]# ls
nginx-1.7.9

進入解壓目錄查看

 


文件>文件>

安裝gcc

[root@localhost nginx-1.7.9]# yum -y install gcc-c++

安裝最基礎的模塊 pcre-devel

[root@localhost nginx-1.7.9]# yum -y install pcre-devel

安裝gzip  

zlib-devel

[root@localhost nginx-1.7.9]# yum -y install zlib-devel

 

 

下面開始編譯nginx 最簡單 最少的編輯

[root@localhost nginx-1.7.9]# ./configure --prefix=/usr/etc/ngin


make


[root@localhost nginx-1.7.9]# make


make install

[root@localhost nginx-1.7.9]# make install

啓動nginx

[root@localhost nginx-1.7.9]# cd /usr/etc
[root@localhost etc]# ls
ngin
[root@localhost etc]# cd ngin
[root@localhost ngin]# ls
conf  html  logs  sbin
[root@localhost ngin]# cd sbin
[root@localhost sbin]# ls
nginx
[root@localhost sbin]# ./nginx

查看進程  查看nginx是否啓用

[root@localhost sbin]# ps -ef|grep nginx
root     13536     1  0 14:14 ?        00:00:00 nginx: master process ./nginx
nobody   13537 13536  0 14:14 ?        00:00:00 nginx: worker process
root     13540  2281  0 14:16 pts/0    00:00:00 grep --color=auto nginx
lsof -i:80 安裝root用戶的纔是 默認的nobody需要刪除
[root@localhost sbin]# lsof -i:80
COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   13536   root    6u  IPv4  32434      0t0  TCP *:http (LISTEN)
nginx   13548 nobody    6u  IPv4  32434      0t0  TCP *:http (LISTEN)

 

 

 

重啓nginx

[root@localhost sbin]# ./nginx -s reload

查看iptables表的規則 是否限制了80等、

[root@localhost sbin]# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
2    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0
3    INPUT_direct  all  --  0.0.0.0/0            0.0.0.0/0
4    INPUT_ZONES_SOURCE  all  --  0.0.0.0/0            0.0.0.0/0
5    INPUT_ZONES  all  --  0.0.0.0/0            0.0.0.0/0
6    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0
7    REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
2    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0
3    FORWARD_direct  all  --  0.0.0.0/0            0.0.0.0/0
4    FORWARD_IN_ZONES_SOURCE  all  --  0.0.0.0/0            0.0.0.0/0
5    FORWARD_IN_ZONES  all  --  0.0.0.0/0            0.0.0.0/0
6    FORWARD_OUT_ZONES_SOURCE  all  --  0.0.0.0/0            0.0.0.0/0          
7    FORWARD_OUT_ZONES  all  --  0.0.0.0/0            0.0.0.0/0
8    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0
9    REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination
1    OUTPUT_direct  all  --  0.0.0.0/0            0.0.0.0/0

Chain FORWARD_IN_ZONES (1 references)
num  target     prot opt source               destination
1    FWDI_public  all  --  0.0.0.0/0            0.0.0.0/0           [goto]
2    FWDI_public  all  --  0.0.0.0/0            0.0.0.0/0           [goto]

Chain FORWARD_IN_ZONES_SOURCE (1 references)
num  target     prot opt source               destination

Chain FORWARD_OUT_ZONES (1 references)
num  target     prot opt source               destination
1    FWDO_public  all  --  0.0.0.0/0            0.0.0.0/0           [goto]
2    FWDO_public  all  --  0.0.0.0/0            0.0.0.0/0           [goto]

Chain FORWARD_OUT_ZONES_SOURCE (1 references)
num  target     prot opt source               destination

Chain FORWARD_direct (1 references)
num  target     prot opt source               destination

Chain FWDI_public (2 references)
num  target     prot opt source               destination
1    FWDI_public_log  all  --  0.0.0.0/0            0.0.0.0/0
2    FWDI_public_deny  all  --  0.0.0.0/0            0.0.0.0/0
3    FWDI_public_allow  all  --  0.0.0.0/0            0.0.0.0/0

Chain FWDI_public_allow (1 references)
num  target     prot opt source               destination

Chain FWDI_public_deny (1 references)
num  target     prot opt source               destination

Chain FWDI_public_log (1 references)
num  target     prot opt source               destination

Chain FWDO_public (2 references)
num  target     prot opt source               destination
1    FWDO_public_log  all  --  0.0.0.0/0            0.0.0.0/0
2    FWDO_public_deny  all  --  0.0.0.0/0            0.0.0.0/0
3    FWDO_public_allow  all  --  0.0.0.0/0            0.0.0.0/0

Chain FWDO_public_allow (1 references)
num  target     prot opt source               destination

Chain FWDO_public_deny (1 references)
num  target     prot opt source               destination

Chain FWDO_public_log (1 references)
num  target     prot opt source               destination

Chain INPUT_ZONES (1 references)
num  target     prot opt source               destination
1    IN_public  all  --  0.0.0.0/0            0.0.0.0/0           [goto]
2    IN_public  all  --  0.0.0.0/0            0.0.0.0/0           [goto]

Chain INPUT_ZONES_SOURCE (1 references)
num  target     prot opt source               destination

Chain INPUT_direct (1 references)
num  target     prot opt source               destination

Chain IN_public (2 references)
num  target     prot opt source               destination
1    IN_public_log  all  --  0.0.0.0/0            0.0.0.0/0
2    IN_public_deny  all  --  0.0.0.0/0            0.0.0.0/0
3    IN_public_allow  all  --  0.0.0.0/0            0.0.0.0/0

Chain IN_public_allow (1 references)
num  target     prot opt source               destination
1    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22 ctstate NEW

Chain IN_public_deny (1 references)
num  target     prot opt source               destination

Chain IN_public_log (1 references)
num  target     prot opt source               destination

Chain OUTPUT_direct (1 references)
num  target     prot opt source               destination

查看規則

iptables -nL --line-number
iptables -nvL --line-number
-L 查看當前表的所有規則,默認查看的是filter表,如果要查看NAT表,可以加上-t NAT參數
-n 不對ip地址進行查,加上這個參數顯示速度會快很多
-v 輸出詳細信息,包含通過該規則的數據包數量,總字節數及相應的網絡接口
–line-number 顯示規則的序列號,這個參數在刪除或修改規則時會用到

 

現在訪問http://192.168.1.103/ 無法訪問 不知道什麼情況額~ IP 肯定沒錯啊

[root@localhost sbin]# ip addr
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp2s0:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 6c:62:6d:73:e6:4a brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.103/24 brd 192.168.1.255 scope global enp2s0
       valid_lft forever preferred_lft forever
    inet6 fe80::6e62:6dff:fe73:e64a/64 scope link
       valid_lft forever preferred_lft forever

 

後記:

查詢一個包是否被安裝 
# rpm -q < rpm package name>

列出所有被安裝的rpm package 
# rpm -qa

[root@localhost sbin]# rpm -qa

 lsof 安裝 查詢端口啓用情況 可以查詢 nginx是否啓用

具體使用

[root@localhost sbin]# lsof -i:80
COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   13536   root    6u  IPv4  32434      0t0  TCP *:http (LISTEN)
nginx   13548 nobody    6u  IPv4  32434      0t0  TCP *:http (LISTEN)

 

[root@localhost sbin]# yum -y install lsof
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.btte.net
 * extras: mirrors.hustunique.com
 * updates: mirrors.skyshe.cn
正在解決依賴關係
--> 正在檢查事務
---> 軟件包 lsof.x86_64.0.4.87-4.el7 將被 安裝
--> 解決依賴關係完成

依賴關係解決

================================================================================
 Package         架構              版本                   源               大小
================================================================================
正在安裝:
 lsof            x86_64            4.87-4.el7             base            331 k

事務概要
================================================================================
安裝  1 軟件包

總下載量:331 k
安裝大小:927 k
Downloading packages:
lsof-4.87-4.el7.x86_64.rpm                                 | 331 kB   00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安裝    : lsof-4.87-4.el7.x86_64                                      1/1
  驗證中      : lsof-4.87-4.el7.x86_64                                      1/1

已安裝:
  lsof.x86_64 0:4.87-4.el7

完畢!

使用vi命令 

不保存退出:q!

保存退出:wq

插入數據    i

跳轉到命令行   比如插入修改數據完成後 ESC 後輸入 :wq  保存退出

 

[root@localhost /]# find -name nginx  查看根目錄下的所有nginx
./usr/etc/ngin/sbin/nginx
./usr/local/bin/nginx-1.7.9/objs/nginx
[root@localhost /]# yum remove nginx  嘗試 刪除默認yum安裝的nginx
已加載插件:fastestmirror
參數 nginx 沒有匹配
不刪除任何軟件包

 

查詢80端口 

netstat -ano | grep 80  沒有就先安裝
yum -y install net-tools

[root@localhost sbin]# yum -y install net-tools
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.btte.net
 * extras: mirrors.hustunique.com
 * updates: mirrors.skyshe.cn
正在解決依賴關係
--> 正在檢查事務
---> 軟件包 net-tools.x86_64.0.2.0-0.17.20131004git.el7 將被 安裝
--> 解決依賴關係完成

依賴關係解決

================================================================================
 Package         架構         版本                             源          大小
================================================================================
正在安裝:
 net-tools       x86_64       2.0-0.17.20131004git.el7         base       304 k

事務概要
================================================================================
安裝  1 軟件包

總下載量:304 k
安裝大小:917 k
Downloading packages:
net-tools-2.0-0.17.20131004git.el7.x86_64.rpm              | 304 kB   00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安裝    : net-tools-2.0-0.17.20131004git.el7.x86_64                   1/1
  驗證中      : net-tools-2.0-0.17.20131004git.el7.x86_64                   1/1

已安裝:
  net-tools.x86_64 0:2.0-0.17.20131004git.el7

完畢!
[root@localhost sbin]# netstat -ano | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      off (0.00/0/0)
tcp        0      0 192.168.1.103:38036     114.247.56.117:80       TIME_WAIT   timewait (0.00/0/0)
unix  3      [ ]         STREAM     CONNECTED     16806    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     20180
unix  3      [ ]         STREAM     CONNECTED     16805

 最後的最後 居然是防火牆的問題 

CentOS 7.0默認使用的是firewall作爲防火牆

關閉防火牆 

systemctl stop firewalld.service #停止firewall

在IP 訪問一切正常

systemctl disable firewalld.service #禁止firewall開機啓動

 

[root@localhost init.d]# systemctl start firewalld.service 開啓防火牆firewall
[root@localhost init.d]# firewall-cmd --state  查看防火牆firewall運行狀態
running

啓動後增加端口 通過規則

[root@localhost init.d]# firewall-cmd --add-port=80/tcp
success

再次訪問 http://192.168.1.103/ 正常

 

 

 

查看firewall 運行情況

[root@localhost init.d]# firewall-cmd --state
not running

 參考文章

firewall防火牆的介紹 上面的 添加端口 也是這裏看到的~

http://linoxide.com/firewall/firewalld-centos-rhel/?utm_source=tuicool


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章