DevOps

Deploy Laravel with Nginx on Ubuntu 20 (LEMP Stack)

Deploying a Laravel application on Ubuntu 20 with Nginx (LEMP stack) from git can be a bit more involved than a simple deployment, but it’s still a manageable process. Here are the detailed steps to deploy your Laravel application on an Ubuntu 20 server using Nginx and git:

  1. Install Nginx: The first step is to install Nginx on your Ubuntu 20 server. This can be done by running the following command in the terminal:
sudo apt-get update sudo apt-get install nginx

This command will update the package list on your server and install the Nginx package.

  1. Install PHP: Laravel is built on PHP, so we will need to install PHP and the necessary extensions to run Laravel. This can be done by running the following command:
sudo apt-get install php-fpm php-mysql

This command installs PHP and the php-fpm and php-mysql modules, which are necessary to run PHP with Nginx.

  1. Install git: git is a version control system used for tracking changes in the code. You can install git by running the following command
sudo apt install git
  1. Clone your Laravel repository: Once you have git installed, you can clone your Laravel repository by running the following command
git clone https://github.com/yourusername/yourrepo.git
  1. Configure Nginx: Next, we will need to configure Nginx to work with our Laravel application. Create a new server block in the Nginx configuration file by running:
sudo nano /etc/nginx/sites-available/yourdomain.com

In this file, you can specify the server name, root directory, and other settings for your application.

  1. Set the correct permissions: Make sure that the storage and bootstrap/cache directories are writable by the web server.
sudo chmod -R 777 storage bootstrap/cache
  1. Install Composer: Laravel uses Composer to manage its dependencies, so we will need to install it. This can be done by running the following command:
sudo apt-get install composer
  1. Run Composer: Run Composer in your project folder to install all the dependencies:
composer install
  1. Create the .env file: Laravel uses the .env file to store environment-specific configuration. You can copy the .env.example to .env and set the necessary configurations like database, mail, and other settings.

  2. Generate the application key: Laravel uses the application key for security, so we will need to generate one. This can be done by running the following command:

php artisan key:generate
  1. Test and Restart: Test your application by visiting your domain. If everything is working, restart Nginx and enjoy your deployed Laravel application.
sudo service nginx restart

Please note that the above is a general guide and you may need to adjust the steps depending on your specific server setup. Also, make sure to configure your domain DNS to point to your server IP.

The process is a bit complex but with the above steps, you should be able to deploy your Laravel app with Nginx on Ubuntu 20 (LEMP stack) from git successfully.