【正式服部署】安装Nginx

【正式服部署】安装Nginx

📔 千寻简笔记介绍

千寻简文库已开源,Gitee与GitHub搜索chihiro-doc,包含笔记源文件.md,以及PDF版本方便阅读,文库采用精美主题,阅读体验更佳,如果文章对你有帮助请帮我点一个Star

更新:支持在线阅读文章,根据发布日期分类。

简介

  • 系统版本:CentOS 7

    • 查看命令:cat /etc/redhat-release
  • Nginx版本:1.20.2

    • 查看命令:

      [root@localhost sbin]# whereis nginx
      nginx: /usr/local/nginx
      [root@localhost sbin]# cd /usr/local/nginx/sbin/
      [root@localhost sbin]# ./nginx -v
      nginx version: nginx/1.20.2
      
  • yum版本:3.4.3

    • 查看命令:yum -v

本文关键词

nginxCentOS 安装教程

实现步骤

1 安装Nginx

1.1 安装Nginx依赖包

在安装Nginx之前安装这些软件包是因为Nginx使用了一些功能和模块,这些功能和模块需要这些软件包中的文件和库来支持和编译。

  • gcc:是GNU编译器套件,用于编译源代码。Nginx可能需要编译一些模块或自定义配置,因此需要gcc来编译源代码。
  • pcre-devel:PCRE是Perl Compatible Regular Expressions的缩写,这个软件包提供了正则表达式的支持。Nginx使用PCRE作为其中一个HTTP模块,以支持正则表达式匹配功能。
  • zlib-devel:zlib是一个用C语言编写的压缩库,Nginx使用它来进行HTTP数据的压缩和解压缩。
  • opensslopenssl-devel:OpenSSL是一个用于安全通信的加密库。Nginx可以使用OpenSSL来支持HTTPS和SSL/TLS协议,openssl-devel提供了开发所需的文件和头文件,以便Nginx可以与OpenSSL进行集成。

安装这些软件包是确保在编译Nginx时拥有必要的依赖项,以便支持Nginx的各种功能和模块,尤其是对于HTTPS、压缩和正则表达式等功能的支持。

# 一键安装上面四个依赖
[root@localhost ~]# yum -y install gcc pcre-devel zlib-devel openssl openssl-devel
Complete!
1.2 下载并解压安装包

查看可下载的版本:

http://nginx.org/download/

下载

# 切换目录
[root@localhost ~]# cd /
# 创建一个文件夹
[root@localhost ~]# mkdir -p /www/server/nginx
# 切换目录
[root@localhost ~]# cd /www/server/nginx
# 下载
[root@localhost nginx]# wget http://nginx.org/download/nginx-1.20.2.tar.gz
# 解压
[root@localhost nginx]# tar -xvf nginx-1.20.2.tar.gz

【可网络请求的服务器忽略此步骤】如果登录的是堡垒机,不允许发起网络请求下载,直接请求文件网址进行下载上传到我们创建的目录,堡垒机工具可以使用xftp,可通过堡垒机直接上传到指定目录。

http://nginx.org/download/nginx-1.20.2.tar.gz

上传完成后查看目录文件

# 查看路径
[root@localhost nginx]# pwd
/www/server/nginx
# 查看文件是否上传成功,大小与网页标注一致
[root@localhost nginx]# ll
total 1040
-rw-r--r-- 1 root root 1062124 Nov 29 15:56 nginx-1.20.2.tar.gz
# 解压
[root@localhost nginx]# tar -xvf nginx-1.20.2.tar.gz
1.3 安装nginx

扩展知识:

  • ./configure:这是运行Nginx的配置脚本的命令。它会检查系统环境并根据系统的特定情况设置Nginx的编译选项。
  • –prefix=/usr/local/nginx:这个选项指定了Nginx安装的路径。在这里,/usr/local/nginx是指将Nginx安装到/usr/local/nginx目录下。--prefix选项允许你指定Nginx的安装根目录,你可以根据自己的需求选择不同的路径。
