Nginx的安装与详细配置
环境
环境:centos7
用户:okwzh
用户路径:/home/okwzh
prec路径:/home/okwzh/nginx/prec
nginx路径:/home/okwzh/nginx
nginx的安装
在安装nginx之前,我们需要知道安装nginx是需要安装相关的编译工具以及依赖
一. 安装编译工具以及依赖
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
二. 安装PCRE,使其支持rewrite功能
[root@localhost /]# cd /home/okwzh/nginx
# 下载pcre
[root@localhost nginx]# wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
# 解压pcre包
[root@localhost nginx]# tar -zxvf pcre-8.35.tar.gz
[root@localhost nginx]# cd pcre-8.35/
# 配置
[root@localhost pcre-8.35]# ./configure
# 编译安装
[root@localhost pcre-8.35]# make && make install
# 当然,你也可以写成 ./configure && make && make install 一样的效果
# 安装完成,查看安装版本
[root@localhost pcre-8.35]# pcre-config --version
8.35
三. 下载安装nginx
[root@localhost /]# cd /home/okwzh/nginx
# 下载nginx
[root@localhost nginx]# wget http://nginx.org/download/nginx-1.6.2.tar.gz
# 解压
[root@localhost nginx]# tar -zxvf nginx-1.6.2.tar.gz
[root@localhost nginx]# cd nginx-1.6.2
配置 配置分为两种,一种是自定义安装路径,一种是系统安装路径(/usr/local/nginx)
系统安装路径
# 系统安装路径
[root@localhost nginx-1.6.2]# ./configure
[root@localhost nginx-1.6.2]# make && make install
自定义安装路径
# 自定义安装路径
# –prefix=PATH 设置软件安装目录路径。
# –with-http_ssl_module启用HTTP_SSL模块,用于构建HTTPS服务。默认情况下不构建此模块。(使用https服务需要用到这个)
# –with-http_stub_status_module启用HTTP_Stub_Status模块,状态信息统计模块,用于返回一个Nginx状态信息统计信息页面,管理员访问这个页面可以获取Nginx的请求处理、当前连接、等待连接等统计信息,一般用于监控Nginx的运行状态。默认情况下不构建此模块。
# nginx安装默认会自动检测系统是否安装pcre,无需指定编译参数,如果系统没有pcre,会报错提示缺乏依赖库。
[root@localhost nginx-1.6.2]# ./configure --prefix=/home/okwzh/nginx/nginx-1.6.2 --with-http_stub_status_module --with-http_ssl_module --with-pcre=/home/okwzh/nginx/pcre-8.35/
[root@localhost nginx-1.6.2]# make && make install
一般情况下,到这里就已经安装成功了,我们进去sbin目录下,查看nginx版本
[root@localhost nginx-1.6.2]# cd sbin
[root@localhost sbin]# ./nginx -v
nginx version: nginx/1.6.2
[root@localhost sbin]# ./nginx -V
nginx version: nginx/1.6.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
TLS SNI support enabled
configure arguments: --prefix=/home/okwzh/nginx/nginx-1.6.2 --with-http_stub_status_module --with-http_ssl_module --with-pcre=/home/okwzh/nginx/pcre-8.35/pcre-8.35
当我们遇到如下这个问题的话,说明是我们的./configure配置命令出现了错误,是最后的pcre路径写错了
我们执行./nginx -t 检查nginx语法问题
[root@localhost sbin]# ./nginx -t
nginx: the configuration file /home/okwzh/nginx/nginx-1.6.2/conf/nginx.conf syntax is ok
nginx: configuration file /home/okwzh/nginx/nginx-1.6.2/conf/nginx.conf test is successful
# nginx配置成功,安装成功,配置文件没有问题
当遇到下面这个问题,是说明没有logs日志的文件夹,我们需要在sbin同级目录下新建logs文件夹
[root@localhost sbin]# cd ..
[root@localhost nginx-1.6.2]# mkdir logs
[root@localhost nginx-1.6.2]# ./sbin/nginx -t
nginx: the configuration file /home/okwzh/nginx/nginx-1.6.2/conf/nginx.conf syntax is ok
nginx: configuration file /home/okwzh/nginx/nginx-1.6.2/conf/nginx.conf test is successful
logs文件夹下的文件
四. 启动nginx服务,防火墙命令
至此,我们的nginx已经下载安装成功了,然后我们需要查看nginx的端口是否启动,nginx的默认端口是80,我们要启动这个端口才可以访问成功,这就需要查看centos的防火墙了
linux centos7 默认防火墙是关闭的
防火墙命令
# 查看防火墙状态
firewall-cmd --state
# 查看已开启的端口
firewall-cmd --list-all
firewall-cmd --list-ports
# 开启防火墙
systemctl start firewalld
# 开启80端口
sudo firewall-cmd --add-port=80/tcp --permanent
# 重启防火墙
firewall-cmd --reload
查看防火墙信息
# 查看防火墙信息
[root@localhost nginx-1.6.2]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: dhcpv6-client ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
#启动防火墙
[root@localhost nginx-1.6.2]# sudo firewall-cmd --add-port=80/tcp --permanent
Warning: ALREADY_ENABLED: 80:tcp
success
# 添加80端口
[root@localhost nginx-1.6.2]# sudo firewall-cmd --add-port=80/tcp --permanent
Warning: ALREADY_ENABLED: 80:tcp
success
# 重启防火墙
[root@localhost nginx-1.6.2]# firewall-cmd --reload
success
# 查看防火墙启动端口
[root@localhost nginx-1.6.2]# firewall-cmd --list-ports
80/tcp
这时我们的80端口已经开启,然后我们启动nginx服务
# 启动nginx服务
[root@localhost sbin]# ./nginx
现在已经启动nginx服务成功了,我们查看nginx服务
[root@localhost sbin]# ps -ef | grep nginx
然后我们查看ip,命令是 ifconfig
然后我们在浏览器输入地址:http://ip
发现是403 nginx,到此我们可以看到服务是启动了,但是还是有一点问题。
我们查看logs日志文件夹,cat error.log可以发现是权限问题
进入conf文件下,vim nginx.conf,修改user 改为root用户。
重启nginx服务,命令:./nginx -s reload
再次访问http://ip,服务启动成功
至此,nginx就安装配置成功了,服务业启动成功。完结撒花O(∩_∩)O哈哈~