Laravel is a prominent member of a new generation of web frameworks. It is an open source PHP framework and is getting more attention from developers. It is intended for the development of web applications following the model–view–controller architectural pattern.
Using Laravel, we can develop scalable, feature-rich and secure websites and applications and can deliver the completed projects within a short period of time. This ensures the long-term relationship between you and your clients. The current latest version of Laravel is 5.7.
Now coming to Vultr Cloud Compute(VC2), it features 100% SSD and latest generation Intel CPUs. Vultr’s 100% SSD and 100% Intel compute plans are engineered to deliver both speed and reliability our cloud application can depend on. Beyond all the above features, it is the cheapest cloud platform available right now.
Here we are going to deploy a Laravel 5.7 application on Vultr VC2. We’ll be using the latest LTS version of Ubuntu, which is 18.04.1 LTS on our cloud instance.
SETTING UP AN UBUNTU SERVER ON VC2
- From your Vultr Dashboard, Goto Servers tab on the left side.
- Press Deploy new server FAB button on the top right corner.
- Now select a suitable Server Location.
- Select Ubuntu 18.04 as Server Type.
- Choose a Server size with IPV4 address.
- Now Enter to your console.
SETTING UP LARAVEL ON UBUNTU SERVER
1. INSTALL APACHE2
Laravel requires a web server to run. The commonly used web servers today are, Apache2 and NGINX. In this guide, we are using Apache2. You can install Apache2 with the below command.
sudo apt-get install apache2
2. INSTALL PHP AND IT’S EXTENSIONS
Since Laravel is a PHP framework, PHP and some of its extensions need to be installed.
sudo apt-get install php libapache2-mod-php php-common php-mbstring php-xmlrpc php-soap php-gd php-xml php-mysql php-cli php-mcrypt
3. INSTALL MYSQL
Laravel also need a database server. Most using database with PHP is Mysql.
sudo apt-get install mysql-server mysql-client sudo mysql_secure_installation
When prompted, answer the questions below by following the guide.
Enter current password for root (enter for none): (Press Enter) Set root password? [Y/n]: Y New password: (Enter password for mysql DB) Re-enter new password: (Repeat password) Remove anonymous users? [Y/n]: Y Disallow root login remotely? [Y/n]: Y Remove test database and access to it? [Y/n]: Y Reload privilege tables now? [Y/n]: Y
In the latest version of MySQL, it rollbacks the support for the root user. So, you can not migrate the database with root user. So that we need to create a new user and grant all privileges to the user.
sudo mysql --user=root mysql CREATE USER 'username'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' WITH GRANT OPTION; EXIT;
4. INSTALL PHPMYADMIN
phpMyAdmin is a free software tool written in PHP, intended to handle the administration of MySql over the Web. It can be installed using the command below.
sudo apt-get install phpmyadmin
It will ask for your MySQL credentials and you have to give it properly.
5. INSTALL COMPOSER
The composer is an application-level package manager for PHP that provides a standard format for managing dependencies of PHP software and required libraries. It helps us installing/updating various requirements/components for our app. The composer can be installed with the below command.
sudo apt-get install composer
6. CLONE YOUR LARAVEL PROJECT
Now we can clone our project from git repository to /var/www/html directory of our server. we also need to run composer install to install all composer dependencies required for our project.
git clone [ link_to_your_git_repository ]
7. SETTING PERMISSIONS FOR THE PROJECT DIRECTORY
We have to set permissions for working with this project directory since as by default the system may only allow us to change the contents in our project directory as root.
sudo chown -R www-data:www-data /var/www/html/laravel sudo chmod -R 755 /var/www/html/laravel/storage
8. CREATING DATABASE
Now create a database on the Mysql server which will be the DB of our Laravel application. This can be done using MySQL commands below.
mysql -u username -p CREATE DATABASE dbname; USE dbname; EXIT;
9. CONFIGURE THE APPLICATION ENVIRONMENT
In this step, we’ll modify some security-related application settings, allow the application to connect to the database, and prepare the database for usage. Edit the .env file in your project folder. If there is no .env file in your project, we have to create a file and paste all the contents from sampe.env to the file created and save the file with a .env filename.
APP_ENV=production APP_DEBUG=false DB_DATABASE=(The database name you created earlier) DB_USERNAME=(Your Mysql username. Usually root) BD_PASSWORD=(Your Mysql password)
10. MIGRATE DATABASE
Migrate the database to our system using the command below.
php artisan migrate
11. ACCESSING YOUR LARAVEL APP
Now On your browser, enter
http://[ ipv4_address_off_your_server ]/[project_directory]/public
And this will lead us to the home page of Laravel application we have created.
12. CREATE VIRTUALHOST IN APACHE SERVER
To serve our app to a connected domain, we have to add the lines below to our /etc/apache2/sites-enabled file.
<VirtualHost *:80> ServerAdmin [email protected] ServerName phalcon.example.com DocumentRoot /var/www/[project_directory]/public <Directory /var/www/[project_directory]/> Options all AllowOverride all </Directory> </VirtualHost>
Then rewrite the URL using Apache 2’s mod_rewrite module.
sudo a2enmod rewrite sudo service apache2 restart
Now we can access our app using the server IP.
http://[ YOUR_IP_ADDRESS ]
We can also connect a domain to our server’s IP.