一、准备工作
- 准备相对干净环境建议恢复到干净的系统,并使用脚本完成初始化
– 配置yum源,包括EPEL
– 停止并禁用防火墙
– 安装基础软件包
注:初始化脚本参考本文最后的脚本部分
- 安装基础编译工具yum install -y “@Development Tools” #@表示后面是一个软件包组,开发工具组
[root@bogon ~]# yum grouplist #查看系统提供的软件包组
Loaded plugins: fastestmirror, langpacks
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Available Environment Groups:
Minimal Install
Virtualization Host
Server with GUI
GNOME Desktop
Available Groups:
Cinnamon
Compatibility Libraries
Console Internet Tools
Development Tools
Educational Software
安装Development Tools组之后:
[root@bogon ~]# yum grouplist
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Available Environment Groups:
Minimal Install
MATE Desktop
Basic Web Server
Installed Groups: #Installed Groups表示已安装的组
Development Tools
Available Groups:
Cinnamon
Compatibility Libraries
- 安装Nginx依赖库yum install -y pcre-devel zlib-devel openssl-devel
二、下载Nginx源代码
- 访问Nginx官网获取最新稳定版下载链接 https://nginx.org/
- 下载并解压源代码wget https://nginx.org/download/nginx-1.28.0.tar.gz #下载软件包
tar -xf nginx-1.28.0.tar.gz #解压软件包
cd nginx-1.28.0/
[root@bogon nginx-1.28.0]# ls
auto CHANGES.ru conf contrib html man SECURITY.md
CHANGES CODE_OF_CONDUCT.md configure CONTRIBUTING.md LICENSE README.md src
三、编译及安装
- 配置./configure \
–prefix=/usr/local/nginx \
–user=nginx \
–sbin-path=/usr/local/nginx/sbin/nginx \
–conf-path=/etc/nginx/nginx.conf \
–error-log-path=/var/log/nginx/error.log \
–with-http_stub_status_module \
–with-http_ssl_module \
–with-http_gzip_static_module \
–with-pcre –with-http_realip_module \
–with-stream
- 编译make
- 安装make install
四、创建Nginx用户
- 创建Nginx用户sudo useradd -r -s /sbin/nologin nginx # -r 系统用户,UID默认为小于1000
五、配置系统服务【扩展】
- 创建systemd服务文件sudo vim /etc/systemd/system/nginx.service
2. **添加以下内容**
```ini
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
- 重新加载systemd配置sudo systemctl daemon-reload
六、启动与验证
- 启动Nginx服务sudo systemctl start nginx
- 设置开机自启sudo systemctl enable nginx
- 检查服务状态sudo systemctl status nginx
- 测试访问http://服务器IP
七、附初始化脚本init.sh
[root@bogon ~]# vim init.sh #创建系统初始化脚本
#!/bin/bash
#删除系统原有的YUM源
rm -rf /etc/yum.repos.d/*
#配置阿里云基础源(base,extras,updates)
curl https://mirrors.aliyun.com/repo/Centos-7.repo -o /etc/yum.repos.d/CentOS-Base.repo
#配置阿里云EPEL源
curl https://mirrors.aliyun.com/repo/epel-7.repo -o /etc/yum.repos.d/epel.repo
# 清理旧缓存并生成新缓存
yum clean all
yum makecache
#安装基本的软件包
yum install -y wget curl vim-enhanced git net-tools bash-completion
#关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
💡 感谢你读到这里!这篇《WEB服务器》是我的用心分享,如果你觉得有收获,不妨:
⭐ 点个「赞」或「分享」,让更多同路人相遇
🔍 关注我的博客,持续接收技术/成长类干货
💬 文末留言聊聊你的想法,或遇到的同类问题~
🔒 本文为原创,转载请保留原文链接(https://www.lichu228.top/archives/305)与作者信息,感谢尊重与支持!
🌐 想找更多优质内容?点击进入博客主页探索吧~