Step-By-Step Guide to Deploying Laravel Applications on Virtual Private Servers

Posted on

Laravel is a PHP framework ideal for developing websites and applications. To host these creations, users need a robust Laravel web hosting infrastructure, ensuring their web projects are accessible globally. The Laravel framework is well-suited for mission-critical web projects that demand enterprise-level hosting capabilities.

Bluehoster provides dedicated hosting and VPS hosting solutions tailored for these demanding projects. We understand the importance of a strong web hosting infrastructure that facilitates scaling websites and applications seamlessly. Our VPS hosting plans feature unmetered bandwidth, a 1 Gbps network, SSD NVMe storage, and a choice of 28 data center locations.

We offer managed VPS hosting services, and our team can assist with managed plans for your Laravel projects hosted on our Virtual Private Servers. However, if you are comfortable with unmanaged web hosting, this step-by-step guide will walk you through ‘How to Run a Laravel Project’ on a Virtual Private Server.

Prior Requirements

This guide assumes you have the necessary technical expertise to manage virtual private servers, including familiarity with Linux command lines and the Ubuntu operating system. Once you can perform relevant tasks on the command terminal, the following steps will help you learn ‘how to run a Laravel project’ on a VPS.

Steps to Follow to Run Laravel Project On VPS

1. Server’s Provisioning

First, you need to establish your virtual private server to host the Laravel project. SSH (Secure Shell) is essential for initiating the setup, enabling connections to a remote server via a password or key pair. The following command generates new key pairs on machines:

ssh-keygen -t rsa

Basic knowledge of the Terminal and Unix-like operating systems is recommended before starting. The provided command-line codes should function with any Linux distribution on your VPS using standard Terminal syntax.

Update the package list: Use the command below to update the package list on your VPS:

sudo apt-get update && sudo apt-get upgrade

Installation of PHP is straightforward. Execute the following:

sudo apt-get install php

The latest PHP version and several extensions will be installed. Verify the version installed using:

php –v

To install a specific version like PHP 7.4, you’ll utilize a PPA (Personal Package Archive) from Ondřej Surý. This method of installing older PHP versions is secure. Run each of these commands sequentially:

sudo apt install software-properties-common

sudo add-apt-repository ppa:ondrej/php

sudo apt-get update

To install a specific version like PHP 8.0, use the following command:

sudo apt install php8.0

You can replace `8.0` with any desired PHP version.

2. VPS Provisioning

Add our VPS hosting plan to your cart, including add-on features such as backup, DDoS protection, and website backups. After purchase, you will receive login credentials to access the server and databases.

Set up Composer

Follow these steps to set up Composer:

$ cd ~

$ mkdir bin

$ cd bin

$ curl -sS https://getcomposer.org/installer | php

Set up Envoy

Here’s a basic configuration for your `Envoy.blade.php` file:


// Envoy.blade.php
@setup
    $branch = isset($branch) ? $branch : "master";
    $serverUser = 'deployer';
    $rootDirectory = '~/home/' . $serverUser;
    $server = $serverUser . '@server_ip';
@endsetup

@servers(['production' => $server])

@task('clone', ['on' => 'production'])
    echo '>> cd {{ $rootDirectory }}'
    cd {{ $rootDirectory }}
    echo '>> mkdir {{ $rootDirectory }}/project'
    mkdir {{ $rootDirectory }}/project
    echo '>> chmod 755 {{ $rootDirectory }}/project'
    chmod 755 {{ $rootDirectory }}/project
    echo '>> cd {{ $rootDirectory }}/project'
    cd {{ $rootDirectory }}/project
    echo '<< git clone git@github.com:username/project.git deploy'
    git clone git@github.com:username/project.git deploy
@endtask

@task('environment', ['on' => 'production'])
    echo '>> cd {{ $rootDirectory }}/project/deploy'
    cd {{ $rootDirectory }}/project/deploy
    echo '<< cp .env.example .env'
    cp .env.example .env
    echo '>> SSH to your server, paste your valid .env credentials & save them. Then run envoy run post-deploy'
@endtask

@task('composer-install', ['on' => 'production'])
    echo '>> cd {{ $rootDirectory }}/project/deploy'
    cd {{ $rootDirectory }}/project/deploy
    echo '<< /home/{{ $serverUser }}/bin/composer.phar install --prefer-dist --no-scripts --no-dev -q -o'
    /home/{{ $serverUser }}/bin/composer.phar install --prefer-dist --no-scripts --no-dev -q -o
@endtask

@task('composer-update', ['on' => 'production'])
    echo '>> cd {{ $rootDirectory }}/project/deploy'
    cd {{ $rootDirectory }}/project/deploy
    echo '<< /home/{{ $serverUser }}/bin/composer.phar dump -o && php artisan optimize'
    /home/{{ $serverUser }}/bin/composer.phar dump -o && php artisan optimize
