Centos7安装配置nginx

Posted 不知名架构师

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Centos7安装配置nginx相关的知识,希望对你有一定的参考价值。

1.前言

在进行nginx安装前,考虑到本博客针对很多新手朋友我决定还是先介绍一些nginx知识,这样更加有利于各位读者朋友对nginx的理解,对后续学习也有很大帮助。

介绍

Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了 IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的 Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004 年10月4日。 其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、简单的配 置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。 Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3) 代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实 上nginx的并发能力在同类型的网页服务器中表现较好,中国大陆使用nginx网站 用户有:百度、京东、新浪、网易、腾讯、淘宝等。

版本

常用的nginx版本分为四大阵营,小编这里就给大家简单介绍

1. Nginx开源版(我们将要安装的版本) 官网链接

干净纯粹,而且还是免费的,是我们将要用来学习的版本

2.Nginx plus 商业版 官网链接

在开源版本的基础上进项强化,增加功能,当然体积也更大了,但是提供了很多好用的功能,尤其是对微服务的整合,和云原生的整合,还有现在比较流行的K8s,都有一个比较好的整合效果,当然这个是商业版本需要付费的。

3.Openresty 官网链接

这是一个免费开源的版本,专栏后续会讲解到,感兴趣的朋友不妨点个关注。这个版本主要是把nginx和lua脚本进行一个完美的整合,在拥有高性能的同时,又没有增加开发的难度。而且官网是中文的对于我们国人学习起来也会方便很多。

4.Tengine 官网链接

这也是一个免费开源的nginx版本,目前比较主流。以模块化的方式增强,性能高,更安全,更稳定。同时官网也是中文的,更多详细信息大家可以参考官网。后续专栏也会更新到,有兴趣的读者朋友可以点个订阅

2.安装Nginx

本篇博客是基于专栏上一篇博客搭建的环境进行安装教程,有兴趣的读者朋友可以看一下我的上一篇博客,讲解虚拟化,虚拟机的运行构架,同时也详细记录了虚拟机安装centos的详细过程。博客链接

安装方法

本篇博客演示的nginx的安装方法是 源码编译安装

1.源码编译安装

在安装nginx之前我们需要准备好安装nginx的安装环境,下面的安装环境根据个人系统环境而定,没有安装的要进行安装。

(1).首先安装nginx要有gcc的环境,可以使用以下命令进行安装。

$ yum install -y gcc

gcc 可以编译 C,C++,Ada,Object C和Java等语言(安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境)

如果没有安装gcc环境直接进行安装nginx会报以下错误

checking for OS
 + Linux 3.10.0-693.el7.x86_64 x86_64
 checking for C compiler ... not found 
 ./configure: error: C compiler cc is not found

(2).还有第三方库 pcre-devel 可以用以下命令进行安装

$ yum install -y pcre pcre-devel

PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。
注: pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。

如果没有安装 pcre pcre-devel 直接进行安装nginx会报以下错误

./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib 
library statically from the source with nginx by using --with-zlib=<path> option.

(3).还有第三方库 zlib 可以用以下命令进行安装

$ yum install -y zlib zlib-devel

zlib-devel zlib库提供了很多种压缩和解压缩方式nginx使用zlib对http包的内容进行gzip,所以需要安装

安装好上面的环境我们就可以开始进行nginx的安装了

(1)下载包

1.可以在nginx的官网下载nginx的安装包 下载地址 下载后上传到虚拟机
2.也可以使用 wget 使用以下命令进行下载
3.这里小编给大家提供了一个nginx安装包 点击跳转百度网盘 下载后上传到虚拟机

// 使用前要确保系统已经安装了wget
// 如果没有则执行 yum -y install wget 进行安装
$ wget https://nginx.org/download/nginx-1.21.6.tar.gz

(2)解压安装

使用以下命令对下载好的压缩包进行解压

$ tar zxvf nginx-1.21.6.tar.gz

