Linux CentOS7下Nginx安装与配置

1.安装环境

  1. CentOS7
  2. Nginx版本 nginx-1.15.8.tar.gz

2.安装

2.1 安装环境

2.1.1 gcc安装

安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装,命令:

yum install gcc-c++

2.1.2 PCRE pcre-devel 安装

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

yum install -y pcre pcre-devel

2.1.3 zlib 安装

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

yum install -y zlib zlib-devel

2.1.4 OpenSSL 安装

 OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。
 

yum install -y openssl openssl-devel

2.2 nginx安装包下载

一下提供两种方式:

2.2.1 官网下载

http://nginx.org/en/download.html

下载后通过ftp工具上传到linux上。

2.2.2 使用wget命令下载

wget -c https://nginx.org/download/nginx-1.15.8.tar.gz

 如下:

2.3 nginx安装

2.3.1 将上面的压缩包解压缩,命令:

tar -zxvf nginx-1.15.8.tar.gz

 

 解压结束后

2.3.2 配置、编译安装

cd到nginx-1.15.8下 这里使用默认配置,使用组合命令,如下:

./configure && make && make install

 

等待安装完成后通过whereis nginx命令查看安装路径

whereis nginx

 

然后cd到/usr/local/nginx/sbin目录下,通过./nginx命令开启nginx服务

可以通过 ps -ef | grep nginx 命令来检测系统是否开启了nginx线程

./nginx

 

nginx其他命令:

#开启nginx服务
./nginx 
#待nginx进程处理任务完毕进行停止
./nginx -s quit
#相当于先查出nginx进程id再使用kill命令强制杀掉进程
./nginx -s stop
#重启
./nginx -s reload

 查看nginx是否安装成功用curl http://127.0.0.1

curl http://127.0.0.1


 如果外网访问时,访问不到,请继续以下 外网访问 步骤


3. 外网访问

nginx搭建好后,向外抛出80端口,通过 firewall-cmd --query-port=80/tcp 来查看80端口是否开启,每开启时候为:no

执行下面命令:

//允许某端口放行
firewall-cmd --permanent --add-port=3389/tcp
//需要留意的是在编写完规则之后,要运行--reload参数
firewall-cmd --reload

 

成功之后页面:

下面提供一些辅助命令:

查询端口号80 是否开启:firewall-cmd --query-port=80/tcp

永久开放80端口号:firewall-cmd --permanent --zone=public --add-port=80/tcp

移除80端口号:firewall-cmd --permanent --zone=public --remove-port=80/tcp

--zone #作用域
--add-port=80/tcp  #添加端口,格式为:端口/通讯协议
--permanent   #永久生效,没有此参数重启后失效

查看防火墙状态
systemctl status firewalld.service
启动|关闭|重新启动  防火墙
systemctl [start|stop|restart] firewalld.service 

 4. 设置开机自启以及系统服务

centos 7以上是用Systemd进行系统初始化的,Systemd 是 Linux 系统中最新的初始化系统(init),它主要的设计目标是克服 sysvinit 固有的缺点,提高系统的启动速度。关于Systemd的详情介绍在https://www.ibm.com/developerworks/cn/linux/1407_liuming_init3

这里是用源码编译安装的,所以要手动创建nginx.service服务文件。
首先进入cd /lib/systemd/system

cd /lib/systemd/system/

 

然后创建nginx.service

vi nginx.service

编辑内容:

[Unit]
Description=nginx
After=network.target
  
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
  
[Install]
WantedBy=multi-user.target

 

ExecStart后面的参数为:用户安装nginx目录下的sbin下的nginx命令,前面有提到。

[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

保存退出。

4.1 开机启动命令

systemctl enable nginx.service

 此命令设置后如果systemctl restart nginx.service 等命令不能使用,请重启再试。

4.2 其他命令

#启动nginx服务
systemctl start nginx.service 
#关闭
systemctl stop nginx.service 
#重启
systemctl restart nginx.service 
#设置开机自启动
systemctl enable nginx.service
#停止开机自启动
systemctl disable nginx.service
#查看服务当前状态
systemctl status nginx.service
#查看所有已启动的服务
systemctl list-units --type=service

注意:以上操作步骤,视安装情况而定。 

猜你喜欢

转载自blog.csdn.net/it_erge/article/details/86490344