虛擬機下linux系統安裝nginx


近在windows虛機下安裝nginx,也遇到部分問題,寫篇隨筆總結一下

一.安裝虛機

  windows下安裝虛擬機我就不說了,一搜一大把,一直下一步就ok了

二. 打開虛擬安裝nginx

1.選擇版本,下載nginx。下載地址:http://nginx.org/download/

[root@localhost  /]# wget  http://nginx.org/download/nginx-1.7.0.tar.gz

2.安裝pcre openssl  gcc庫及源碼包

[root@nginx /]# yum -y install pcre pcre-devel openssl openssl-devel gcc gcc-c++

3.解壓

[root@nginx /]# ll nginx-1.6.3.tar.gz 
[root@nginx /]# tar zxvf nginx-1.6.3.tar.gz
[root@nginx /]# cd nginx-1.6.3
[root@nginx nginx-1.7.0]# pwd
/nginx-1.7.0

 

4.創建nginx用戶 

[root@nginx nginx-1.7.0]# useradd nginx -s /sbin/nologin -M

 

5.配置、編譯、安裝

[root@nginx nginx-1.7.0]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
[root@nginx nginx-1.7.0]# echo $?
0
[root@nginx nginx-1.7.0]# make && make install
[root@nginx nginx-1.7.0]# echo $?
0
[root@nginx nginx-1.7.0]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin

 

6.啟動nginx

[root@nginx nginx-1.7.0]# /usr/local/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx nginx-1.7.0]# /usr/local/sbin/nginx
[root@nginx nginx-1.7.0]# netstat -lntup | grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      3556/nginx          
[root@nginx nginx-1.7.0]# lsof -i :80
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   3556  root    6u  IPv4  17544      0t0  TCP *:http (LISTEN)
nginx   3557 nginx    6u  IPv4  17544      0t0  TCP *:http (LISTEN)

 7.至此nginx啟動成功了,我們先在虛擬機中試一下

[root@nginx nginx-1.7.0]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

8.在外部的windows下訪問虛擬機中的nginx服務

[root@nginx nginx-1.7.0]# ifconfig

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.78.130 netmask 255.255.255.0 broadcast 192.168.78.255
inet6 fe80::3164:48be:dd5f:fa27 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:78:48:f2 txqueuelen 1000 (Ethernet)
RX packets 202596 bytes 299827475 (285.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 107108 bytes 6501152 (6.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 104 bytes 10607 (10.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 104 bytes 10607 (10.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:87:29:e0 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

紅色字體部分就是虛擬機的ip地址了,然后我在外部瀏覽器中訪問http://192.168.78.130,發現請求不到服務,原來是因為linux下防火牆的問題,默認使用的fireWall

9.禁用firewalld&開啟iptables&systemctl使用簡介

9.1.安裝

[root@localhost ~]# yum install iptables-services

9.2.屏蔽該服務

[root@localhost ~]# systemctl mask firewalld

# systemctl mask firewalld 屏蔽服務(讓它不能啟動)
# ln -s '/dev/null''/etc/systemd/system/firewalld.service'
# systemctl unmask firewalld 顯示服務(如 firewalld.service)
# rm '/etc/systemd/system/firewalld.service'

 

9.3.啟用iptables

[root@localhost ~]# systemctl enable iptables
#如果需要使用 ip6tables , 需另外加一行
[root@localhost ~]# systemctl enable ip6tables

 

9.4.啟動iptables,停止firewalld

#停止firewalld服務,開啟 iptables服務
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl start iptables
# 同上,如果需要使用 ip6tables , 需另外加一條
[root@localhost ~]# systemctl start ip6tables

 

最后在用外部windows瀏覽器訪問:http://192.168.78.130,bingo,可以了

以上就是我安裝nginx的整個流程,網上類似的教程很多,我也只是親身實驗,整理了出來。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM