How To Set Up A Laravel Application on Ubuntu 16.04 VPS
Let us presume that you have already set up an Ubuntu 16.04 VPS server with Apache and that you have installed a bare bones Laravel app. In this tutorial we shall install and create a MySQL database and connect the app to it. The final step is to fully configure the application via the .env file and artisan.
;
What we are going to cover
- Creation of MySQL database for the app,
- Connecting Laravel app to the database
- Creating the .env file for Laravel environment variables
- Using artisan to generate the app key
- Additional configuration for timezone and locale
PRE-REQUISITES
We shall install and deploy Laravel 5 on Ubuntu 16.04:
- Starting with a clean VPS with
- At least 512Mb of RAM and
- 15Gb of free disk space.
- You will need root user access
- You have already installed Laravel on your VPS and it is running properly
Step 1: Install MySQL Database
First use SSH to access the VPS server. To install MySQL database, run the following command from Ubuntu command line:
sudo apt install mysql-server -y
This will install the MariaDB database server (an enhanced fork of MySQL). You will be asked to enter password for the MySQL root user. (Use Tab key from the keyboard to switch to the OK button and press Enter on the keyboard.)
Then secure MySQL installation by running:
sudo /usr/bin/mysql_secure_installation
Depending on the level of security you wish to achieve, you will have the option to adjust the minimum password complexity. Press 2 to select the highest level. Answer y
to every prompt you get afterwards.
So you enter one password first, to enable access to MySQL, then ener another password to secure the installation. Store that second password as you will need it in Step 9 of this article.
To make it run on every system boot, enable it via systemctl:
sudo systemctl enable mysql
Step 2 Creating a Database
Launch MySQL shell:
sudo mysql -u root -p
When asked, enter the second password from Step 1 of this tutorial.
Once in MySQL prompt, copy and paste the following code as a whole, then press Enter on the keyboard:
CREATE DATABASE blogdb;
USE blogdb;
CREATE TABLE posts (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(50),
body TEXT,
created DATETIME DEFAULT NULL,
modified DATETIME DEFAULT NULL
);
INSERT INTO posts (title, body, created)
VALUES ('Sample title', 'This is the article body.', NOW());
GRANT ALL PRIVILEGES ON blogdb.* TO 'post_user'@'localhost' IDENTIFIED BY 'password9I%';
FLUSH PRIVILEGES;
EXIT;
That will execute everything at once. Here is what it will look like in the terninal:
The first line will create a database blogdb and from the second line on, will start using it. Then we create table called posts with two visible fields, title and body. Column idis necessary but will be used by only internally.
Then we populate table posts by inserting one sample value.
Command GRANT ALL PRIVILEGES creates a new user called postuser_ and grants it all privileges on database blogdb. Currently there is only one table in this database, posts.
Command IDENTIFIED BY defines the password. It must contain lower and uppercase letters, as well as digits and special characters. Be sure to always change and invent new passwords for database users.
Command FLUSH PRIVILEGES; reloads the database with the changes made. The last command is EXIT, to leave the MySQL prompt and go back to the command line in Ubuntu itself.
Step 3 The .env File
Navigate to the app folder and execute the ls -a command:
cd /var/www/html/blog/
ls -a
The following image shows the contents of the blog folder. Note that there are files starting with dot, such as .end, which is not visible with the usual ls command. Using ls -a will, however, show the dot files as well.
The fact that the dot files are “invisible” will not stop us from reading these files into an editor and changing them to finish the installation. The .env file contains debugging options, parameters to connect to the database and so on. So let us open it for editing:
sudo nano /var/www/html/blog/.env
Step 4 Connect the App To the Database
Put the following into the .env file to enable access to the database you have created in previous steps:
DB_DATABASE=blogdb
DB_USERNAME=post_user
DB_PASSWORD=assword9I%
Save and close the file.
The .env File May Be Missing
In some installations and scenarios, the .env file may be missing. You can download it from here, then create the missing .env file by opening it in an editor such as nano and copying the contents there.
With every installation there will be a file called .env.example so the other way to recreate the .env file is to use .env.example as a template, and create the .env files by copying:
cd /var/www/html/blog
cp .env.example .env
Step 5 Creating the Key for the Application
With each change of the the .env file, there will be several artisan commands to execute. As a minimum, you will need to run php artisan key:generate. If you don’t, you will get a warning screen like this:
So execute the following commands in a row:
php artisan key:generate
php artisan config:cache
composer dump-autoload
If you are changing the database, run the migration commands as well:
php artisan migrate
Step 6 Additional Configuration
In file config/app.php you can set up timezone and locale parameters. They look like this:
The command to access the file is:
sudo nano /var/www/html/blog/config/app.php
What To Do Next
Now you have a rudimentary Laravel app on your server, running smoothly.Once you develop your app and test it, you may aim for a production level of system. See article “How To Set Up Laravel For Production: Ubuntu 16.04” to improve your site before going to production phase.
Dusko Savic is a technical writer and Flutter programmer.
duskosavic.com
Related Posts:
- World Host Group Expands Again with Nimbus Hosting Acquisition - October 30, 2024
- OVHcloud’s 2024 Annual Performance: The Growth Continues - October 29, 2024
- Looking for a VPN?We’ve Got a Thread for You - December 7, 2021
Leave a Reply