在Linux系统的VPS 或云服务器上搭建Web网站,使用脚本化操作不仅能提高效率,还能保证一致性和可重复性。
本文介绍Linux环境下各阶段所需的脚本,从服务器初始化、安全加固、Web环境安装、网站自动化部署到运维监控,覆盖网站全生命周期管理。
1. 服务器初始化脚本
首次购买 VPS 后,第一步是基础配置:更新系统、设置时区、创建新用户并配置 SSH 公钥登录、关闭 root 直连等。
#!/usr/bin/env bash
# server_init.sh # 1. 设置时区
timedatectl set-timezone Asia/Shanghai # 2. 更新系统
apt update && apt -y upgrade # Debian/Ubuntu
# yum update -y # RHEL/CentOS # 3. 安装基本工具
apt install -y vim wget curl git ufw # 4. 创建新用户
read -p "请输入新用户名: " USERNAME
groupadd sudo
useradd -m -s /bin/bash -G sudo $USERNAME # 5. 配置 SSH 公钥登录
mkdir -p /home/$USERNAME/.ssh
read -p "请输入公钥内容: " PUBKEY
echo $PUBKEY > /home/$USERNAME/.ssh/authorized_keys
chmod 700 /home/$USERNAME/.ssh
ochmod 600 /home/$USERNAME/.ssh/authorized_keys
chown -R $USERNAME:$USERNAME /home/$USERNAME/.ssh # 6. 禁用 root 密码登录
sed -i 's/^PermitRootLogin .*/PermitRootLogin no/' /etc/ssh/sshd_config
systemctl restart sshd echo "初始化完成,用户 $USERNAME 已创建并配置 SSH 登录。"
此脚本完成了基础环境准备,接下来可在同一脚本中添加防火墙规则。
2. 安全加固脚本
安全加固主要包含防火墙(UFW/iptables)、Fail2ban、SSH 限制等。
#!/usr/bin/env bash
# security_hardening.sh # 1. 配置 UFW
ufw default deny incoming
ufw default allow outgoing
ufw allow 22/tcp # SSH
ufw allow 80/tcp # HTTP
ufw allow 443/tcp # HTTPS
ufw enable # 2. 安装并配置 Fail2ban
apt install -y fail2ban
cat > /etc/fail2ban/jail.local <<EOF
[sshd]
enabled = true
port = 22
filter = sshd
action = iptables[name=SSH, port=22, protocol=tcp]
logpath = /var/log/auth.log
maxretry = 5
EOF
systemctl restart fail2ban # 3. 修改 SSH 默认端口
NEW_PORT=2222
sed -i "s/#Port 22/Port $NEW_PORT/" /etc/ssh/sshd_config
ufw allow $NEW_PORT/tcp
describe UFW rules
echo "请手动验证 SSH 端口已修改并配置防火墙规则。"
通过脚本方式,安全设置可一键完成。
3. 环境安装脚本
3.1 LAMP/LEMP 环境
LAMP (Linux + Apache + MySQL/MariaDB + PHP):
#!/usr/bin/env bash
# install_lamp.sh # 更新源
apt update # 安装 Apache, MariaDB, PHP
apt install -y apache2 mariadb-server php php-mysql libapache2-mod-php # 启动并开机自启
systemctl enable --now apache2 mariadb # 初始化数据库安全
mysql_secure_installation <<EOF
Y
root_password
root_password
Y
Y
Y
Y
EOF # 测试 PHP
cat > /var/www/html/info.php <<EOF
<?php phpinfo(); ?>
EOF echo "LAMP 环境安装完成,访问 http://服务器IP/info.php 查看 PHP 信息。"
LEMP (Nginx + MariaDB + PHP-FPM):
#!/usr/bin/env bash
# install_lemp.sh apt update
apt install -y nginx mariadb-server php-fpm php-mysql # 启动并开机自启
systemctl enable --now nginx mariadb php7.*-fpm # 配置 Nginx
cat > /etc/nginx/sites-available/default <<EOF
server { listen 80; server_name example.com; root /var/www/html; index index.php index.html; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.*-fpm.sock; }
}
EOF nginx -t && systemctl reload nginx echo "LEMP 环境安装完成,请将网站文件放入 /var/www/html。"
3.2 Node.js & PM2 环境
#!/usr/bin/env bash
# install_node_pm2.sh apt update
# 安装 Node.js 16.x
curl -fsSL https://deb.nodesource.com/setup_16.x | bash -
apt install -y nodejs # 安装并配置 PM2
npm install -g pm2
pm2 startup systemd -u $USER --hp /home/$USER # 示例启动应用
cd /path/to/your/app
pm2 start app.js --name my-app
pm2 save echo "Node.js & PM2 环境配置完成,使用 'pm2 status' 查看进程。"
4. 网站自动化部署脚本
4.1 Git 部署脚本
简单的拉取并重启示例:
#!/usr/bin/env bash
# deploy.sh APP_DIR=/var/www/html/your-app
BRANCH=main cd $APP_DIR
# 拉取最新代码
git fetch origin
git reset --hard origin/$BRANCH # 依赖安装 & 构建
npm install && npm run build # 或者 composer install, pip install -r requirements.txt # 重启服务
pm2 restart my-app # Node.js 服务
systemctl restart php-fpm # PHP-FPM
systemctl reload nginx # Nginx echo "部署完成:分支 $BRANCH 的最新代码已上线。"
4.2 Ansible 自动化剧本
Ansible 剧本将多台服务器统一管理,可定义安装、配置、部署流程。
# site.yml
- hosts: webservers become: yes vars: app_repo: 'git@github.com:your/repo.git' app_path: '/var/www/html/your-app' tasks: - name: 更新 apt 缓存 apt: update_cache: yes - name: 安装必要软件 apt: name: - nginx - git - php-fpm state: present - name: 拉取应用代码 git: repo: '{{ app_repo }}' dest: '{{ app_path }}' version: 'main' - name: 安装依赖 shell: 'cd {{ app_path }} && npm install' - name: 配置 Nginx template: src: templates/nginx.conf.j2 dest: /etc/nginx/sites-available/default - name: 重启服务 service: name: nginx state: restarted - name: 启动应用 shell: 'pm2 restart all'
使用 ansible-playbook -i inventory site.yml 一键完成多节点部署。
















