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.
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:
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:
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
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:
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:
Change it to
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.