Linux CentOS7 under Nginx Installation and Configuration

1. Installation Environment

  1. CentOS7
  2. Nginx version nginx-1.15.8.tar.gz

2. Install

2.1 Installation Environment

2.1.1 gcc installed

We need to first install nginx official website to download the source code to compile, gcc compiler-dependent environment, if there is no gcc environment, you need to install the command:

yum install gcc-c++

Installation 2.1.2 PCRE pcre-devel

PCRE (Perl Compatible Regular Expressions) is a Perl library, including perl-compatible regular expression library. The module uses nginx http pcre regular expressions to parse, so it is necessary to install pcre library on linux, pcre-devel pcre developed using a secondary development library. nginx also need this library. command:
 

yum install -y pcre pcre-devel

2.1.3 zlib installation

zlib library provides a variety of compression and decompression of the way, nginx using zlib the contents of the package are http gzip, so you need to install on Centos zlib library.

yum install -y zlib zlib-devel

2.1.4 OpenSSL installation

 OpenSSL is a powerful Secure Sockets Layer cryptographic library cryptographic algorithms include major, commonly used key and certificate management and SSL protocol encapsulation, and provides a wealth of applications for testing or other purposes.
nginx not only supports the http protocol, also supports https (http over ssl ie transmission protocol), so you need to install the OpenSSL library in Centos.
 

yum install -y openssl openssl-devel

 

2.2 nginx installation package download

It provides two ways:

2.2.1 official website to download

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

After downloading via ftp upload to linux tool.

2.2.2 Use wgetcommand to download

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

 as follows:

2.3 nginx installation

2.3.1 The above compression decompressed packet, the command:

tar -zxvf nginx-1.15.8.tar.gz

 

 After extracting the end

2.3.2 configure, compile and install

cd to nginx-1.15.8 default configuration used herein, a combination of the command, as follows:

./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

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

 

 

Guess you like

Origin blog.csdn.net/it_erge/article/details/86490344