虚拟机下linux系统安装nginx

时间:2024-01-23 17:15:18

近在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 
-rw-r--r-- 1 root root 805253 Apr  8  2015 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.6.3]# pwd
/nginx-1.6.3
复制代码

 

4.创建nginx用户 

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

 

5.配置、编译、安装

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

 

6.启动nginx

复制代码
[root@nginx nginx-1.6.3]# /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.6.3]# /usr/local/sbin/nginx
[root@nginx nginx-1.6.3]# netstat -lntup | grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      3556/nginx          
[root@nginx nginx-1.6.3]# 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的整个流程,网上类似的教程很多,我也只是亲身实验,整理了出来。