How To Set Up A Laravel Application on Ubuntu 16.04 VPS

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

The post How To Set Up A Laravel Application on Ubuntu 16.04 VPS appeared first on Low End Box.

How To Install Laravel on Ubuntu 16.04 VPS

How To Install Laravel on Ubuntu 16.04 VPS

With this tutorial, you will start from a clean Ubuntu 16.04 install, set it up to run Laravel, and end up with a demo Laravel application installed. Other articles in this series will show you how to set up the database and Laravel environment, and then how to strenghten the Laravel setup for production.

;

What we are going to cover

  • installation of Apache and PHP 7.2,
  • creation of user which will have access to the app,
  • installation of Laravel itself,
  • setting up Apache to execute the app.

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

Step 1: Creating a Non Root User

Once you are logged in as root, you can create a new user account that you’ll use to access the app once it is installed. We’ll call the new user laraveluser. To create it, run:

adduser laraveluser

Then, add it to the sudo group, so that you can run commands as sudo:

usermod -aG sudo laraveluser

Step 2: Install Apache

First, update your package manager’s cache:

sudo apt update -y

Install the Apache web server:

sudo apt install apache2 -y

Enable its service to make it run on every system boot:

sudo systemctl enable apache2

Finally, start it:

sudo systemctl start apache2

To verify that Apache was installed successfully, access it from your local browser by navigating to http://SERVER_IP/. If that does not work, try adding :80 in the end, like this:

http://SERVER_IP:80

You should see a welcome page for Apache, which means that you now have Apache running.

Step 3: Install PHP 7.2

We are installing version 5.8 of Laravel, which requires a version of PHP greater than 7.1.3. We shall install PHP 7.2. First install the prerequisite packages:

sudo apt-get install software-properties-common python-software-properties

Then, add the ondrej PPA:

sudo add-apt-repository -y ppa:ondrej/php
sudo apt-get update

and update your sources by running:

sudo apt install php -y

Install PHP 7.2 using the following command:

sudo apt-get install php7.2 php7.2-cli php7.2-common

Step 4 Install the Extensions For Laravel

These are the PHP extensions that Laravel 5.8 requires:

  • OpenSSL PHP Extension
  • PDO PHP Extension
  • Mbstring PHP Extension
  • Tokenizer PHP Extension
  • XML PHP Extension
  • Ctype PHP Extension
  • JSON PHP Extension
  • BCMath PHP Extension

First install the most popular PHP extensions:

sudo apt-get install php7.2-curl php7.2-gd php7.2-json  php7.2-mbstring php7.2-intl php7.2-mysql php7.2-xml php7.2-zip

Then add the extensions specific to Laravel:

sudo apt install -y php7.2-bcmath  php7.2-ctype

Restart to activate changes:

sudo systemctl restart apache2

Linux uses tar to decompress files while some of the files for PHP come in zip format, so here is the command to install zip and unzip extensions as well:

sudo apt -y install zip

Step 6 Install git

We shall use git to install Laravel apps from github.com.

sudo apt install -y git

Step 5 Install redis

Redis is a database, cache and message in-memory broker. We are not going to use it in this tutorial, though, but we can install it now.

sudo apt install -y redis-server

Step 6: Install Composer

Laravel uses Composer to manage all of its dependencies.

Install curl to download the install script for Composer:

sudo apt install curl -y

Then, download the installer:

cd ~
sudo curl -s https://getcomposer.org/installer | php

To make it available globally, move it to /usr/bin:

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

You are installing Composer as root user and it will show a warning about it. Once we have the app installed, we shall give access to its folder to user laraveluser.

Step 7: Create an Example Laravel App

Apache stores the data it serves under /var/www/html. To create a new Laravel app, run:

composer create-project --prefer-dist laravel/laravel /var/www/html/blog

Here “blog” will be the name of the app.

When Composer asks if you want it to set up folder permissions, answer with y.

Run these two commands to enable access to folders storage and bootstrap/cache as without that access, Laravel will not run:

sudo chmod -R a+rw /var/www/html/blog/storage
sudo chmod -R a+rw /var/www/html/blog/bootstrap/cache

We can now give user laraveluser access to the app folder:

sudo chown -R laraveluser /var/www/html/blog

Ubuntu will ask you for passwords whenever you change or access something in the system so be sure to have them nearby.

Step 8: Configuring Apache

Edit Apache configuration file so that it will serve Laravel app by default. It is called 000-default.conf and is stored in directory /etc/apache/sites-enabled. Open it for editing:

sudo nano /etc/apache2/sites-enabled/000-default.conf

Find line which starts with DocumentRoot and replace it with this:

DocumentRoot /var/www/html/blog/public

Save and close the file.

Laravel requires Apache modulerewrite_ to also be enabled; do so by running:

sudo a2enmod rewrite

Finally, instruct Apache to use .htaccess files, with which Laravel configures Apache on the fly. Open Apache global configuration file for editing:

sudo nano /etc/apache2/apache2.conf

Under the block, you’ll find the following line:

AllowOverride None

Change it to

AllowOverride All

When you are done, save the file.

Again restart Apache so that it takes new configuration into account:

sudo systemctl restart apache2

You can now navigate to your domain in your browser. You should see the following:

Laravel is now installed properly.

What To Do Next

Now you have a Laravel app on your server. The server is set up but Laravel itself is not. Follow the article “How To Set Up a Laravel Application on Ubuntu 16.04” which will show you how to create a database, connect it to the app and set up environment for a Laravel app properly. This will enable you to further develop and test your app on this server.

When your app becomes ready for deployment and production, read the next article in the series, on “How To Set Up Laravel For Production: Ubuntu 16.04”.

Dusko Savic is a technical writer and Flutter programmer.

duskosavic.com

The post How To Install Laravel on Ubuntu 16.04 VPS appeared first on Low End Box.