温馨提示×

Ubuntu PHP项目怎么部署

小樊
49
2025-07-17 17:35:03
栏目: 编程语言

在Ubuntu上部署PHP项目通常涉及以下几个步骤:

  1. 更新系统

    sudo apt update
    sudo apt upgrade
    
  2. 安装Web服务器

    • Apache
      sudo apt install apache2
      sudo systemctl start apache2
      sudo systemctl enable apache2
      
    • Nginx
      sudo apt install nginx
      sudo systemctl start nginx
      sudo systemctl enable nginx
      
  3. 安装PHP及相关扩展

    sudo apt install php php-mysql php-cli php-fpm php-gd php-mbstring php-xml php-zip
    
  4. 配置Web服务器

    • Apache配置: 编辑 /etc/apache2/sites-available/000-default.conf 文件,添加以下内容:

      <VirtualHost *:80>
          ServerAdmin webmaster@localhost
          DocumentRoot /var/www/html/your_project_directory
          Directory /var/www/html/your_project_directory
          Options Indexes FollowSymLinks
          AllowOverride All
          Require all granted
      </VirtualHost>
      

      保存并退出编辑器,然后启用站点:

      sudo a2ensite 000-default.conf
      sudo systemctl restart apache2
      
    • Nginx配置: 编辑 /etc/nginx/sites-available/default 文件,添加以下内容:

      server {
          listen 80 default_server;
          listen [::]: 80 default_server;
          root /var/www/html/your_project_directory;
          index index.php index.html index.htm index.nginx-debian.html;
          server_name _;
          location / {
              try_files $uri $uri/ =404;
          }
          location ~ \.php$ {
              include snippets/fastcgi-php.conf;
              fastcgi_pass unix:/run/php/php7.4-fpm.sock; # 根据你的PHP版本调整
          }
          location /\.ht {
              deny all;
          }
      }
      

      保存并退出编辑器,然后测试Nginx配置并重启Nginx:

      sudo nginx -t
      sudo systemctl restart nginx
      
  5. 部署PHP项目: 将你的PHP项目文件上传到服务器上的指定目录(例如 /var/www/html/your_project_directory)。你可以使用FTP、SCP或其他文件传输方法。

  6. 配置数据库(如果需要):

    sudo apt install mysql-server
    sudo mysql_secure_installation
    

    在PHP代码中使用PDO或MySQL扩展连接数据库。

  7. 测试部署: 打开浏览器并访问你的服务器IP地址或域名,确保你的PHP项目能够正常运行。

0