Laravel is a PHP framework for creating websites and applications. And to host them, users require Laravel web hosting infrastructure because, only in this way can they make web projects accessible worldwide. Laravel framework allows the development of mission-critical web projects that meet enterprise hosting requirements.
MilesWeb has dedicated hosting and VPS hosting services for such crucial web projects. After all, we want a robust web hosting infrastructure that helps scale websites and applications. Our VPS hosting services comprise unmetered bandwidth, a 1 Gbps network, SSD NVMe storage and 28 data centers location.
We offer managed VPS hosting services. If you want your Laravel projects to be hosted on our Virtual Private Servers, we can assist you with managed plans! However, if you are satisfied with unmanaged web hosting plans, here is the step by step guide to answer ‘How to Run a Laravel Project?’ on Virtual Private Servers.
Prior Requirements
Referring to the above information, we assume that you have the prior technical expertise to manage virtual private servers. You must know Linux command lines with an Ubuntu operating system. Once you are able to conduct all relevant tasks on the command terminal, underlying steps will help you know ‘how to run a Laravel project’ on VPS.
Steps to Follow to Run Laravel Project On VPS
1. Server’s Provisioning
First, you must set up a virtual private server to run the Laravel project on it. Then, SSH (Secure Shell) protocol is required to initiate the setup. This protocol lets users connect to a remote server using a password or key pair. Below is the command to generate new key pairs on machines.
ssh-keygen -t rsa
You should be familiar with the Terminal and how to operate Unix-like OSes in general before starting. These command line codes should work on any Linux distribution of VPS that uses the standard Terminal syntax.
Update the package list: Run the following command to update the package list on your VPS:
sudo apt-get update && sudo apt-get upgrade
Now you can install PHP. It is straightforward and effortless to do so.
sudo apt-get install php
PHP and several extensions will be installed along with the latest version of PHP. This code will tell you what version you have.
php –v
If you want to install PHP 7.4, how do you do it? It is necessary to use a PPA, or a Personal Package Archive, created by Ondřej Surý. Installing older PHP versions this way is safe. One by one, run these three commands:
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
Now that PHP 8.0 is recognized by your system, run the following command:
sudo apt install php8.0
Any PHP version can be substituted for this number.
2. VPS Provisioning
Add our VPS hosting plans to the cart and include all relevant add-on features like backup, DDoS protection, website backup and others. After that, you will get all log in credentials to access the server and its databases.
Set up Composer
Here are the steps for setting up a composer.
$ cd ~
$ mkdir bin
$ cd bin
$ curl -sS https://getcomposer.org/installer | php
Set up Envoy
// 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
During the initial launch phase of your application on the remote server, you will only need to run this task.
$ envoy run deploy
A message is displayed at the end of the initial task, informing the user that the .env file is being prepared.
Set up .env
Make sure that the .env configuration contains the necessary credentials for your application to run on the server. Copy and paste the .env file to the server where the .env file is located, and save it.
Final Deploy
Run another command after saving the server .env file.
$ envoy run post-deploy
Finishing
You may be wondering if the deployment script could potentially cause your application to break and result in downtime during any of the deployments.
3. Basic Setup Process
If your VPS server runs hassle-free, use SSH with an IP address to log in as a root user. You can check your IP address online. Here is an example of how can implement it:
ssh root@104.248.157.172
Logging in to the client using key pair is more secure than using a user id and password. Below is the command to add a new user to the terminal.
adduser nonroot
Execute this command in your server. Then, create a sudo group using the following command:
usermod -aG sudo nonroot
After deploying the basic setup, installing PHP and other required software on your server is time. Here are the rest of the steps that we will discuss.
Step 2: Set up a VPS instance
Once you’ve chosen a provider, create a new VPS instance and choose an operating system (such as Ubuntu or CentOS) that supports PHP, MySQL, and other required software. Configure the server’s firewall, SSH access, and other security settings.
Step 3: Install the necessary software
Next, install the necessary software on the server, such as PHP, MySQL, Nginx or Apache, and Composer. You can use package managers like apt or yum to install these packages automatically. Install Composer, a dependency manager for PHP, by running the following commands:
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
- Install Node.js and npm by running the command
sudo apt-get install nodejs npm.
- Install the Laravel installer by running the command sudo composer global require laravel/installer.
- Add Laravel to your PATH environment variable by running the command
echo ‘export PATH=”$PATH:$HOME/.composer/vendor/bin”‘ >> ~/.bashrc.
- Refresh your shell by running the command source
~/.bashrc
Step 4: Configure the server
Configure the server to work with Laravel by setting up a database, configuring the web server, and setting environment variables. You may also need to install additional PHP extensions, such as the mbstring and pdo_mysql extensions, to support Laravel’s requirements. You can install MySQL on the server using the command:”
sudo apt install mysql-server –y
Then, secure your MySQL database, and execute the following command:
sudo mysql_secure_installation
Step 5: Deploy the application
Finally, deploy your Laravel application to the server using Git or other version control systems. Configure the application to use the correct database credentials and environment variables. You may also need to set up a domain name and SSL certificate to secure your laravel application.
Use a deployment tool, such as Envoyer or Deployer, to automate the deployment process and minimize downtime. Consider using a CDN, such as Cloudflare or Akamai, to cache static assets and reduce server load. Implement security best practices, such as setting up firewalls, updating software regularly, and using strong passwords and encryption.
Installing name server
Installing web server on Linux VPS
For this tutorial, we will consider the Apache web server example. Let us discuss step by step how to install it on Linux VPS servers.
Log in to your Linux VPS via SSH using your terminal program.
Update the package manager on your VPS using the following command:
sudo apt-get update
Install the Apache web server using the following command:
sudo apt-get install apache2
Once the installation is complete, start the Apache service using the following command:
sudo systemctl start apache2
Check the status of the Apache service to ensure it’s running properly:
sudo systemctl status apache2
Enable the Apache service to start automatically on system boot:
sudo systemctl enable apache2
Open up the firewall to allow incoming HTTP and HTTPS traffic by running the following commands:
sudo ufw allow http
sudo ufw allow https
Verify that the Apache web server is working by the following command
# /etc/init.d/apache2 status
Run the following command to restart Apache.
# /etc/init.d/apache2 restart
Conclusion
Deploying Laravel applications on Virtual Private Servers requires some technical knowledge, but it can provide more control and flexibility than shared hosting. Following these steps, you can set up a reliable and scalable server for your Laravel application. And when it comes to hosting these web projects, MilesWeb’s VPS hosting with robust infrastructure makes Laravel hosting hassle-free. Thus, choose our reliable web hosting to scale your web project.