nginx 菜鸟的自我进化

虽然一直听说,但是一直没在项目中用过(大部分时候是其他人已经配置好了),于是乎竟然说不出来 nignx 究竟是个神马东东。

Nginx 安装

1、下载 Nginx 及相关组件

[root@master packages]# cd nginx-1.18.0
[root@master nginx-1.18.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  objs  README  src
[root@master nginx-1.18.0]# 

安装 c++ 编译环境,如已安装可略过

[root@localhost src]# yum install gcc-c++
省略安装内容...
期间会有确认提示输入y回车
Is this ok [y/N]:y

2、安装 Nginx 及相关组件

openssl安装

[root@localhost src]# tar zxvf openssl-fips-2.0.10.tar.gz
省略安装内容...
[root@localhost src]# cd openssl-fips-2.0.10
[root@localhost openssl-fips-2.0.10]# ./config && make && make install
省略安装内容...

pcre安装

[root@localhost src]# tar zxvf pcre-8.40.tar.gz
省略安装内容...
[root@localhost src]# cd pcre-8.40
[root@localhost pcre-8.40]# ./configure && make && make install
省略安装内容...

zlib安装

[root@localhost src]# tar zxvf zlib-1.2.11.tar.gz
省略安装内容...
[root@localhost src]# cd zlib-1.2.11
[root@localhost zlib-1.2.11]# ./configure && make && make install
省略安装内容...

nginx安装

[root@localhost src]# tar zxvf nginx-1.10.2.tar.gz
省略安装内容...
[root@localhost src]# cd nginx-1.10.2
[root@localhost nginx-1.10.2]# ./configure && make && make install
省略安装内容...

3、启动 Nginx

先找一下 nginx 安装到什么位置上了

[root@master nginx-1.18.0]# whereis nginx
nginx: /usr/local/nginx

进入 nginx 目录并启动

[root@master nginx-1.18.0]# cd /usr/local/nginx/
[root@master nginx]# sbin/nginx 

打开浏览器输入 localhost 会看到下图(我 nginx 是在虚拟机启动的,所以我输入的虚拟机的静态局域网 ip),说明 nginx 启动成功

nginx 的基本操作

启动
[root@localhost ~]# /usr/local/nginx/sbin/nginx
停止/重启
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop(quit、reload)
命令帮助
[root@localhost ~]# /usr/local/nginx/sbin/nginx -h
验证配置文件
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

4、简单配置 Nginx

打开 nginx 配置文件位于nginx目录下的 conf 文件夹下

[root@master nginx]# pwd
/usr/local/nginx
[root@master nginx]# cd conf/
[root@master conf]# vim nginx.conf

使用不同的端口 81 保存退出并且重启 nginx

...

    server {
        listen          81;
        server_name     nginx.test.com;
        location / {
            root        html;
            index       index.html index.htm;
        }
    }

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

5、开启外网访问

在 Linux 系统中默认有防火墙 Iptables 管理者所有的端口,只启用默认远程连接 22 端口,其他都关闭,咱们上面设置的 80 等等也是关闭的,所以我们需要先把应用的端口开启。

自己虚拟机测试的时候可以直接关闭防火墙,这样性能较好,但安全性较差,如果有前置防火墙可以采取这种方式:

[root@master nginx]# service iptables stop
Redirecting to /bin/systemctl status iptables.service
Unit iptables.service could not be found.

在 centos7 下如果有 Unit iptables.service could not be found. 报错的话记得先安装 iptables-services。

[root@master nginx]# yum install iptables-services
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: mirrors.bfsu.edu.cn
[root@master nginx]# service iptables stop
Redirecting to /bin/systemctl stop iptables.service

下面是防火墙的其他操作命令:

[root@master nginx]# service iptables start
[root@master nginx]# service iptables stop
[root@master nginx]# service iptables restart
[root@master nginx]# service iptables status

centos7 以后的话最好是用 systemctl
[root@slave nginx]# systemctl start firewalld
[root@slave nginx]# systemctl stop firewalld

Linux 配置完毕了,使用另一台电脑而非安装 nginx 的电脑,我是用的 windows 系统,配置一下 host 在“C:\Windows\System32\drivers\etc”下的 hosts 中配置一下域名重定向:

192.168.128.139 nginx.test.com

然后在 cmd 再 ping 一下这个域名是否正确指向了这个 IP 上:

C:\Users\looking>ping nginx.test.com

正在 Ping nginx.test.com [192.168.128.139] 具有 32 字节的数据:
来自 192.168.128.139 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.128.139 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.128.139 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.128.139 的回复: 字节=32 时间<1ms TTL=64

192.168.128.139 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 0ms,最长 = 0ms,平均 = 0ms

正确指向后在 telnet 一下 81 端口看一下是否可以与端口通信(如果 telnet 提示没有此命令是没有安装客户端,在启用或禁用 windows 功能处安装后再操作即可)

C:\Users\lookinf>telnet 192.168.128.139 81 

得到以下界面及代表通信成功

打开这台 Windows 系统内的浏览器,输入 nginx.test.com 会得到以下结果,就说明访问成功。

如果防火墙你依然启用,只是设置了启用端口,那我们访问 81 那个端口会发现无法访问,因为我并没有加入白名单

到此 Nginx 服务器菜鸟教程部署完成。

猜你喜欢

转载自blog.csdn.net/TomorrowAndTuture/article/details/111224672