DEV Community

WHAT TO KNOW
WHAT TO KNOW

Posted on

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

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

This comprehensive guide will lead you through the process of deploying a Laravel application on a Ubuntu server using the LEMP stack. This guide aims to be a beginner-friendly resource, covering every step from setting up the server to deploying your Laravel application.

Introduction

Laravel, a powerful PHP framework, has become a popular choice for building modern web applications. Its elegant syntax, robust features, and extensive ecosystem make it a developer's delight. However, deploying a Laravel application to a live server can seem daunting for beginners. This is where the LEMP stack comes in.

LEMP Stack Explained

LEMP is an acronym that stands for:

  • **Linux:** The operating system, providing a stable foundation.
  • **Nginx:** A high-performance web server responsible for handling HTTP requests.
  • **MySQL:** A relational database management system for storing application data.
  • **PHP:** The scripting language that powers your Laravel application.

Why Use LEMP for Laravel Deployment?

The LEMP stack is a well-established and widely used combination for hosting web applications, offering several benefits:

  • Performance: Nginx is renowned for its high performance and ability to handle large volumes of traffic efficiently.
  • Security: Both Nginx and MySQL have built-in security features to protect your application from vulnerabilities.
  • Open-Source: The entire LEMP stack is open-source, making it free to use and customize.
  • Community Support: A vast community of developers actively contributes to the LEMP stack, providing ample support and resources.

Setting Up the LEMP Stack on Ubuntu

This section guides you through the installation and configuration of the LEMP stack on your Ubuntu server.

1. Install Nginx

Use the following command to install Nginx:


sudo apt update
sudo apt install nginx

2. Install MySQL

Install MySQL using the following command:


sudo apt install mysql-server

After installation, secure your MySQL installation by running the provided script:


sudo mysql_secure_installation

3. Install PHP

Install PHP and required extensions for Laravel:


sudo apt install php7.4 php7.4-fpm php7.4-mysql php7.4-curl php7.4-gd php7.4-mbstring php7.4-xml php7.4-zip

4. Configure Nginx for Laravel

Create a new Nginx server block file for your Laravel application. For example, if your domain is "example.com," create the file at `/etc/nginx/sites-available/example.com`:


server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/html/your-laravel-project/public;

    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

Enable the server block:


sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com

Test and reload Nginx:


sudo nginx -t
sudo systemctl reload nginx

5. Configure PHP-FPM

Open the PHP-FPM pool configuration file:


sudo nano /etc/php/7.4/fpm/pool.d/www.conf

Modify the following settings:


listen = 127.0.0.1:9000
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
user = www-data
group = www-data

Restart PHP-FPM:


sudo systemctl restart php7.4-fpm

Deploying Your Laravel Application

Now that the LEMP stack is set up, you can deploy your Laravel application to the server.

1. Clone Your Laravel Project

Use Git to clone your Laravel project repository to the server:


cd /var/www/html
git clone  your-laravel-project

2. Create a Database

Log into MySQL and create a new database for your application:


mysql -u root -p
CREATE DATABASE your_database_name;

3. Configure Database Credentials

Open the `.env` file in your Laravel project directory and configure the database credentials:


DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=root
DB_PASSWORD=your_mysql_password

4. Install Composer Dependencies

Navigate to your Laravel project directory and install the required Composer dependencies:


cd /var/www/html/your-laravel-project
composer install

5. Generate Application Keys

Generate a unique application key for your Laravel application:


php artisan key:generate

6. Run Migrations and Seeders

If you have database migrations, run them to set up your database tables:


php artisan migrate

If you have database seeders, run them to populate your database with initial data:


php artisan db:seed

7. Set File Permissions

Set the correct file permissions for your Laravel project:


sudo chown -R www-data:www-data /var/www/html/your-laravel-project
sudo chmod -R 755 /var/www/html/your-laravel-project

8. Access Your Laravel Application

Open your web browser and visit your domain name (e.g., http://example.com) to access your deployed Laravel application.

Deployment Automation with Tools

While manual deployment is suitable for small projects, using automation tools can streamline the process for larger applications. Some popular tools include:

  • Ansible: A powerful automation engine that simplifies deploying and managing infrastructure, including Laravel applications.
  • Docker: Containerization technology that packages your Laravel application and its dependencies into a portable container, simplifying deployment across different environments.
  • Laravel Forge: A platform-as-a-service solution specifically designed for deploying and managing Laravel applications.

Challenges and Limitations

While the LEMP stack is an excellent choice for Laravel deployment, you may encounter some challenges:

  • Security: Keeping your server secure requires vigilance and regular security updates.
  • Performance Optimization: Optimizing performance for high traffic websites requires careful configuration and tuning of the LEMP stack components.
  • Scaling: Scaling your application to handle increased traffic may require advanced techniques like load balancing and caching.

Comparison with Alternatives

While LEMP is a popular choice, there are alternative stacks suitable for deploying Laravel applications:

  • LAMP: Uses Apache instead of Nginx. Apache is more flexible but often less performant than Nginx.
  • LEMP with a different database: PostgreSQL or MariaDB can replace MySQL, depending on your application's requirements.
  • Serverless Platforms: Platforms like AWS Lambda or Google Cloud Functions allow you to deploy your Laravel application as serverless functions, eliminating server management entirely.

Conclusion

This guide has equipped you with the knowledge and steps to deploy your Laravel application on an Ubuntu server using the LEMP stack. Remember, continuous learning and adaptation are essential in the ever-evolving world of web development.

Further Learning

To enhance your knowledge, explore these resources:

Call to Action

Start deploying your Laravel application on an Ubuntu server today. Experiment with the LEMP stack and explore automation tools to streamline your deployment process. Embrace the power of Laravel and unlock the potential of building sophisticated web applications.

Top comments (0)