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

Mary-Kate Olsen
Release: 2024-09-20 06:55:32
Original
559 people have browsed it

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
Copy after login

Step 2: Install Nginx

Nginx will serve your application.

sudo apt install nginx -y
Copy after login

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

sudo systemctl start nginx
sudo systemctl enable nginx
Copy after login

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
Copy after login

Secure the MySQL installation:

sudo mysql_secure_installation
Copy after login

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
Copy after login

Verify the PHP installation:

php -v
Copy after login

You should see something like:

PHP 7.x.x (cli) (built: ...)
Copy after login

Part 2: Configuring MySQL for Laravel

Step 1: Log in to MySQL

Log in to the MySQL console as the root user:

sudo mysql
Copy after login

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;
Copy after login

Step 3: Test the Database

Ensure that the new database user can connect:

mysql -u laravel_user -p
Copy after login

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

SHOW DATABASES;
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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;
    }
}
Copy after login

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/
Copy after login

Step 3: Test and Reload Nginx

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

sudo nginx -t
Copy after login

If everything is fine, restart Nginx:

sudo systemctl reload nginx
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

Add the following line:

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

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!

The above is the detailed content of Easy Laravel Deployment on Ubuntu: A Beginner&#s Guide with LEMP Stack. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!