Linux_基础篇
欢迎来到Linux的世界,看笔记好好学多敲多打,每个人都是大神!
版本号: 1.0,0
作者: @老王要学习
日期: 2025.05.02
适用环境: Centos7
本文档聚焦于 CentOS 7 环境下 Nginx 的安装部署。详细介绍了两种安装方法,即 yum 安装和编译安装,同时包含创建自定义配置文件、域名解析、设置开机自启等内容,还提供了一键安装脚本,为用户在 Linux 系统上搭建 Nginx 服务提供全面指导
systemctl stop firewalld && systemctl disable firewalld
sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
reboot
#安装依赖:
yum -y install wget vim
#安装epel源
wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
# 更新缓存
yum makecache
# 查看yum仓库
yum repolist
#结果如下:
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
源标识 源名称 状态
base/7/x86_64 CentOS-7 - Base - mirrors.aliyun.com 10,072
epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 13,791
extras/7/x86_64 CentOS-7 - Extras - mirrors.aliyun.com 526
updates/7/x86_64 CentOS-7 - Updates - mirrors.aliyun.com 6,173
repolist: 30,562
cat>/etc/yum.repos.d/nginx.repo<
# 重新建立缓存
yum makecache
# 安装nginx
yum install nginx
yum -y install pcre-devel openssl-devel zlib-devel
# 下载nginx包
wget https://nginx.org/download/nginx-1.26.3.tar.gz -c /usr/local/src/
# 解压安装包
tar zxf /usr/local/src/nginx-1.26.3.tar.gz
groupadd -r www
useradd -g www -M -s /bin/false -r www
id www
#编译
cd /usr/local/src/nginx-1.26.3
./configure --prefix=/usr/local/nginx/
#安装
make && make install
/usr/local/nginx/sbin/nginx
ss -antpl | grep nginx
#结果如下:
LISTEN 0 128 *:80 *:* users:(("nginx",pid=10790,fd=6),("nginx",pid=10789,fd=6))
curl localhost
#结果如下:
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
# 停止nginx
/usr/local/nginx/sbin/nginx -s stop
# 写入配置文件
cat>/usr/local/nginx/conf/mynginx.conf<
#创建网页lw01目录
mkdir /usr/local/nginx/html1/
#写入内容
echo "my nginx web01" > /usr/local/nginx/html1/index.html
#启动服务
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/mynginx.conf
#访问测试
curl localhost
#停止nginx服务
/usr/local/nginx/sbin/nginx -s stop
#创建web02目录
mkdir /usr/local/nginx/html2
#写入数据
echo "mynginx web02" > /usr/local/nginx/html2/index.html
#修改配置文件
cat > /usr/local/nginx/conf/nginx.conf <
#追加内容到hosts文件
cat>>/etc/hosts<
/usr/local/nginx/sbin/nginx -s stop
cat>/usr/lib/systemd/system/nginx.service<
systemctl stop firewalld && systemctl disable firewalld
sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
reboot
#!/bin/bash
#下载nginx进行安装
#安装开发软件
yum -y install pcre-devel openssl-devel zlib-devel
#下载nginx包
cd /usr/local/src/
wget https://nginx.org/download/nginx-1.26.3.tar.gz
tar zxf nginx-1.26.3.tar.gz
#创建一个系统用户组
groupadd -r www
useradd -g www -M -s /bin/false -r www
id www
#添加模块
cd /usr/src/nginx-1.26.3/
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_random_index_module --with-http_stub_status_module
#源码编译
make && make install
#停止nginx
/usr/local/nginx/sbin/nginx -s stop
cat > /usr/local/nginx/conf/nginx.conf < /usr/local/nginx/html1/index.html
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/mynginx.conf
curl localhost
#创建web02
/usr/local/nginx/sbin/nginx -s stop
mkdir /usr/local/nginx/html2
echo "mynginx web02" > /usr/local/nginx/html2/index.html
cat > /usr/local/nginx/conf/nginx.conf <> /etc/hosts
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/mynginx.conf
curl www.web1.com
curl www.web2.com
#开机自启
/usr/local/nginx/sbin/nginx -s stop
cd /usr/lib/systemd/system
cp sshd.service nginx.service
cat > nginx.service <