{{format('0')}} {{format('589')}} {{format('5537')}}

Centos8部署Nginx并且配置开机自启 [ 技术杂谈 ]

九叶的小窝 文章 正文

As Long As You Love Me.
分享

九叶

{{nature("2022-09-13 11:10:48")}}更新

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

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

加入开机启动

# 添加
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

![]()

评论 0
0
{{userInfo.data?.nickname}}
{{userInfo.data?.email}}
TOP 2
Minecraft | [1.12.2] 核电工艺模拟器 1.2.25 —— 汉化版

{{nature('2021-12-07 15:29:00')}} {{format('3004')}}人已阅读

TOP 3
SSM搭建Spring单元测试环境

{{nature('2021-01-31 20:01:00')}} {{format('1315')}}人已阅读

TOP 4
dispatcher-servlet.xml文件配置模板

{{nature('2020-12-11 21:17:00')}} {{format('1236')}}人已阅读

TOP 5
Windows平台Nacos启动报错无法创建Bean实例

{{nature('2021-04-22 15:16:00')}} {{format('1192')}}人已阅读

目录

标签云

CentOS Nginx

一言

# {{hitokoto.data.from || '来自'}} #
{{hitokoto.data.hitokoto || '内容'}}
作者:{{hitokoto.data.from_who || '作者'}}
自定义UI
配色方案

侧边栏