温馨提示×

Linux LAMP怎样部署网站项目

小樊
39
2026-01-06 13:20:11
栏目: 云计算

Linux LAMP部署网站项目实操指南

一 环境准备与组件安装

  • 系统更新与权限
    • 更新系统并准备具有 sudo 权限的账号:
      • Ubuntu/Debian:sudo apt update && sudo apt upgrade -y
      • CentOS/RHEL:sudo yum update -y
  • 安装与启动 Apache
    • Ubuntu/Debian:sudo apt install apache2 -y && sudo systemctl start apache2 && sudo systemctl enable apache2
    • CentOS/RHEL:sudo yum install httpd -y && sudo systemctl start httpd && sudo systemctl enable httpd
  • 安装数据库(二选一或并存)
    • MySQL:sudo apt install mysql-server -y 或 sudo yum install mysql-server -y
    • MariaDB(推荐于 RHEL/CentOS 场景):sudo yum install mariadb-server -y
    • 安全初始化:sudo mysql_secure_installation
  • 安装 PHP 与常用扩展
    • Ubuntu/Debian:sudo apt install php libapache2-mod-php php-mysql php-gd php-mbstring php-curl php-xml php-zip -y
    • CentOS/RHEL(启用 Remi 仓库按需安装 PHP 8.x):sudo yum install epel-release && sudo rpm -Uvh https://mirrors.tencent.com/remi/enterprise/remi-release-7.rpm && sudo yum-config-manager --enable remi-php83 && sudo yum install -y php php-cli php-fpm php-mysqlnd php-zip php-gd php-mbstring php-curl php-xml php-bcmath php-json
  • 访问测试
    • 浏览器访问 http://服务器IP/ 出现 Apache 欢迎页即表示 Web 服务正常。

二 配置 Apache 与虚拟主机

  • 目录与权限
    • Ubuntu/Debian 常用 Web 根目录:/var/www/html 或项目目录 /var/www/example.com/html
    • CentOS/RHEL 常用 Web 根目录:/var/www/html
    • 设置属主属组(Apache 运行用户):
      • Ubuntu/Debian:sudo chown -R www-data:www-data /var/www/example.com/html
      • CentOS/RHEL:sudo chown -R apache:apache /var/www/example.com/html
  • 虚拟主机示例(Apache 2.4)
    • Ubuntu/Debian(/etc/apache2/sites-available/example.com.conf):
      <VirtualHost *:80>
          ServerAdmin webmaster@example.com
          ServerName example.com
          ServerAlias www.example.com
          DocumentRoot /var/www/example.com/html
          ErrorLog ${APACHE_LOG_DIR}/error.log
          CustomLog ${APACHE_LOG_DIR}/access.log combined
      
          <Directory /var/www/example.com/html>
              Options Indexes FollowSymLinks
              AllowOverride All
              Require all granted
          </Directory>
      </VirtualHost>
      
      启用站点:sudo a2ensite example.com.conf && sudo a2dissite 000-default.conf && sudo systemctl reload apache2
    • CentOS/RHEL(/etc/httpd/conf.d/example.com.conf):
      <VirtualHost *:80>
          ServerName example.com
          DocumentRoot /var/www/example.com/html
          <Directory /var/www/example.com/html>
              AllowOverride All
              Require all granted
          </Directory>
          ErrorLog /var/log/httpd/example.com_error.log
          CustomLog /var/log/httpd/example.com_access.log combined
      </VirtualHost>
      
      检查配置与重载:sudo httpd -t && sudo systemctl reload httpd
  • 常用目录与启用模块
    • 主配置:Ubuntu/Debian /etc/apache2/apache2.conf;CentOS/RHEL /etc/httpd/conf/httpd.conf
    • 模块与站点:a2enmod、a2ensite / a2dissite。

三 配置数据库与创建项目资源

  • 登录与基础安全
    • 登录数据库:mysql -u root -p
    • 安全脚本:sudo mysql_secure_installation(设置 root 密码、移除匿名用户、禁止远程 root、删除测试库)
  • 创建数据库与用户(示例)
    CREATE DATABASE exampledb CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
    CREATE USER 'exampleuser'@'localhost' IDENTIFIED BY 'StrongPass!';
    GRANT ALL PRIVILEGES ON exampledb.* TO 'exampleuser'@'localhost';
    FLUSH PRIVILEGES;
    EXIT;
    
  • 导入项目数据
    • 方式一:mysql -u exampleuser -p exampledb < backup.sql
    • 方式二:应用安装向导(如 WordPress、Drupal 等)在线导入
  • 项目文件部署
    • 方式一:SCP/SFTP 上传至 DocumentRoot
      • scp -r ./myapp user@your_server_ip:/var/www/example.com/html
    • 方式二:Git 拉取
      • git clone https://repo-url.git /var/www/example.com/html
  • 配置文件
    • 将应用配置中的数据库主机、库名、账号与密码更新为上述创建的值(如 localhostexampledbexampleuser)。

四 域名 DNS 与防火墙放行

  • 域名与解析
    • 在域名 DNS 将 example.comwww.example.com 解析到服务器 公网 IP
    • 无域名阶段可用本地 hosts 解析测试(客户端:编辑 /etc/hosts 添加 “服务器IP example.com”)
  • 防火墙放行
    • Ubuntu/Debian(UFW):sudo ufw allow ‘Apache Full’ 或 sudo ufw allow 80,443/tcp
    • CentOS/RHEL(firewalld):sudo firewall-cmd --permanent --add-service=http --add-service=https && sudo firewall-cmd --reload
  • 云服务器安全组
    • 在云控制台放行 TCP 80/443 入站规则(安全组与操作系统防火墙需同时放行)。

五 上线验证与性能安全优化

  • 环境验证
    • PHP 解析测试:echo “” > /var/www/html/info.php
    • 访问 http://example.com/info.php 应显示 PHP 信息页(验证后建议删除该文件)
    • 访问 http://example.com/ 检查站点首页与应用是否正常
  • 性能优化要点
    • PHP:启用 OPcache(opcache.enable=1;可按内存调优 opcache.memory_consumption、opcache.max_accelerated_files)
    • Apache:启用 KeepAlive、合理设置 MaxRequestWorkers,必要时启用缓存模块(如 mod_cache)
    • MySQL/MariaDB:建立合适索引、优化慢查询、合理配置缓冲与连接数
    • 缓存与静态资源:引入 Redis/Memcached,开启 Gzip 压缩,静态资源使用长缓存
  • 安全加固要点
    • 全站 HTTPS:使用 Let’s Encrypt 免费证书(certbot --apache 或 certbot --nginx)
    • 最小权限:Web 目录禁止执行脚本、限制上传目录执行权限
    • 组件更新:定期更新 Linux、Apache、MySQL/MariaDB、PHP 与扩展
    • 入侵防护:部署 Fail2ban,监控异常登录与暴力请求
    • 备份恢复:定期备份 /var/www 与数据库(如每日全备+binlog),并演练恢复流程。

0