nginx源码安装及添加系统服务—CentOS7.6生产环境下的微服务部署(五)

1. nginx源码安装安装

1.1 安装所需插件

1.1.1 gcc安装

nginx 源码安装依赖 gcc 环境,如果没有 gcc 环境,则需要安装:

yum install gcc-c++
1.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
1.1.3 zlib安装

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

yum install -y zlib zlib-devel
1.1.4 openssl安装

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

yum install -y openssl openssl-devel

1.2 下载安装包

1.2.1 下载1.18.0安装包
wget -c https://nginx.org/download/nginx-1.18.0.tar.gz
1.2.2 将安装包解压到/usr/local文件夹
tar -zxvf nginx-1.18.0.tar.gz -C /usr/local
cd /usr/local/nginx-1.18.0
1.2.3 配置、编译、安装
./configure
make && make install
1.2.4 查看nginx安装地址
whereis nginx
1.2.5 启动、停止、重启
cd /usr/local/nginx/sbin/
./nginx 
./nginx -s stop
./nginx -s reload
1.2.6 查询nginx进程,删除安装包
ps -ef|grep nginx
rm -rf /usr/local/nginx-1.18.0

2. 添加系统服务

2.1 添加nginx的系统服务文件

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

2.2 nginx.service内容

[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.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 stop

[Install]
WantedBy=multi-user.target

2.3 nginx.service内容解释

[Unit]:服务的说明
Description:描述服务
After:描述服务类别

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

2.4 使文件生效

systemctl daemon-reload

2.5 常用命令

2.5.1 设置开机自启动
systemctl enable nginx.service
2.5.2启动nginx服务
 systemctl start nginx.service
2.5.3 停止开机自启动
systemctl disable nginx.service
2.5.4 查看服务当前状态
systemctl status nginx.service
2.5.5 重新启动服务
systemctl restart nginx.service
2.5.6 查看所有已启动的服务
systemctl list-units --type=service

2.6 命令集合

systemctl is-enabled servicename.service #查询服务是否开机启动
systemctl enable *.service #开机运行服务
systemctl disable *.service #取消开机运行
systemctl start *.service #启动服务
systemctl stop *.service #停止服务
systemctl restart *.service #重启服务
systemctl reload *.service #重新加载服务配置文件
systemctl status *.service #查询服务运行状态
systemctl --failed #显示启动失败的服务

注:*代表某个服务的名字,如http的服务名为httpd

你可能感兴趣的:(nginx源码安装及添加系统服务—CentOS7.6生产环境下的微服务部署(五))