如何在Ubuntu上快速部署PHP项目,实现高效稳定上线?

更新于
2026-08-11 07:23:20
6阅读来源:SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

在 Ubuntu 上部署 PHP 项目,往往会遇到以下痛点:

  • 痛点一:安装步骤繁琐——需要分别安装 Web 服务器、PHP、数据库及相应 手动操作容易出错。
  • 痛点二:权限配置不当——文件或目录权限设置不当,导致应用无法访问或出现 403/500 错误。
  • 痛点三:日志难以定位——错误信息分散在 Apache/Nginx、PHP 和数据库日志中,定位困难。

一、环境准备

先更新程序包索引并升级已安装的软件包,确保所有依赖是最新的。

如何在Ubuntu上快速部署PHP项目,实现高效稳定上线?
sudo apt update && sudo apt upgrade -y

1.1 安装 Web 服务器

A. Apache

sudo apt install apache2 -y

B. Nginx

sudo apt install nginx -y

1.2 安装 PHP 与必要模块

根据项目需求选择 PHP 主版本。同时安装常用

sudo apt install php php-fpm libapache2-mod-php php-mysql php-pgsql \
php-mbstring php-xml php-curl -y

若使用 Apache,则需要启用 mod_php;若使用 Nginx,则需确保 PHP‑FPM 正常运行。

1.3 启动并开启服务自启动

# Apache
sudo systemctl enable --now apache2
# Nginx + PHP-FPM
sudo systemctl enable --now nginx php7.*fpm # 替换为实际版本号,例如 php7.4-fpm
# MySQL / MariaDB 或 PostgreSQL 根据需要选择后启动并开启自启动
sudo systemctl enable --now mysql # 或者 sudo systemctl enable --now postgresql

二、Web 服务器设置

A. 配置 Apache

  1. Edit virtual host configuration:
