JiuyeXD's Blog
九叶
九叶博主

越努力 越幸运

登录
夜间

Centos8部署Nginx并且配置开机自启

1.创建用户

1. 创建www用户

UID、GID 皆是 501通过cat /etc/passwd,检查是否存在www用户

# 添加组
groupadd -g 501 www
# 添加用户
useradd -u 501 -g www www
# 查询www是否存在
cat /etc/passwd

image.png

image.png

/etc/passwd文件以 [font color=“red”]‘:’[/font]为分割符

字段 具体含义
www 用户的名称
x 用户的密码占位符
501 用户的UID信息
501 用户的GID信息
用户的注释信息
/home/www 用户的home目录
/bin/bash /bin/bash #用户的命令解释器 能够登录系统的
/sbin/nologin 禁止登录系统

2. 给www用户指定目录

# usermod [-LU][-c <备注>][-d <登入目录>][-e <有效期限>][- f <缓冲天数>][-g <群组>][-G <群组>][-l <帐号名称>][-s ][-u ] [用户帐号]
usermod -d /var/www -u 501 www

image.png

字段 具体含义
-c <备注> 修改用户帐号的备注文字
-d <登入目录> 修改用户登入时的目录
-e <有效期限> 修改帐号的有效期限
-f <缓冲天数> 修改在密码过期后多少天即关闭该帐号
-g <群组> 修改用户所属的群组
-G <群组> 修改用户所属的附加群组
-l <帐号名称> 修改用户帐号名称
-L 锁定用户密码,使密码无效
-s 修改用户登入后所使用的shell
-u 修改用户ID
-U 解除密码锁定

2. 下载并安装Nginx

1. 下载

# 下载Redis包
wget -P /opt http://nginx.org/download/nginx-1.20.0.tar.gz
# 解压
cd /opt
tar -zxvf nginx-*.tar.gz
# 删除
rm -rf nginx-*.tar.gz

2. 编译安装

# 下载编译依赖
yum install gcc gcc-c++ make automake autoconf libtool pcre* zlib openssl openssl-devel

result.png

# 进入nginx目录
cd /opt/nginx*
# 配置
./configure --user=www --group=www --prefix=/opt/nginx --with-http_gunzip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_ssl_module --with-http_stub_status_module --with-pcre --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module

image.png

# 编译安装
make && make install && cd /opt && rm -rf nginx*.*

image.png

# 进入sbin目录
cd /opt/nginx/sbin
# 运行nginx
./nginx

image.png

3. 配置Nginx

1. 配置 nginx.conf 让每个网站可以有自己的配置文件

# 进入 conf 目录
cd /opt/nginx/conf
# 创建 vhosts 目录
mkdir sites
# 编辑 nginx.conf 文件
vim /opt/nginx/conf/nginx.conf

在文件头插入

user www;

user www.png

在http的最下面添加

server_names_hash_bucket_size 64; 
include /opt/nginx/vhosts/*.conf;

image.png

然后退出保存

2. 配置环境变量

新建文件

vim /etc/profile.d/nginx.sh

添加内容

export PATH=/opt/nginx/sbin:$PATH

image.png

在全局变量中生效

exec bash /etc/profile.d/nginx.sh

测试nginx指令在全局已经生效

result.png

从容停止服务器

nginx -s quit

这种方法较stop相比就比较温和一些了,需要进程完成当前工作后再停止。

立即停止服务器

nginx -s stop

这种方法比较强硬,无论进程是否在工作,都直接停止进程。

查询nginx主进程号

ps -ef | grep nginx

从容停止 kill -QUIT 主进程号

快速停止 kill -TERM 主进程号

强制停止 kill -9 nginx

3. 设置开机自启

进入到/lib/systemd/system/目录

cd /lib/systemd/system/

创建nginx.service文件,并编辑

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

[collapse status=“false” title=“配置文件解释”]

[scode type=“blue”]

[Unit] 服务的说明 Description:描述服务 After:描述服务类别 [Service] 服务运行参数的设置 Type=forking是后台运行的形式 ExecStart为服务的具体运行命令 ExecReload为重启命令 ExecStop为停止命令 PrivateTmp=True表示给服务分配独立的临时空间 注意:

[Service]的启动、重启、停止命令全部要求使用绝对路径 [Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

[/scode]

[/collapse]

加入开机启动

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

一个常见的错误

Warning: nginx.service changed on disk. Run ‘systemctl daemon-reload’ to reload units.

直接按照提示执行命令systemctl daemon-reload 即可

# systemctl daemon-reload

THE END