再使用以下命令进入到解压好的目录中

$ cd nginx-1.21.6

然后再使用以下命令开始安装nginx

$ ./configure --prefix=/usr/local/nginx  // /usr/local/nginx 指定的安装目录

再执行以下两条命令进行编译

$ make
$ make install

(3) 启动ngxin

使用以下命令进入到安装目录的sbin目录

$ cd /usr/local/nginx/sbin

然后就可以使用以下命令进行ngxin的操作

$ ./nginx					    //启动nginx
$ ./nginx -s stop			 	//停止nginx
$ ./nginx -s quit 			    //优雅的关闭,在退出前完成已经接受的连接请求
$ ./nginx -s reload 			// 重新加载nginx配置

(4) 启动和端口问题

因为nginx启动需要占用tcp协议的80端口, 这样我们在浏览器才能通过虚拟机ip地址访问ngin的服务,所以我们要保证防火墙开放了虚拟机的80端口,以下命令可以以对防火墙进行操作

$ firewall-cmd --state  // 查看防火墙状态
$ systemctl stop firewalld.service  // 关闭防火墙(不推荐使用)
$ systemctl disable firewalld.service  // 阻止防火墙开机启动
$ firewall-cmd --reload // 重启防火墙
$ firewall-cmd --zone=public --list-ports // 查看防火墙所有开放的端口
$ firewall-cmd --permanent --zone=public --remove-port=80/tcp // 关闭80端口

在启动nginx之前 我们要执行以下两条命令

$ firewall-cmd --zone=public --add-port=80/tcp --permanent  // 放行防火墙80端口(–permanent永久生效,没有此参数重启后失效)
$ firewall-cmd --reload // 重启防火墙

之后我们就可以使用以下命令启动ngxin服务

$ ./nginx  // 启动nginx服务 后台启动

(5) 一些常用命令

$ ./nginx					    // 启动nginx
$ ./nginx -s stop			 	// 停止ngxin
$ ./nginx -s quit 			    // 优雅的关闭nginx,在退出前完成已经接受的连接请求
$ ./nginx -s reload 			// 重新加载ngxin配置
$ ./nginx -s reopen             // 打开nginx日志文件
$ ./nginx -t                    // 检测配置文件是否有语法错误
$ ./nginx -v                    // 显示版本信息
$ ./nginx -c [配置文件路径]      // 指定配置文件
$  killall nginx                // 杀死所有nginx的进程
$ ps -aux | grep nginx          // 查看nginx的进程

接下来我们就可以使用虚拟机ip地址访问ngxin的默认页面(占用80端口)
可以看到nginx的默认页面已经呈现在浏览器

3.将nginx安装成系统服务

上面我们只是将nginx启动,当我们重启虚拟机后nginx将会是关闭状态,且为了简化我们启动nginx的步骤,我们可以将nginx安装成系统服务这样不仅可以设置开机启动,而且给我们操作nginx带来了极大的便利。

(1) 创建服务脚本

使用以下命令在/usr/lib/systemd/system下创建并编辑nginx的服务脚本

$ vi /usr/lib/systemd/system/nginx.service

将以下内容添加到刚刚创建好的脚本中 注意路径

[Unit]
Description=nginx - web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

添加完后建议执行以下命令设置一下配置文件的权限

$ chmod 755 /usr/lib/systemd/system/nginx.service

修改好后执行以下命令重新加载一下系统服务

$ systemctl daemon-reload

使用以下命令启动我们刚刚安装好的nginx服务

$ systemctl start nginx.service

将创建好的服务设置为开机启动

$ systemctl enable nginx.service

这样即使我们重启虚拟机,这个nginx服务也会自动打开。

建议此时将虚拟机重启一下 进行测试

(2) 常用命令