sudo nano /etc/apache2/sites-available/000-default.conf
  • Add following inside `` block:
  • 
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Require all granted
    
    
  • Enable `mod_rewrite` if your framework requires it:
  • sudo a2enmod rewrite && sudo systemctl restart apache2
    

    B. 配置 Nginx

    1. Edit default server block:
    sudo nano /etc/nginx/sites-available/default
    
  • Add/modify server configuration as follows:
  • 
    server {
    listen 80;server_name your_domain_or_ip;root /var/www/html;index index.php index.html index.htm;location / {
    try_files $uri $uri/ =404;}
    location ~ \\.php$ {
    include snippets/fastcgi-php.conf;fastcgi_pass unix:/var/run/php/php7.*‑fpm.sock;# replace * with your version e.g.,7.4
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}
    }
     
  • SE & EXIT,n restart Nginx:
  • sudo systemctl restart nginx
    

    三、数据库服务器安装与安全加固

    A. MySQL/MariaDB

    1. Create root password and secure installation:
     sudo apt install mysql-server -y
    sudo mysql_secure_installation
     
  • Create application database & user:
    • Mysql command example:
      • CREATE DATABASE app_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
      • CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'StrongPassword!',
      • GRANT ALL PRIVILEGES ON app_db.* TO 'app_user'@'localhost';
      • FLUSH PRIVILEGES;

    B.PostgreSQL

    • Install and enable service: bash sudo apt install postgresql -y sudo systemctl start postgresql sudo systemctl enable postgresql bash # Create database & user psql –U postgres –c "CREATE DATABASE app_db;" psql –U postgres –c "CREATE USER app_user WITH ENCRYPTED PASSWORD 'StrongPassword!'," psql –U postgres –c "GRANT ALL PRIVILEGES ON DATABASE app_db TO app_user;"

      四、上传项目文件并设置正确权限

      ⚠️ 使用者痛点二:如果文件权限错误,Apache/Nginx 会返回 “Forbidden” 或 “500 Internal Server Error”。话说回来,请务必按以下方式处理!

      # 上传方式可以是 FTP/SFTP/SCP 或 Git 拉取到 `/var/www/html`。示例使用 Git:

      bash cd /var/www/html git clone https://your.repo/project.git . # 注意最终的 . 表示当前目录

      如何在Ubuntu上快速部署PHP项目,实现高效稳定上线?

      # 设置拥有者为 web 使用者组,并给目录赋予适当权限:

      bash sudo chown -R www-data:www-data /var/www/html # www-data 是默认的 Apache/Nginx 使用者组

      find /var/www/html -type f -exec chmod 644 {} \; find /var/www/html -type d -exec chmod 755 {} \;

      五、配置 PHP 环境与

      # 根据项目需求检查是否缺少 可通过下列命令快速安装:

      bash

      for ext in mbstring xml curl gd intl opcache mysqli pdomysql pdopgsql redis zip;do \ sudo apt install php-$ext -y;\ done

      # 重新启动使改动生效:

      bash if systemctl list-units | grep apache;n \ sudo systemctl restart apache2;\ else \ sudo systemctl restart nginx;\ fi

      systemctl restart php7.*‑fpm # 替换为实际版本号

      检查 PHP 是否正常工作?在浏览器打开 `http://your_server_ip/info.php` 并添加下面内容:

      html

      If you see a detailed PHP info page,everything is set up correctly.

      test-db.php 并添加:

      html getMessage;} >

      运行该脚本确认无报错。


      六、测试部署与上线检查

      步骤 操作 检查项
      浏览器访问 http://your_domain_or_ip/ 页面是否完整加载,无 CSS/JS 报错
      API 调用 curl http://.../api/... 返回 JSON 且状态码为200
      日志查看 tail -n1000 /var/log/apache*/var/log/nginx/* 无明显错误记录

      七、常见问题排查教程

      痛点 原因 排查方法
      403 Forbidden 文件夹权限过高或 .htaccess 错误。怎么说呢, 确认 chmod 为755。.htaccess 内容无语法错误。
      500 Internal Server Error PHP 缺失或语法错误。 查看 /var/log/apache/error.log/var/log/nginx/error.log
      502 Bad Gateway PHP‑FPM 未运行或 socket 方法不匹配。 检查 systemctl status php*-fpm.service 与 socket 方法。
      Connection timed out 防火墙阻塞3306端口或 MySQL 未绑定127.0.0.1。 确认防火墙规则和 my.cnf 中 bind-address 设置。
      乱码 数据库字符集未设为 utf8mb4。 在创建数据库时指定字符集,并在连接字符串中加上 charset=utf8mb4。

      通过上述步骤。你已经完成了 Ubuntu 程序上从基础开始部署一个完整的 PHP 项目,并解决了最常见的三大痛点:繁琐的安装流程、权限设置失误还有日志排查困难。只要按顺序执行并及时检查日志,就可以快速、高效且稳定地上线发布。说起来,

      祝你部署愉快 🚀

    标签:Ubuntu

    在 Ubuntu 上部署 PHP 项目,往往会遇到以下痛点:

    • 痛点一:安装步骤繁琐——需要分别安装 Web 服务器、PHP、数据库及相应 手动操作容易出错。
    • 痛点二:权限配置不当——文件或目录权限设置不当,导致应用无法访问或出现 403/500 错误。
    • 痛点三:日志难以定位——错误信息分散在 Apache/Nginx、PHP 和数据库日志中,定位困难。

    一、环境准备

    先更新程序包索引并升级已安装的软件包,确保所有依赖是最新的。

    如何在Ubuntu上快速部署PHP项目,实现高效稳定上线?
    sudo apt update && sudo apt upgrade -y
    

    1.1 安装 Web 服务器

    A. Apache

    sudo apt install apache2 -y
    

    B. Nginx

    sudo apt install nginx -y
    

    1.2 安装 PHP 与必要模块

    根据项目需求选择 PHP 主版本。同时安装常用

    sudo apt install php php-fpm libapache2-mod-php php-mysql php-pgsql \
    php-mbstring php-xml php-curl -y
    

    若使用 Apache,则需要启用 mod_php;若使用 Nginx,则需确保 PHP‑FPM 正常运行。

    1.3 启动并开启服务自启动

    # Apache
    sudo systemctl enable --now apache2
    # Nginx + PHP-FPM
    sudo systemctl enable --now nginx php7.*fpm # 替换为实际版本号,例如 php7.4-fpm
    # MySQL / MariaDB 或 PostgreSQL 根据需要选择后启动并开启自启动
    sudo systemctl enable --now mysql # 或者 sudo systemctl enable --now postgresql
    

    二、Web 服务器设置

    A. 配置 Apache

    1. Edit virtual host configuration:
    sudo nano /etc/apache2/sites-available/000-default.conf
    
  • Add following inside `` block:
  • 
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Require all granted
    
    
  • Enable `mod_rewrite` if your framework requires it:
  • sudo a2enmod rewrite && sudo systemctl restart apache2
    

    B. 配置 Nginx

    1. Edit default server block:
    sudo nano /etc/nginx/sites-available/default
    
  • Add/modify server configuration as follows:
  • 
    server {
    listen 80;server_name your_domain_or_ip;root /var/www/html;index index.php index.html index.htm;location / {
    try_files $uri $uri/ =404;}
    location ~ \\.php$ {
    include snippets/fastcgi-php.conf;fastcgi_pass unix:/var/run/php/php7.*‑fpm.sock;# replace * with your version e.g.,7.4
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}
    }
     
  • SE & EXIT,n restart Nginx:
  • sudo systemctl restart nginx
    

    三、数据库服务器安装与安全加固

    A. MySQL/MariaDB

    1. Create root password and secure installation:
     sudo apt install mysql-server -y
    sudo mysql_secure_installation
     
  • Create application database & user:
    • Mysql command example:
      • CREATE DATABASE app_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
      • CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'StrongPassword!',
      • GRANT ALL PRIVILEGES ON app_db.* TO 'app_user'@'localhost';
      • FLUSH PRIVILEGES;

    B.PostgreSQL

    • Install and enable service: bash sudo apt install postgresql -y sudo systemctl start postgresql sudo systemctl enable postgresql bash # Create database & user psql –U postgres –c "CREATE DATABASE app_db;" psql –U postgres –c "CREATE USER app_user WITH ENCRYPTED PASSWORD 'StrongPassword!'," psql –U postgres –c "GRANT ALL PRIVILEGES ON DATABASE app_db TO app_user;"

      四、上传项目文件并设置正确权限

      ⚠️ 使用者痛点二:如果文件权限错误,Apache/Nginx 会返回 “Forbidden” 或 “500 Internal Server Error”。话说回来,请务必按以下方式处理!

      # 上传方式可以是 FTP/SFTP/SCP 或 Git 拉取到 `/var/www/html`。示例使用 Git:

      bash cd /var/www/html git clone https://your.repo/project.git . # 注意最终的 . 表示当前目录

      如何在Ubuntu上快速部署PHP项目,实现高效稳定上线?

      # 设置拥有者为 web 使用者组,并给目录赋予适当权限:

      bash sudo chown -R www-data:www-data /var/www/html # www-data 是默认的 Apache/Nginx 使用者组

      find /var/www/html -type f -exec chmod 644 {} \; find /var/www/html -type d -exec chmod 755 {} \;

      五、配置 PHP 环境与

      # 根据项目需求检查是否缺少 可通过下列命令快速安装:

      bash

      for ext in mbstring xml curl gd intl opcache mysqli pdomysql pdopgsql redis zip;do \ sudo apt install php-$ext -y;\ done

      # 重新启动使改动生效:

      bash if systemctl list-units | grep apache;n \ sudo systemctl restart apache2;\ else \ sudo systemctl restart nginx;\ fi

      systemctl restart php7.*‑fpm # 替换为实际版本号

      检查 PHP 是否正常工作?在浏览器打开 `http://your_server_ip/info.php` 并添加下面内容:

      html

      If you see a detailed PHP info page,everything is set up correctly.

      test-db.php 并添加:

      html getMessage;} >

      运行该脚本确认无报错。


      六、测试部署与上线检查

      步骤 操作 检查项
      浏览器访问 http://your_domain_or_ip/ 页面是否完整加载,无 CSS/JS 报错
      API 调用 curl http://.../api/... 返回 JSON 且状态码为200
      日志查看 tail -n1000 /var/log/apache*/var/log/nginx/* 无明显错误记录

      七、常见问题排查教程

      痛点 原因 排查方法
      403 Forbidden 文件夹权限过高或 .htaccess 错误。怎么说呢, 确认 chmod 为755。.htaccess 内容无语法错误。
      500 Internal Server Error PHP 缺失或语法错误。 查看 /var/log/apache/error.log/var/log/nginx/error.log
      502 Bad Gateway PHP‑FPM 未运行或 socket 方法不匹配。 检查 systemctl status php*-fpm.service 与 socket 方法。
      Connection timed out 防火墙阻塞3306端口或 MySQL 未绑定127.0.0.1。 确认防火墙规则和 my.cnf 中 bind-address 设置。
      乱码 数据库字符集未设为 utf8mb4。 在创建数据库时指定字符集,并在连接字符串中加上 charset=utf8mb4。

      通过上述步骤。你已经完成了 Ubuntu 程序上从基础开始部署一个完整的 PHP 项目,并解决了最常见的三大痛点:繁琐的安装流程、权限设置失误还有日志排查困难。只要按顺序执行并及时检查日志,就可以快速、高效且稳定地上线发布。说起来,

      祝你部署愉快 🚀

    标签:Ubuntu