原文地址:Nginx Linux详细安装部署教程css
1、Nginx简介html
Nginx是一个web服务器也能够用来作负载均衡及反向代理使用,目前使用最多的就是负载均衡,具体简介我就不介绍了百度一下有不少,下面直接进入安装步骤nginx
2、Nginx安装c++
一、下载Nginx及相关组件web
下载相关组件vim
[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++编译环境,如已安装可略过windows
[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安装到什么位置上了
[root@localhost src]# whereis nginx nginx: /usr/local/nginx
进入nginx目录并启动
[root@localhost src]#cd /usr/local/nginx [root@localhost nginx]# /sbin/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文件夹下
[root@localhost nginx]# vim conf/nginx.conf
保存退出而且重启nginx
[root@localhost nginx]# sbin/nginx -s reload
五、开启外网访问
在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会获得如下结果,就说明外网访问成功
到此Nginx服务器雏形部署完成。
六、Nginx负载均衡配置
Nginx集反向代理和负载均衡于一身,在配置文件中修改配就能够实现
首先咱们打开配置文件
[root@localhost nginx]# vim conf/nginx.conf 每个server就是一个虚拟主机,咱们有一个看成web服务器来使用 listen 80;表明监听80端口 server_name xxx.com;表明外网访问的域名 location / {};表明一个过滤器,/匹配全部请求,咱们还能够根据本身的状况定义不一样的过滤,好比对静态文件js、css、image制定专属过滤 root html;表明站点根目录 index index.html;表明默认主页
这样配置完毕咱们输入域名就能够访问到该站点了。
负载均衡功能每每在接收到某个请求后分配到后端的多台服务器上,那咱们就须要upstream{}块来配合使用
upstream xxx{};upstream模块是命名一个后端服务器组,组名必须为后端服务器站点域名,内部能够写多台服务器ip和port,还能够设置跳转规则及权重等等 ip_hash;表明使用ip地址方式分配跳转后端服务器,同一ip请求每次都会访问同一台后端服务器 server;表明后端服务器地址 server{};server模块依然是接收外部请求的部分 server_name;表明外网访问域名 location / {};一样表明过滤器,用于制定不一样请求的不一样操做 proxy_pass;表明后端服务器组名,此组名必须为后端服务器站点域名 server_name和upstream{}的组名能够不一致,server_name是外网访问接收请求的域名,upstream{}的组名是跳转后端服务器时站点访问的域名
配置一下Windows的host将咱们要访问的域名aaa.test.com指向Linux
Nginx的负载功能就配置完成了。