$ systemctl start nginx.service // 启动nginx服务
$ systemctl stop nginx.service  // 停止nginx服务
$ systemctl restart nginx.service // 重启nginx服务
$ systemctl reload nginx.service // 重载nginx服务 (推荐使用)
$ systemctl status nginx.service  // 查看nginx状态
$ systemctl enable nginx.service // 设置为开机启动nginx服务

4.一些小bug

当小编安装好nginx后,重启虚拟机,竟然发现远程连接工具连接不上了,使用ip addr 命令发现没有分配到ip ,但是小编之前是配置过网卡配置文件的。 小编相关博客
于是小编使用以下命令进入到网卡配置文件目录中

$ cd /etc/sysconfig/network-scripts

使用 ls 命令 发现竟然多了一个配置文件(果然如我所料) 多了个 ifcfg-ens33.BCK4m_ 的网卡配置文件

于是小编使用以下命令删除这个多余的配置文件

$ rm -f ifcfg-ens33.BCK4m_

再使用以下命令重启一下虚拟机网络服务

$ systemctl restart network

再使用 ip addr 命令 发现网络服务已经恢复正常了 小小bug也敢跟小编动手 这时查看nginx的状态也正常

5.结尾

这是小编的第四篇博客,感觉写博客的过程也学到了很多东西,而且还有一种成就感,确实是一种学习的好方法。推荐各位读者朋友也可以试试,关于文章最后的那个小bug小编并没有去深入研究,要是有知道为什么会有这个bug的读者朋友可以在评论区指出来大家一起学习成长,目前写的都是一些比较基础的博客。相信后续会给大家带来更好更优质的文章,期待的话不妨给小编点个订阅,谢谢大家!!!

CentOS7安装及配置Nginx服务

Nginx是CentOS中最常用的HTTP服务程序之一,以下针对CentOS7下Nginx二进制包安装、编译安装两个场景分别予以说明。


一、二进制包安装方式


在安装CentOS7时选择同时安装Nginx或者后来通过yum命令安装Nginx应用,得到的Nginx的安装路径、默认配置都是一样的,并且已经配置好Nginx服务,可通过如命令查看Nginx相关进程:

ps -ef | grep nginx

并可通过systemctl xx命令管理Nginx服务(xx命令可以是:enable-开机自动启动,disable-开机不自动启动,start-启动,status-查看状态,stop-停止),例如启动Nginx服务:

systemctl start nginx

查看版本,执行:

nginx -V

查看Nginx的服务信息,包括二进制、配置文件路径,执行如下命令:

systemctl status nginx

结果显示:

● nginx.service - The nginx HTTP and reverse proxy server   
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)   
Active: active (running) since Wed 2018-07-04 22:13:33 CST; 50min ago  
Process: 964 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)  
Process: 914 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)  
Process: 909 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS) 
Main PID: 966 (nginx)   CGroup: /system.slice/nginx.service           
├─966 nginx: master process /usr/sbin/nginx           
├─967 nginx: worker process           
└─968 nginx: worker process
Jul 04 22:13:33 centos.vm0 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jul 04 22:13:33 centos.vm0 nginx[914]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Jul 04 22:13:33 centos.vm0 nginx[914]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Jul 04 22:13:33 centos.vm0 systemd[1]: Started The nginx HTTP and reverse proxy server.

只查看配置文件路径,可通过命令:

nginx -t

结果显示:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful


二、编译安装方式


1、准备源码包到服务器目录

