Deployment of Laravel Web App on Linux(VPS or VM) through Apache Webserver

Setting up the server environment, configuring the application, and deploying the code are the stages involved in deploying a Laravel application on Ubuntu. Assuming that you have a basic LAMP (Linux, Apache, MySQL, PHP) stack installed on your Ubuntu server, the following step-by-step instructions are provided.

Step 1: Prepare Your Server

  • Update System Packages:

      sudo apt update
      sudo apt upgrade
    

    Install Necessary Packages:

      sudo apt install apache2 mysql-server php libapache2-mod-php php-mysql unzip
    

Step 2: Configure MySQL

Secure MySQL Installation:

sudo mysql_secure_installation
  • Create a Database and User:
sudo mysql -u root -p   #by default root password is "root"
CREATE DATABASE your_database_name;
CREATE USER 'your_username'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_username'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 3: Install Composer

Download and Install Composer:

sudo apt install curl php-cli php-mbstring unzip
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

Step 4: Configure Apache for Laravel

Enable Required Apache Modules:

sudo a2enmod rewrite

Create a Virtual Host Configuration:

sudo nano /etc/apache2/sites-available/your_domain.conf

Add the following configuration (replace your_domain with your actual domain or IP address):

<VirtualHost *:80>
    ServerAdmin webmaster@your_domain
    DocumentRoot /var/www/html/your_laravel_project/public

    <Directory /var/www/html/your_laravel_project>
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

If you want attach SSL certificate then make .conf file which have configuration for

listening port 443.

sudo nano /etc/apache2/sites-available/your_domain-ssl.conf

Add configuration mention below:

Note: Add exact address your "SSL certificate" and "key" according to your machine.

<VirtualHost *:443>
        SSLEngine on
        SSLCertificateFile /etc/ssl/server_chain.crt
        SSLCertificateKeyFile /etc/ssl/server.key
        ServerName example.com
        ServerAlias www.example.com
        DocumentRoot /var/www/your_project/public

        <Directory /var/www/your_project>
                AllowOverride All
        </Directory>
</VirtualHost>

Save and exit.

  • Enable the Virtual Host and Restart Apache:

      sudo a2dissite default.conf
      sudo a2ensite your_domain.conf
      sudo a2ensite your_domain-ssl.conf
      sudo systemctl restart apache2
      sudo systemctl reload apache2
    

Step 5: Deploy Your Laravel Application

  • Clone or Upload Your Laravel Code:

      cd /var/www/html/
      git clone https://github.com/your_username/your_laravel_project.git
    
  • Install Composer Dependencies:

      cd your_laravel_project
      composer install
    
  • Configure Environment Variables:

      cp .env.example .env
      nano .env
    

    Update the database connection details and other necessary settings.

  • Generate Application Key:

      php artisan key:generate
    
  • Run Database Migrations:

      php artisan migrate
    
  • Set Proper Permissions:

      sudo chown -R www-data:www-data /var/www/html/your_laravel_project
      sudo chmod -R 755 /var/www/html/your_laravel_project/storage
    
  • Optimize Laravel:

      php artisan optimize
    

Step 6: Finalize

Restart Apache:

sudo systemctl restart apache2
sudo systemctl reload apache2

At this point, your server's IP address or domain should be able to access your Laravel application. Ensure that your DNS settings are configured appropriately.