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 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. The latest version of PHP is php7.2 and it is also supported by Laravel 5.7. So can install the php7.2 and it’s extensions.
sudo apt install php7.2-common php7.2-cli php7.2-gd php7.2-mysql php7.2-curl php7.2-intl php7.2-mbstring php7.2-bcmath php7.2-imap php7.2-xml php7.2-zip
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.
We can access the phpmyadmin panel using the link below.
http://[ ipv4_address_off_your_server ]/phpmyadmin
if it is giving an error “The requested URL /phpmyadmin was not found on this server”, do the below steps.
- Open apache2.conf file using nano editor.
sudo -H nano /etc/apache2/apache2.conf
2. Paste the following line to the end of the file.
3. Restart apache server
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 the 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>
Now we can assess the home page of our Laravel application. But the routes will not work. For this,
Edit /etc/apache2/apache2.conf, changing the “AllowOverride”
directive for the /var/www directory : AllowOverride All
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.