linux

如何在Linux系统中部署ThinkPHP应用

小樊
53
2025-08-31 15:43:12
栏目: 编程语言

在Linux系统中部署ThinkPHP应用,可以按照以下步骤进行:

1. 准备工作

2. 安装Web服务器

Apache

sudo apt update
sudo apt install apache2

Nginx

sudo apt update
sudo apt install nginx

3. 安装PHP环境

sudo apt update
sudo apt install php php-cli php-fpm php-mysql php-zip php-gd php-mbstring php-curl php-xml php-pear php-bcmath

4. 配置Web服务器

Apache

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

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    <Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</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;
    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:/var/run/php/php7.4-fpm.sock; # 根据PHP版本调整
    }

    location ~ /\.ht {
        deny all;
    }
}

重启Nginx:

sudo systemctl restart nginx

5. 部署ThinkPHP应用

  1. 下载ThinkPHP应用

    cd /var/www/html
    git clone https://github.com/top-think/thinkphp.git myapp
    cd myapp
    
  2. 配置数据库: 编辑application/database.php文件,配置数据库连接信息。

  3. 安装Composer依赖

    composer install
    
  4. 运行应用

    • 使用PHP内置服务器(仅用于开发环境):
      php run start
      
    • 使用Nginx和PHP-FPM(生产环境): 确保Nginx配置正确,然后访问你的域名或IP地址。

6. 配置防火墙

确保防火墙允许HTTP和HTTPS流量:

sudo ufw allow 'Nginx Full'

7. SSL证书(可选)

如果你需要HTTPS,可以使用Let’s Encrypt免费获取SSL证书:

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com

8. 监控和日志

通过以上步骤,你应该能够在Linux系统中成功部署ThinkPHP应用。

0
看了该问题的人还看了