Easy Laravel Deployment on Ubuntu: A Beginner's Guide with LEMP Stack

MD ARIFUL HAQUE - Sep 19 - - Dev Community

Deploying a Laravel application on Ubuntu with the LEMP stack (Linux, Nginx, MySQL, PHP) can seem daunting, but breaking it down step by step makes it manageable. This guide will walk you through the process from server setup to deploying a Laravel application.

Prerequisites:

  • You should have an Ubuntu server (local or cloud, e.g., AWS, DigitalOcean).
  • Basic familiarity with the terminal.
  • A domain name (optional but recommended).

Part 1: Setting Up the LEMP Stack

Step 1: Update the System

Start by ensuring that your server is up to date.

sudo apt update && sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

Step 2: Install Nginx

Nginx will serve your application.

sudo apt install nginx -y
Enter fullscreen mode Exit fullscreen mode

Once installed, you can start and enable Nginx to run on boot:

sudo systemctl start nginx
sudo systemctl enable nginx
Enter fullscreen mode Exit fullscreen mode

You can verify that Nginx is running by visiting your server’s IP address in a browser.

Step 3: Install MySQL

Next, we’ll install the MySQL database server.

sudo apt install mysql-server -y
Enter fullscreen mode Exit fullscreen mode

Secure the MySQL installation:

sudo mysql_secure_installation
Enter fullscreen mode Exit fullscreen mode

This will prompt you to set up a root password and remove insecure defaults.

Step 4: Install PHP

Laravel requires PHP, so let's install it along with some necessary extensions:

sudo apt install php-fpm php-mysql php-cli php-xml php-mbstring php-curl php-zip -y
Enter fullscreen mode Exit fullscreen mode

Verify the PHP installation:

php -v
Enter fullscreen mode Exit fullscreen mode

You should see something like:

PHP 7.x.x (cli) (built: ...)
Enter fullscreen mode Exit fullscreen mode

Part 2: Configuring MySQL for Laravel

Step 1: Log in to MySQL

Log in to the MySQL console as the root user:

sudo mysql
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a Database

Create a new database and user for the Laravel application:

CREATE DATABASE laravel_app;
CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'strong_password';
GRANT ALL PRIVILEGES ON laravel_app.* TO 'laravel_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Enter fullscreen mode Exit fullscreen mode

Step 3: Test the Database

Ensure that the new database user can connect:

mysql -u laravel_user -p
Enter fullscreen mode Exit fullscreen mode

You’ll be prompted for the password, then enter:

SHOW DATABASES;
Enter fullscreen mode Exit fullscreen mode

You should see laravel_app in the list.


Part 3: Installing Laravel

Step 1: Install Composer

Laravel uses Composer as its dependency manager. Install Composer:

sudo apt install composer -y
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a Laravel Project

Navigate to the directory where you want to install Laravel (e.g., /var/www/):

cd /var/www/
composer create-project --prefer-dist laravel/laravel laravel_app
Enter fullscreen mode Exit fullscreen mode

Step 3: Set Directory Permissions

Laravel requires some directories to be writable by the web server:

sudo chown -R www-data:www-data /var/www/laravel_app
sudo chmod -R 775 /var/www/laravel_app/storage
sudo chmod -R 775 /var/www/laravel_app/bootstrap/cache
Enter fullscreen mode Exit fullscreen mode

Step 4: Configure .env File

In the Laravel project root, open the .env file and configure the database settings:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_app
DB_USERNAME=laravel_user
DB_PASSWORD=strong_password
Enter fullscreen mode Exit fullscreen mode

Part 4: Configuring Nginx for Laravel

Step 1: Create a New Nginx Server Block

We'll create an Nginx configuration file for the Laravel project.

sudo nano /etc/nginx/sites-available/laravel_app
Enter fullscreen mode Exit fullscreen mode

Add the following configuration to the file:

server {
    listen 80;
    server_name your_domain_or_ip;

    root /var/www/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.x-fpm.sock; # Change this to the correct PHP version.
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}
Enter fullscreen mode Exit fullscreen mode

Replace your_domain_or_ip with your actual domain name or server IP address.

Step 2: Enable the Nginx Configuration

Enable the new Nginx configuration by creating a symbolic link to sites-enabled:

sudo ln -s /etc/nginx/sites-available/laravel_app /etc/nginx/sites-enabled/
Enter fullscreen mode Exit fullscreen mode

Step 3: Test and Reload Nginx

Test the Nginx configuration to ensure there are no syntax errors:

sudo nginx -t
Enter fullscreen mode Exit fullscreen mode

If everything is fine, restart Nginx:

sudo systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

Part 5: Final Steps

Step 1: Run Laravel Migrations

Run the Laravel migrations to set up the database:

cd /var/www/laravel_app
php artisan migrate
Enter fullscreen mode Exit fullscreen mode

Step 2: Access the Application

You should now be able to access the Laravel application by navigating to your server’s IP or domain in the browser. You’ll see the default Laravel welcome page.

Step 3: Enable HTTPS (Optional but Recommended)

If you have a domain, secure your site with Let's Encrypt:

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d your_domain
Enter fullscreen mode Exit fullscreen mode

Follow the instructions to install an SSL certificate. Certbot will automatically configure Nginx to redirect HTTP traffic to HTTPS.


Part 6: Optional: Setting Up Laravel Queue and Scheduler

Laravel Queue:

Queues handle tasks like sending emails or processing jobs in the background.

  1. Set up a queue driver (e.g., Redis or database).
  2. Run the Laravel queue worker:
   php artisan queue:work
Enter fullscreen mode Exit fullscreen mode

Laravel Scheduler:

Use Laravel's task scheduling feature for tasks like clearing caches, sending daily emails, etc.

  1. Add the Laravel cron entry to your crontab:
   sudo crontab -e
Enter fullscreen mode Exit fullscreen mode

Add the following line:

   * * * * * php /var/www/laravel_app/artisan schedule:run >> /dev/null 2>&1
Enter fullscreen mode Exit fullscreen mode

Conclusion:

You’ve successfully deployed a Laravel application on an Ubuntu server using the LEMP stack. From here, you can continue to develop your application, secure it, and monitor it for performance.

If you encounter any issues, check the Nginx error logs at /var/log/nginx/error.log or Laravel logs at /var/www/laravel_app/storage/logs/laravel.log.

With these steps, you've completed a full hands-on Laravel deployment!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player