"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Easy Laravel Deployment on Ubuntu: A Beginner&#s Guide with LEMP Stack

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

Published on 2024-11-01
Browse:184

Easy Laravel Deployment on Ubuntu: A Beginner

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

Step 2: Install Nginx

Nginx will serve your application.

sudo apt install nginx -y

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

sudo systemctl start nginx
sudo systemctl enable nginx

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

Secure the MySQL installation:

sudo mysql_secure_installation

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

Verify the PHP installation:

php -v

You should see something like:

PHP 7.x.x (cli) (built: ...)

Part 2: Configuring MySQL for Laravel

Step 1: Log in to MySQL

Log in to the MySQL console as the root user:

sudo mysql

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;

Step 3: Test the Database

Ensure that the new database user can connect:

mysql -u laravel_user -p

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

SHOW DATABASES;

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

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

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

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

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

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;
    }
}

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/

Step 3: Test and Reload Nginx

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

sudo nginx -t

If everything is fine, restart Nginx:

sudo systemctl reload nginx

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

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

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

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

Add the following line:

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

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!

Release Statement This article is reproduced at: https://dev.to/mdarifulhaque/easy-laravel-deployment-on-ubuntu-a-beginners-guide-with-lemp-stack-155a?1 If there is any infringement, please contact [email protected] delete
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3