centos安装nginx

转载自https://www.cnblogs.com/mophy/p/8092794.html

一、Nginx简介

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

二、Nginx安装

1、下载Nginx及相关组件

Linux系统是Centos 6.8 64位,我直接切换到root用户下安装

centos安装nginx

进入用户目录下载程序

centos安装nginx

下载相关组件

centos安装nginx

[[email protected] src]# wget http://nginx.org/download/nginx-1.10.2.tar.gz
省略安装内容...
[[email protected] src]# wget http://www.openssl.org/source/openssl-fips-2.0.10.tar.gz
省略安装内容...
[[email protected] src]# wget http://zlib.net/zlib-1.2.11.tar.gz
省略安装内容...
[[email protected] src]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz
省略安装内容...

centos安装nginx

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

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

2、安装Nginx及相关组件

openssl安装

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

pcre安装

 

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

zlib安装

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

nginx安装

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

3、启动Nginx

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

[[email protected] src]# whereis nginx

进入nginx目录并启动

centos安装nginx

如果报错,error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory,按照下面方式解决

 

centos安装nginx

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查看状态

[[email protected] nginx]# whereis libpcre.so.1
[[email protected] nginx]# ln -s /usr/local/lib/libpcre.so.1 /lib64
[[email protected] nginx]# sbin/nginx
[[email protected] nginx]# ps -aux | grep nginx

centos安装nginx

进入Linux系统的图形界面,打开浏览器输入localhost会看到下图,说明nginx启动成功

centos安装nginx

nginx的基本操作

centos安装nginx

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

centos安装nginx

4、简单配置Nginx

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

PS:vim语法

centos安装nginx

默认vim打开后是不能录入的,需要按键才能操作,具体如下:
开启编辑:按“i”或者“Insert”键
退出编辑:“Esc”键
退出vim:“:q”
保存vim:“:w”
保存退出vim:“:wq”
不保存退出vim:“:q!”

centos安装nginx

"#"代表注释,最重要的是server{}块这部分就代表每一个web站点,详细的配置介绍可以查阅我的另一片配置文章,此处我们先暂时设置三个站点

centos安装nginx

分别使用不同的端口80、81、82保存退出并且重启nginx

centos安装nginx

5、开启外网访问

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

方法一直接关闭防火墙,这样性能较好,但安全性较差,如果有前置防火墙可以采取这种方式

关闭防火墙
[[email protected] ~]# service iptables stop
关闭开机自启动防火墙
[[email protected] ~]# chkconfig iptables off
[[email protected] ~]# chkconfig --list|grep ipt

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

centos安装nginx

方法二将开启的端口加入防火墙白名单中,这种方式较安全但性能也相对较差

编辑防火墙白名单
[[email protected] ~]# vim /etc/sysconfig/iptables
增加下面一行代码
-A INPUT -p tcp -m state -- state NEW -m tcp --dport 80 -j ACCEPT
保存退出,重启防火墙
[[email protected] ~]# service iptables restart

centos安装nginx

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上