可从Nginx的网站(http://nginx.org/en/download.html) 下载与当前CentOS7配套的源码包(例如nginx-1.14.0.tar.gz),然后上传到CentOS7服务器,并解压,例如:

tar -zxvf nginx-1.14.0.tar.gz

2、检查gcc

执行如下命令:

gcc --version

如果报错,说明未安装gcc,需先通过如下命令安装gcc

yum install gcc

3、检查PCRE

进入到解压后的源码目录:

cd nginx-1.14.0/

执行configure命令:

./configure

如果configure失败,如果是缺少目录,需根据目录名并补充创建缺少的目录,如果是缺少PCRE库,通过如下命令安装PCRE依赖库:

yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel

4、正常编译及安装Nginx

在解压后的源码目录,为了确保执行正常,请以root身份依次执行如下命令:

./configure
make
make install

以上操作后Nginx将被安装在/usr/local/nginx目录下

5、普通方式启动Nginx程序

进入/usr/local/nginx/sbin目录下:

cd /usr/local/nginx/sbin

执行:

./nginx -c /usr/local/nginx/conf/nginx.conf

注:上述可选参数-c后面指定了/usr/local/nginx/conf/nginx.conf为配置文件

6、普通方式停止Nginx程序

进入/usr/local/nginx/sbin目录后,如立即停止Nginx并退出,可执行:

./nginx -s stop

如优雅停止Nginx并退出,可执行

./nginx -s quit

7、重新加载nginx.conf配置文件

进入/usr/local/nginx/sbin目录后,执行:

./nginx -s reload

8、将Nginx配置服务可以通过systemctl操作的服务

CentOS7的服务systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户(user)之分,需要开机不登陆就能运行的程序,存在系统服务里,即:/usr/lib/systemd/system目录下.
CentOS7的每一个服务以.service结尾,一般会分为3部分:[Unit]、[Service]和[Install],对Nginx服务,通过vi命令打开服务配置:

vi /usr/lib/systemd/system/nginx.service

配置文件nginx.service的内容示例如下:

[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
#  
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginxExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target


建议在修改之前,如果旧的文件已经存在,可以先做好备份,例如:

cp /usr/lib/systemd/system/nginx.service /usr/local/nginx/nginx.service.bak

之后将nginx.service内容中的路径修改为正确的路径即可,例如:

[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
#  
ExecStartPre=/usr/bin/rm -f /usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUITTimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]WantedBy=multi-user.target

注:pid文件的路径,在编译安装的情况下,默认位置一般为/usr/local/nginx/logs/nginx.pid(即nginx.conf中不设置pid路径时的情况),在二进制包安装方式下,默认位置一般为/run/nginx.pid(相应的nginx.conf中有一行pid /run/nginx.pid;),这个位置在/usr/lib/systemd/system/nginx.service中配置需与nginx.conf文件中的保持一致;

之后需要检查Nginx目录下的相关权限:

[[email protected] hnl]# cd /usr/local/nginx
[[email protected] nginx]# ls
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
[[email protected] nginx]# ll
total 4
drwx------. 2 nobody root    6 Jul  4 22:43 client_body_temp
drwxr-xr-x. 2 root   root 4096 Jul  5 00:45 conf
drwx------. 2 nobody root    6 Jul  4 22:43 fastcgi_temp
drwxr-xr-x. 2 root   root   40 Jul  4 22:43 html
drwxr-xr-x. 2 root   root   58 Jul  5 01:11 logs
drwx------. 2 nobody root    6 Jul  4 22:43 proxy_temp
dr-xr-xr-x. 2 root   root   19 Jul  4 22:43 sbin
drwx------. 2 nobody root    6 Jul  4 22:43 scgi_temp
drwx------. 2 nobody root    6 Jul  4 22:43 uwsgi_temp
[[email protected] nginx]# ll sbin
total 3660
-rwxr-xr-x. 1 root root 3746616 Jul  4 22:43 nginx

为了配置服务自动启动,执行

sysemctl enable nginx

然后重启CentOS,重启完成后,参考一中的命令检查Nginx的运行情况:

ps -ef |grep nginx
systemctl status nginx



以上是关于Centos7安装配置nginx的主要内容,如果未能解决你的问题,请参考以下文章

centos7 nginx+php5.6+mysql安装与配置

CentOS7.2编译安装LNMP

服务器运维及部署

linux centos7 nginx 安装部署和配置

nginx 安装与配置

CentOS7.2下安装php加速软件Xcache