Transitioning from a Laravel Sail (development environment) to a production environment on a VPS can indeed be a bit challenging if you're not familiar with the process. Here’s a step-by-step guide to help you through this transition:
1. Prepare Your VPS
First, ensure your VPS is set up with the necessary software. You will need:
- A web server (e.g., Nginx or Apache)
- PHP (compatible version with your Laravel application)
- A database server (e.g., MySQL or PostgreSQL)
- Composer
- Git (optional, but recommended for deployment)
2. Set Up Your Web Server
For this example, let's use Nginx. Here’s a basic Nginx configuration for a Laravel application:
server {
listen 80;
server_name yourdomain.com;
root /var/www/your-laravel-app/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
3. Deploy Your Application
You can deploy your application using Git. Here’s a simple way to do it:
-
Clone your repository:
cd /var/www git clone https://github.com/yourusername/your-laravel-app.git cd your-laravel-app -
Install dependencies:
composer install --optimize-autoloader --no-dev -
Set up environment variables:
cp .env.example .env nano .envUpdate the
.envfile with your production settings. -
Generate application key:
php artisan key:generate -
Run migrations:
php artisan migrate --force -
Set permissions:
chown -R www-data:www-data /var/www/your-laravel-app chmod -R 775 /var/www/your-laravel-app/storage chmod -R 775 /var/www/your-laravel-app/bootstrap/cache
4. Configure Supervisor (for Queue Workers)
If your application uses queues, you’ll need to set up Supervisor to manage your queue workers.
-
Install Supervisor:
sudo apt-get install supervisor -
Create a Supervisor configuration file:
sudo nano /etc/supervisor/conf.d/your-laravel-app-worker.confAdd the following content:
[program:your-laravel-app-worker] process_name=%(program_name)s_%(process_num)02d command=php /var/www/your-laravel-app/artisan queue:work --sleep=3 --tries=3 autostart=true autorestart=true user=www-data numprocs=1 redirect_stderr=true stdout_logfile=/var/www/your-laravel-app/storage/logs/worker.log -
Start Supervisor:
sudo supervisorctl reread sudo supervisorctl update sudo supervisorctl start your-laravel-app-worker:*
5. Set Up SSL (Optional but recommended)
Use Certbot to obtain an SSL certificate from Let's Encrypt:
sudo apt-get install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com
6. Optimize Your Application
Run the following commands to optimize your Laravel application for production:
php artisan config:cache
php artisan route:cache
php artisan view:cache
Conclusion
Using Laravel Sail for development is beneficial because it provides a consistent environment that mirrors production as closely as possible. However, for production, you typically want a more tailored setup that is optimized for performance and security. By following the steps above, you can transition from a Sail-based development environment to a robust production environment on a VPS.