How to Deploy a Laravel 11 Application to Server

To Deploy a Laravel 11 application to a server involves several steps. Let’s go through a practical example tutorial on how to do this:

Step 1: Prepare your Laravel application

First, make sure your Laravel application is ready for deployment. Ensure that you have:

  • Set up your Laravel application locally and tested it.
  • Included a .env file with the appropriate environment configurations.
  • Configured your database settings.

Step 2: Set up your server

For this example, we’ll use a server running Ubuntu 20.04 and Nginx.

  1. SSH into your server:
   ssh user@your_server_ip
  1. Update your server:
   sudo apt update && sudo apt upgrade
  1. Install Nginx, PHP, and MySQL:
   sudo apt install nginx php php-fpm php-mysql mysql-server

Step 3: Configure Nginx

  1. Create a new Nginx server block:
   sudo nano /etc/nginx/sites-available/your_domain
  1. Add the following configuration:
   server {
       listen 80;
       server_name your_domain.com;
       root /var/www/your_laravel_app/public;

       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;
       }
   }
  1. Enable the server block:
   sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
  1. Test Nginx configuration:
   sudo nginx -t
  1. Reload Nginx:
   sudo systemctl reload nginx

Step 4: Deploy your Laravel application

  1. Copy your Laravel application to the server:
   scp -r /path/to/your/laravel/app user@your_server_ip:/var/www/your_laravel_app
  1. Install composer dependencies:
   cd /var/www/your_laravel_app
   composer install --no-dev
  1. Set permissions:
   sudo chown -R www-data:www-data /var/www/your_laravel_app
   sudo chmod -R 755 /var/www/your_laravel_app/storage
  1. Generate application key:
   php artisan key:generate

Step 5: Set up the database

  1. Create a new database and user:
   mysql -u root -p
   CREATE DATABASE your_database;
   CREATE USER 'your_user'@'localhost' IDENTIFIED BY 'your_password';
   GRANT ALL PRIVILEGES ON your_database.* TO 'your_user'@'localhost';
   FLUSH PRIVILEGES;
  1. Update your .env file with the database details:
   DB_CONNECTION=mysql
   DB_HOST=127.0.0.1
   DB_PORT=3306
   DB_DATABASE=your_database
   DB_USERNAME=your_user
   DB_PASSWORD=your_password

Step 6: Finalize

  1. Migrate your database:
   php artisan migrate
  1. Optimize Laravel for production:
   php artisan config:cache
   php artisan route:cache
   php artisan view:cache
  1. Restart Nginx:
   sudo systemctl restart nginx

Now your Laravel 11 application should be successfully deployed to your server and accessible through your domain.

Leave a Reply