Linux VPSLaravelServer ManagementUbuntu

Installing Laravel with LEMP Stack on Ubuntu

Laravel is a widely acclaimed PHP web application framework known for its elegant syntax and a plethora of features that make web development a breeze. When combined with the LEMP stack (Linux, Nginx, MySQL, and PHP), you have a powerful and efficient environment for hosting Laravel applications. In this comprehensive guide, we will walk you through the step-by-step process of installing Laravel on an Ubuntu server with a LEMP stack.

Prerequisites

Before you begin, ensure you have the following prerequisites in place:

  1. An Ubuntu-based server or desktop system (we’ll use Ubuntu 20.04 for this guide).
  2. A user account with sudo privileges.
  3. LEMP stack installed and configured as described in our previous guide. If you haven’t already, you can find the guide here.

Step 1: Preparing Your Ubuntu Server

Before you can start with Laravel, ensure your Ubuntu server is up-to-date. Open a terminal and run the following commands:

sudo apt update

The sudo apt update command is used to refresh the package information on a Debian-based Linux system. It ensures that your system’s package manager (APT, which stands for Advanced Package Tool) has the latest information about available packages and their versions from the configured software repositories.

Then Upgrade:

sudo apt upgrade

The sudo apt upgrade command is used to upgrade the installed packages on a Debian-based Linux system, such as Ubuntu. This command will search for updates to your installed packages and, if any updates are available, it will prompt you to confirm the upgrade. It is a good practice to regularly run this command to keep your system up to date with the latest software updates and security patches.

Step 2: Installing Composer

Composer is a critical tool for PHP development and is essential for Laravel. You’ll use Composer to manage dependencies and packages for your Laravel project. Install Composer globally by following these commands:

First go to base directory:

cd ~ 

Then download composer using curl:

curl -sS https://getcomposer.org/installer -o composer-setup.php 

Move composer file to /usr/local/bin dir folder and set filename to composer:

sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

With Composer installed, you can now manage Laravel’s dependencies and easily incorporate packages into your application.

Step 3: Installing Laravel

With Composer in place, you can proceed to install Laravel. Use the Composer create-project command to set up a new Laravel project. Run the following command:

composer create-project --prefer-dist laravel/laravel your-app-name

Replace your-app-name with your desired project name. This command will create a Laravel project in a directory named after your project.

Once the installation is complete, navigate to your Laravel project’s directory:

cd your-app-name

Step 4: Configuring Nginx for Laravel

To serve your Laravel application, you’ll need to configure Nginx. Create an Nginx server block configuration file for your project:

sudo nano /etc/nginx/sites-available/laravel

Inside this file, insert the following configuration:

server {
     listen 80; 
     server_name your-domain.com www.your-domain.com;
     root /var/www/your-app-name/public; index index.php;
     location / { 
          try_files $uri $uri/ /index.php?$query_string;
      } 
     location ~ \.php$ {
          include snippets/fastcgi-php.conf; 
          fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; 
     } 
     location ~ /\.ht { 
          deny all; 
     } 
}

Replace your-domain.com with your domain name and your-app-name with the directory where your Laravel project is located. This configuration instructs Nginx to serve your Laravel application, passing PHP requests to the PHP-FPM process.

To enable this configuration, create a symbolic link in the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/

Now, test the Nginx configuration for syntax errors:

sudo nginx -t

If the test is successful, restart Nginx to apply the changes:

sudo systemctl restart nginx

Step 5: Configuring Permissions

For Laravel to work correctly, you need to adjust some file permissions to ensure that Nginx can read and write to the necessary directories. Run the following commands:

sudo chown -R www-data:www-data /var/www/your-app-name
sudo chmod -R 755 /var/www/your-app-name/storage

These commands make the web server user (www-data) the owner of the Laravel project files and ensure appropriate directory permissions.

Step 6: Laravel Environment Configuration

Edit the .env file in your Laravel project directory to configure your application’s environment settings, including the database connection, app key, and other configuration options. You can use a text editor or the nano command:

nano .env

Edit the file according to your specific requirements and environment settings.

Step 7: Generating an Application Key

Laravel requires an application key for encryption and security-related tasks. To generate an application key, run the following command from your Laravel project directory:

php artisan key:generate

This command will create a secure, random key and update your application’s .env file.

Step 8: Testing Your Laravel Installation

With Laravel installed and configured, you can now access your website in a web browser. If you’ve set up your DNS records to point to your server’s IP address, simply visit your domain name. You should see the Laravel welcome page, confirming that your installation is successful.

Conclusion

You’ve successfully installed Laravel on an Ubuntu server with a LEMP stack, setting the stage for your web application development journey. Laravel’s elegant syntax and powerful features make it a top choice for modern web development. You can now begin building and customizing your Laravel application, utilizing its extensive functionality to create efficient and scalable web applications. Good luck with your Laravel project!

By following these detailed steps, you’ve laid the foundation for a robust Laravel web application. Enjoy the benefits of Laravel’s features and the efficiency of the LEMP stack as you develop your web project.

IQBAL HASAN

I love to talk on trending ⚡ technology ⚡, I have huge attraction on 😁 Linux 😅 and I am a bit 😄 mad 😄 when I code.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button