“`html

Laravel is an open-source PHP web application framework. Its toolkits, libraries, and features help improve development efficiency since users don’t need to code all functionality from scratch. This popular framework has a large community of developers, enhancing support and resource availability. However, new users might be unsure how to deploy Laravel due to its various dependencies and commands. In this tutorial, we will explain how to deploy the framework on an Ubuntu virtual private server (VPS). This Laravel deployment guide will also explore several tips that help simplify the task.

How to Deploy Laravel Project

In this section, we will explain the steps for deploying Laravel on VPS hosting running Ubuntu 22.04. Depending on the Linux distro and version, the commands may differ.

  1. Choose the Right VPS Hosting
    • Before getting started with Laravel, purchase a virtual private server hosting plan. When choosing the best VPS hosting plan and provider for your project, consider these aspects:
      • Compatibility. Your VPS must support the chosen operating system, web server, Laravel, the latest version of PHP, and other dependencies to run properly.
      • Access. The host server must provide full root access via a secure shell (SSH) connection to enable seamless and secure Laravel deployment.
      • Performance. The ideal VPS hosting environment should provide sufficient bandwidth and server resources to ensure the Laravel application performs optimally.
      • Additional features. Some web hosting providers have features that help simplify the application deployment process, like a control panel and a one-click installer.
    • Hostinger VPS hosting plans are compatible with various Linux distributions, software, and frameworks, including Laravel. Users can install an operating system via hPanel in one click. Moreover, all our VPS plans provide full root access to avoid permissions-related issues. Users can also use our built-in Browser terminal to connect to their remote server. It is more convenient than an SSH client since you can run Linux commands directly from your web browser. Starting at $5.99/month, our VPS solution offers up to 400 GB of NVMe storage, 8 TB of bandwidth, 32 GB of RAM, and 8 vCPU cores, suitable for large-scale enterprise projects. All our plans also come with a 30-day money-back guarantee.
  2. Prepare the Server for Laravel
    • After purchasing and setting up your VPS, connect remotely via SSH. You can use an SSH application like PuTTY, Terminal, or Hostinger’s Browser terminal. Log in to your server using the root user. On Hostinger VPS, access the credentials by going to hPanel → VPS on the top menu. Then, navigate to your VPS plan and head to Overview → the SSH access tab. Important! Instead of root, we recommend creating a new account with superuser privilege to avoid accidental destructive command execution. Laravel requirements include a web server, database, PHP databases, and Composer. You’ll also need other software like Git to push the application files from your local computer to the remote machine. Here are the steps:
      1. Update your APT package manager repository using this command: sudo apt update && sudo apt upgrade
      2. Install the web server. In this tutorial, we’ll use Apache: sudo apt install -y apache2
      3. Run this command to set up PHP and its extensions: sudo apt install -y php php-cli php-fpm php-mysql php-xml php-mbstring
      4. Configure a database. We’ll install MySQL using the following command: sudo apt install -y mysql-server
      5. Install Composer to manage Laravel dependencies using this command: sudo apt install -y composer
      6. Enter this command to set up the Git version control system: sudo apt install -y git

      If you don’t want to manually set up the production server, purchase Laravel Forge. Starting at $12.99/month, this platform provides a centralized area for installing and managing all applications required for your project.

  3. Deploy the Application
    • Create a virtual host for your Laravel project by setting up a web server configuration file using the nano text editor. For Apache, run the following command: sudo nano /etc/apache2/sites-available/laravel.conf
    • Important! The configuration file content will look different if you use another web server like NGINX. Deploy Laravel application project code from your Git repository by running these commands in a series. Replace the URL with the actual link:
      • cd /var/www/html
      • sudo git clone

      If you haven’t pushed your Laravel project files to a repository, read our Git tutorial to learn more about how to do so. To deploy the project files, you can use methods like the rsync utility or a secure file transfer protocol (SFTP) application. However, we recommend version control in Laravel deployment for convenience and security. Navigate to your project directory path and run composer install to configure the dependencies: cd test-laravel

    • If Terminal returns error messages, try running the composer update command. Also, add the --ignore-platform-req=ext-curl option to see if the issue resolves.
  4. Configure the .env File
    • When you create a Laravel application, Composer will generate a .env.example file template. It stores environment-specific configurations, determining various variables for your applications, like database information. To set up a .env file for your Laravel project, copy the template below and open it using nano: sudo cp .env.example .envsudo nano .envImportant! By default, Laravel will set the DB_USERNAME as root and leave the DB_PASSWORD empty. However, it might cause an error when you run PHP artisan, the framework command-line utility, for database migration. To resolve it, change the default root password and update the DB_PASSWORD value accordingly. Also, we recommend creating a non-default database and users specific to your current project for easier management.
  5. Run the Application
    • To start your Laravel application, make the project folder executable by changing the permissions and ownership: sudo chown -R www-data /var/www/html/test-laravel sudo chmod -R 755 /var/www/html/test-laravel/storage Run PHP artisan to generate the application key and initiate the database migration. Here are the commands: sudo php artisan key:generate sudo php artisan migrate Whenever you modify the database schema, update the migration files to maintain consistent configuration for your app. Check out Laravel’s migration guide to learn more about it. Disable the virtual host’s default configuration file in Apache using the following command: sudo a2dissite 000-default.conf Enable the new virtual host and the rewrite module using these commands: sudo a2ensite laravel.conf sudo a2enmod rewrite Restart Apache using the systemctl command: sudo systemctl restart apache2 Once the deployment process is complete, enter your VPS IP address or domain name into a web browser to check whether the application works correctly. If the files are empty, you should see the Laravel welcome page.

Laravel Deployment Tips and Tricks

In this section, we will discuss several Laravel deployment best practices to help streamline the process.

Use Caching Configuration in Laravel

The Laravel framework lets you store frequently accessed data as cache. It reduces the time required to fetch the information and offloads database queries, improving your web application performance. It also provides various caching backends and drivers. Users can choose one that best suits their needs and configure it easily through the .env file’s CACHE_DRIVER key. If you don’t specify the cache driver, Laravel will use file by default. Alternatively, you can set it to array, database, redis, or memcached. To enable cache, add the method to your application’s codebase. For example, write Cache::put() to store data for a specified duration. Meanwhile, use the Cache:rememberForever() method to store the cache indefinitely. This framework provides many caching methods for different purposes, like retrieving, removing, or incrementing the stored data. To manage the cache, you can run the PHP artisan commands in your project’s working directory. For example, enter the following to remove the stored data: php artisan cache:clear

Use Automatic Laravel Deployment Tools

Automating Laravel deployment helps save time and effort since without it, the process involves many steps. There are various tools and platforms for this task. You can use continuous integration/continuous deployment (CI/CD) services like GitHub Actions…

“`