# 进入nginx目录
[root@localhost nginx]# cd /www/server/nginx
# 进入目录
[root@localhost nginx]# cd nginx-1.20.2
# 执行命令:配置编译Nginx的安装选项
[root@localhost nginx-1.20.2]# ./configure --prefix=/usr/local/nginx
# 执行make命令
[root@localhost nginx-1.20.2]# make
# 执行make install命令
[root@localhost nginx-1.20.2]# make install
1.4 配置nginx.conf
# 备份Nginx配置文件
[root@localhost conf]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
# 打开配置文件
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
1.5 运行以下命令,进入Nginx的sbin目录,然后启动Nginx
[root@localhost nginx]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ./nginx

安装完成一般常用命令

  • 首先进入 sbin 目录

    [root@localhost sbin]# cd /usr/local/nginx/sbin
    
  • 启动 Nginx

    [root@localhost sbin]# ./nginx
    
  • 停止 Nginx

    [root@localhost sbin]# ./nginx -s stop
    
  • 重新加载 Nginx

    [root@localhost sbin]# ./nginx -s reload
    
  • 查看 Nginx 版本

    [root@localhost sbin]# ./nginx -v
    
1.6 查看nginx是否成功启动
[root@localhost sbin]# ps -ef | grep nginx
root     24247     1  0 16:08 ?        00:00:00 nginx: master process ./nginx
nobody   24248 24247  0 16:08 ?        00:00:00 nginx: worker process
root     24250 21480  0 16:11 pts/0    00:00:00 grep --color=auto nginx

2 扩展

2.1 配置文件修改后,需要指定配置文件进行重启
/usr/local/nginx/sbin/nginx -s reload -c /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
#定配置文件进行重启
[root@localhost sbin]# /usr/local/nginx/sbin/nginx -s reload -c /usr/local/nginx/conf/nginx.conf
#检测文件是否配置正确
[root@localhost sbin]# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

# 创建日志目录
[root@localhost sbin]# mkdir -p /var/log/nginx
2.2 创建一个软链接启动nginx
#建立软链接
[root@localhost sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
#启动
[root@localhost sbin]# nginx
#查看nginx版本
[root@localhost ~]# nginx -v
nginx version: nginx/1.21.0
2.3 配置nginx开机自启动
  1. 创建一个nginx.service,按i键进入编辑模式,修改配置文件,按Esc键,然后输入:wq并按Enter键以保存
#进入目录
[root@localhost system]# cd /usr/lib/systemd/system/
#创建文件
[root@localhost system]# touch nginx.service
#编辑文件内容
[root@localhost system]# vi /usr/lib/systemd/system/nginx.service
#赋予可执行的权限
[root@localhost system]# chmod +x /usr/lib/systemd/system/nginx.service
  1. 编辑文件内容
[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                               #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                                               #服务用户的模式
  1. 查看文件内容
[root@localhost ~]# cat /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
  1. 启动服务
#在启动服务之前,需要先重载systemctl命令
[root@localhost ~]# systemctl daemon-reload
#启动服务或者使用systemctl start nginx
[root@localhost ~]# systemctl start nginx.service
#运行以下命令设置Nginx服务开机自启动
[root@localhost ~]# systemctl enable nginx
#查看nginx
[root@localhost ~]# ps -ef | grep nginx
root      14869      1  0 17:37 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody    14871  14869  0 17:37 ?        00:00:00 nginx: worker process
root      14893   1549  0 17:37 pts/0    00:00:00 grep --color=auto nginx
  1. 配置systemctl之后的启动方式
systemctl status nginx  #状态
systemctl start nginx  #启动
systemctl stop nginx   #停止
systemctl restart nginx  #重启
  1. 使用reboot命令重启后,查看nginx是否成功的自启动了
[root@localhost ~]# reboot
Connection to 192.168.0.197 closed by remote host.
Connection to 192.168.0.197 closed.

C:\Users\beauty>ssh root@192.168.0.197
root@192.168.0.197's password:
Last login: Sat Jun  5 16:09:35 2021 from 192.168.0.194
[root@localhost ~]# ps -ef | grep nginx
root       1081      1  0 17:39 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody     1084   1081  0 17:39 ?        00:00:00 nginx: worker process
root       1567   1548  0 17:40 pts/0    00:00:00 grep --color=auto nginx
  • 23
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千寻简

感谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值