在Ubuntu上实现Laravel的分布式部署可以通过多种方式来完成,以下是一个基本的步骤指南,使用Nginx和PHP-FPM来实现分布式部署。
首先,确保你的Ubuntu系统是最新的,并且安装了必要的软件包。
sudo apt update
sudo apt upgrade -y
sudo apt install nginx php-fpm php-mysql mariadb-server git -y
创建一个新的Nginx配置文件来处理Laravel应用。
sudo nano /etc/nginx/sites-available/laravel
添加以下配置:
server {
listen 80;
server_name yourdomain.com;
root /var/www/laravel;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 根据你的PHP版本调整
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
启用这个配置文件:
sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl restart nginx
编辑PHP-FPM配置文件以优化性能。
sudo nano /etc/php/7.4/fpm/pool.d/www.conf
找到并修改以下行:
listen = /var/run/php/php7.4-fpm.sock
listen.owner = www-data
listen.group = www-data
重启PHP-FPM服务:
sudo systemctl restart php7.4-fpm
将你的Laravel应用克隆到服务器上。
cd /var/www
sudo git clone https://github.com/your-repo/laravel-app.git
cd laravel-app
sudo chown -R www-data:www-data .
安装依赖:
sudo apt install composer -y
cd laravel-app
composer install --no-dev --optimize-autoloader
生成应用密钥:
php artisan key:generate
配置环境变量:
cp .env.example .env
nano .env
根据你的数据库配置修改.env文件中的数据库连接信息。
运行迁移和数据填充(如果有):
php artisan migrate
php artisan db:seed
如果你有多个服务器实例,可以使用Nginx的负载均衡功能。
编辑Nginx配置文件:
sudo nano /etc/nginx/nginx.conf
在http块中添加负载均衡配置:
upstream laravel_app {
server server1.example.com;
server server2.example.com;
# 添加更多服务器
}
server {
listen 80;
server_name yourdomain.com;
root /var/www/laravel;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
重启Nginx服务:
sudo systemctl restart nginx
确保所有服务器实例都连接到同一个数据库和缓存服务(如Redis或Memcached)。
使用Let’s Encrypt为你的域名配置SSL证书。
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yourdomain.com
按照提示完成SSL证书的安装和配置。
通过以上步骤,你可以在Ubuntu上实现Laravel的分布式部署。根据你的具体需求,可能还需要进行一些额外的配置和优化。