玖叶教程网

前端编程开发入门

企业十大应用-nginx-部署(nginx软件)

Nginx简介

Nginx是一个web服务器也可以用来做负载均衡及反向代理使用,目前使用最多的就是负载均衡,具体简介我就不介绍了百度一下有很多,下面直接进入安装步骤

Nginx安装

  • 下载Nginx及相关组件
  • Linux系统是Centos 6.5 64位,我直接切换到root用户下安装
  • 进入用户目录下载程序
  • 下载相关组件
[root@localhost src]# wget http://nginx.org/download/nginx-1.10.2.tar.gz
[root@localhost src]# wget http://www.openssl.org/source/openssl-fips-2.0.10.tar.gz
[root@localhost src]# wget http://zlib.net/zlib-1.2.11.tar.gz
[root@localhost src]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz
  • 安装c++编译环境,如已安装可略过
[root@localhost src]# yum install gcc-c++
期间会有确认提示输入y回车
Is this ok [y/N]:y
  • 安装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

启动Nginx

  • 到安装目录
  • 进入nginx目录并启动
  • 报错了,error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory,按照下面方式解决
1.用whereis libpcre.so.1命令找到libpcre.so.1在哪里
2.用ln -s /usr/local/lib/libpcre.so.1 /lib64命令做个软连接就可以了
3.用sbin/nginx启动Nginx
4.用ps -aux | grep nginx查看状态
[root@localhost nginx]# whereis libpcre.so.1
[root@localhost nginx]# ln -s /usr/local/lib/libpcre.so.1 /lib64
[root@localhost nginx]# sbin/nginx
[root@localhost nginx]# ps -aux | grep nginx 
  • 进入Linux系统的图形界面,打开浏览器输入localhost会看到下图,说明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

简单配置Nginx

  • 打开nginx配置文件位于nginx目录下的conf文件夹下
  • 此处我们先暂时设置三个站点
  • 分别使用不同的端口80、81、82保存退出并且重启nginx

开启外网访问

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

  • 方法一直接关闭防火墙,这样性能较好,但安全性较差,如果有前置防火墙可以采取这种方式
关闭防火墙
[root@localhost ~]# service iptables stop
[root@localhost ~]# chkconfig iptables off
[root@localhost ~]# chkconfig --list|grep ipt
  • 方法二将开启的端口加入防火墙白名单中,这种方式较安全但性能也相对较差
编辑防火墙白名单
[root@localhost ~]# vim /etc/sysconfig/iptables
增加下面一行代码
-A INPUT -p tcp -m state -- state NEW -m tcp --dport 80 -j ACCEPT
保存退出,重启防火墙
[root@localhost ~]# service iptables restart
  • Linux配置完毕了,使用另一台电脑而非安装nginx的电脑,我是用的windows系统,配置一下host在“C:\Windows\System32\drivers\etc”下的hosts中配置一下域名重定向
10.11.13.22 nginx.test.com nginx.test1.com nginx.test2.com
  • 然后cmd再ping一下这个域名是否正确指向了这个IP上
  • 正确指向后在telnet一下80端口看一下是否可以与端口通信(如果telnet提示没有此命令是没有安装客户端,在启用或禁用windows功能处安装后再操作即可)
  • 得到以下界面及代表通信成功
  • 打开这台Windows系统内的浏览器,输入nginx.test.com会得到以下结果,就说明外网访问成功

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

到此Nginx服务器雏形部署完成。

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言