@endtask

@task('migrate', ['on' => 'production'])
    echo '>> cd {{ $rootDirectory }}/project/deploy'
    cd {{ $rootDirectory }}/project/deploy
    php artisan migrate --force;
@endtask

@task('symlink', ['on' => 'production'])
    echo '<< ln -s /home/{{ $serverUser }}/project/deploy/public /var/www/html'
    ln -s /home/{{ $serverUser }}/project/deploy/public /var/www/html
@endtask

@task('deploy-changes', ['on' => 'production'])
    echo '>> cd {{ $rootDirectory }}/project/deploy'
    cd {{ $rootDirectory }}/project/deploy
    echo '>> git checkout {{ $branch }}'
    git checkout {{ $branch }}
    echo '<< git pull --rebase'
    git pull --rebase
@endtask

@story('deploy', ['on' => 'production'])
    setup
    environment
@endstory

@story('post-deploy', ['on' => 'production'])
    composer-install
    composer-update
    migrate
    symlink
@endstory

@story('update')
    deploy-changes
    composer-update
    migrate
@endstory


Deploy

Use this task for the initial launch on your remote server:

$ envoy run deploy

The initial task completion will display a message that the `.env` file is being prepared.

Set up .env

Ensure your `.env` configuration includes the correct credentials for your server. Copy and paste your local `.env` to the server, then save it.

Final Deploy

After saving the server’s `.env` file, execute:

$ envoy run post-deploy

Finishing

The deployment script is designed to ensure your application remains stable and prevent any downtime during deployments.

3. Basic Setup Process

Once your VPS server is running smoothly, connect via SSH using your server’s IP as the root user. You can find your IP address online. Example:

ssh root@104.248.157.172

Using key pairs for login is more secure than passwords. To add a new user on the terminal, use:

adduser nonroot

Run this command on your server. Afterward, create a sudo group with:

usermod -aG sudo nonroot

Once the basic setup is complete, it’s time to install PHP and other necessary software.

Step 2: Set up a VPS instance

After choosing a provider, create a new VPS instance with an OS (like Ubuntu or CentOS) that supports PHP, MySQL, and other necessary software. Configure the server’s firewall, SSH access, and any other security settings.

Step 3: Install the necessary software

Install PHP, MySQL, Nginx or Apache, and Composer. Use package managers like `apt` or `yum`. To install Composer, run the following:

curl -sS https://getcomposer.org/installer | php

sudo mv composer.phar /usr/local/bin/composer

  • Install Node.js and npm with:

sudo apt-get install nodejs npm

  • Install the Laravel installer by running sudo composer global require laravel/installer.
  • Add Laravel to your PATH using:

echo ‘export PATH=”$PATH:$HOME/.composer/vendor/bin”‘ >> ~/.bashrc

  • Refresh your shell:

source ~/.bashrc

Step 4: Configure the server

Configure the server for Laravel by creating a database, configuring the web server, and setting environment variables. You may need to install additional PHP extensions like `mbstring` and `pdo_mysql`. Install MySQL using the following:

sudo apt install mysql-server –y

Then, secure it using:

sudo mysql_secure_installation

Step 5: Deploy the application

Deploy your Laravel application using Git or similar version control systems. Set up the correct database credentials and environment variables. You also might need to set up a domain name and SSL certificate to secure your laravel application.

Use deployment tools like Envoyer or Deployer to automate deployments and minimize downtime. Using a CDN like Cloudflare or Akamai can cache static assets to reduce server load. Implement security best practices like setting up firewalls, regular software updates, and strong passwords with encryption.

Installing name server

Installing web server on Linux VPS

We will use Apache as an example. Follow these steps to install on your Linux VPS server:

Log in to your Linux VPS via SSH.

Update the package manager:

sudo apt-get update

Install Apache:

sudo apt-get install apache2

Start the Apache service:

sudo systemctl start apache2

Check the Apache service status:

sudo systemctl status apache2

Enable the Apache service to start automatically:

sudo systemctl enable apache2

Open the firewall to allow incoming HTTP and HTTPS traffic:

sudo ufw allow http

sudo ufw allow https

Verify the Apache web server is running by:

# /etc/init.d/apache2 status

Restart Apache using

# /etc/init.d/apache2 restart

Conclusion

Deploying Laravel applications on Virtual Private Servers requires technical skill, but it provides greater control and flexibility compared to shared hosting. By following these steps, you can establish a robust and scalable server for your Laravel application. When it comes to hosting, Bluehoster’s VPS hosting with its reliable infrastructure makes Laravel hosting hassle-free. Opt for our dependable web hosting to effectively scale your web project.

Leave a Reply

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