linux中Nginx安装

编译安装

​ Nginx的优点太多,这里不再赘述,详情请看这篇博客深入理解nginx

​ Nginx的安装有rpm包安装、编译安装和docker安装。本文将介绍编译安装方式。

1. 安装环境准备

1.0 更新yum

yum -y update

1.1 安装GCC编译器:

(用于编译c、c++代码)

yum install -y gcc

1.2 安装G plus plus编译器

yum install -y g++

1.3 安装PCRE库(用来解析正则表达式)

(用c语言编写的正则表达式函数库))

yum install -y pcre pcre-devel

1.4 安装zlib库

(用于数据压缩的函式库))

yum install -y zlib zlib-devel

1.5 安装openssl库

(安全套接字层密码库))

yum install -y openssl openssl-devel

2. 下载安装

2.1 下载nginx

wget http://nginx.org/download/nginx-1.15.12.tar.gz

2.2 编译nginx

tar -zxvf nginx-1.15.12.tar.gz
cd nginx-1.15.12/
./configure
make
make install

2.3 查看安装位置

whereis nginx

2.4 启动nginx

cd /usr/local/nginx/
cd sbin/
./nginx

3.nginx操作

修改配置后重新加载生效
/usr/local/nginx/sbin/nginx -s reload
开启nginx
/usr/local/nginx/sbin/nginx
快速停止nginx
/usr/local/nginx/sbin/nginx -s stop
完整有序的停止nginx
/usr/local/nginx/sbin/nginx -s quit

: stop和quit的区别在于

quit是一个优雅的关闭方式,Nginx在退出前完成已经接受的连接请求

Stop 是快速关闭,不管有没有正在处理的请求。

4. nginx加入systemctl服务

nginx安装完成后不能被系统管理,而且开机不能自启,这里设置nginx开机自启。

为了方便,现将nginx服务添加至systemctl

4.1 关闭nginx服务

/usr/local/nginx/sbin/nginx -s stop

4.2 更改nginx.conf

#pid        logs/nginx.pid;
更改为
pid logs/nginx.pid;

注意:

  • 这个参数为nginx运行时的pid号,这个配置为将pid号存入nginx.pid文件中,接下来的配置需要用到这个参数,所以取消注释。

  • 注意要保持此处和下面的配置路径时相同的。

  • 此处的路径时相对路径

4.2 配置服务

在/usr/lib/systemd/system/新建nginx.service文件,vim编辑如下

vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance 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

注意:

[Unit]部分主要是对这个服务的说明,内容包括Description和After,Description用于描述服务,After用于描述服务类别

[Service]部分是服务的关键,是服务的一些具体运行参数的设置,这里Type=forking是后台运行的形式,PIDFile为存放PID的文件路径ExecStart为服务的具体运行命令ExecReload为重启命令ExecStop为停止命令,PrivateTmp=True表示给服务分配独立的临时空间,注意:[Service]部分的启动、重启、停止命令全部要求使用绝对路径,使用相对路径则会报错

[Install]部分是服务安装的相关设置,可设置为多用户的

服务脚本按照上面编写完成后,以754的权限保存在/usr/lib/systemd/system目录下

然后执行。

注意修改或新增文件需要执行以下语句才能生效

systemctl daemon-reload
  • PIDFile这个参数很重要,要保持nginx.conf和nginx.service路径相同,注意此处的PIDFile为绝对路径。

4.3 启动、关闭、重启nginx

开启:

systemctl start nginx.service

关闭:

systemctl stop nginx.service

重新加载

systemctl reload nginx.service

优雅退出:

systemctl quit nginx.service

开机自启:

systemctl enable nginx.service

查看状态:

systemctl status nginx.service

注意:

上述配置,如果出现下面问题,请仔细查看教程,检查nginx.service和nginx.conf两个PID文件的路径是否对照。

最新文章

  1. android The connection to adb is down 错误信息
  2. Linux Linux程序练习十(网络编程大文件发送)
  3. 解决:对 PInvoke 函数的调用导致堆栈不对称问题 <转载>
  4. PMP考试--成本管理中常用的概念
  5. 第五节:AppDomain FirstChance异常通知
  6. 非常实用的10个PHP高级应用技巧
  7. 20+ Rsync command’s switches and common usages with examples – Unix/Linux--reference
  8. PS基础
  9. 转:栈和队列小知识【STL用法】
  10. 花海漫步 NOI模拟题
  11. 内存管理 & 内存优化技巧 浅析
  12. LPC1788的外部中断和GPIO中断
  13. mysql进阶(二十六)MySQL 索引类型(初学者必看)
  14. Windows环境下消息中间件RabbitMq的搭建与应用
  15. arcgis api 4.x for js之图层管理篇
  16. python3,打印一年的某一天是一年的第几天
  17. [No000012E]WPF(6/7):概念绑定
  18. VS2010安装项目程序打包操作详解
  19. VS编程,WPF中两个滚动条 ScrollViewer 同步滚动的一种方法
  20. .NetCore 结合微服务项目设计总结下实践心得

热门文章

  1. SQL查询小案例
  2. 2018.8.10 python中的迭代器
  3. Python的深浅拷贝
  4. EXCEL批量导入到Sqlserver数据库并进行两表间数据的批量修改
  5. pythonpip的基本使用
  6. Asp.net Core 系列之--3.领域、仓储、服务简单实现
  7. 针对CCTV摄像头的扫描爆破工具 :Cameradar
  8. 『题解』LibreOJ6277 数列分块入门 1
  9. dubbo监控安装
  10. python3学习,有c++的基础