In this guide, we look at how to install Laravel framework on Ubuntu 18.04 server. Laravel is a free and open source PHP web framework. It was created in 2011 by Taylor Otwell and has since been used for development of robust mobile applications using the MVC model.
Let’s dive in and see how we can install Laravel framework on Ubuntu 18.04 LTS Server.
Before we install Laravel framework, let’s first install the prerequisite packages that will be required.
System prerequisites
Your system will need to satisfy the requirements below before proceeding.
- Apache Web server
- PHP >= 7.1.3 with OpenSSL, PDO, Mbstring, Tokenizer, XML, Ctype and JSON PHP Extensions.
- Composer – an application-level package manager for the PHP
1. Install Apache Web server and PHP 7.2
We are going to add a third party PHP repository. Even though Ubuntu’s own repository has its own PHP, a third party repository is very much recommended because it gets more frequently updated.
To add the repository execute the command:
1 |
$ sudo add-apt-repository ppa:ondrej/php |
Sample Output
Next, update your system’s repositories.
1 |
$ sudo apt update |
Sample Output
Next, install Apache and PHP 7.2 alongside other prerequisites.
1 |
$ sudo apt-get install apache2 libapache2-mod-php7.2 php7.2 php7.2-xml php7.2-gd php7.2-opcache php7.2-mbstring |
Sample Output
2. Install Composer
Before we embark on the installation, we will first install a few useful tools. These are git version control, curl and unzip packages.
1 |
$ sudo apt install curl git unzip |
Sample Output
Next, we need to install composer. The composer is another useful tool that handles dependency management in PHP and allows you to package the required libraries associated with a package as one. Composer will download and install all the packages required to run the Laravel framework.
To install Composer, execute the following commands
1 2 3 |
$ cd /opt $ curl -sS https://getcomposer.org/installer | php |
Sample Output
The curl command downloads Composer to the /opt
directory. Since we need composer running globally, we must move it to the /usr/local/bin
directory under 'composer'
name.
1 |
$ mv composer.phar /usr/local/bin/composer |
This will allow us to run composer from anywhere.
3. Install Laravel Framework
Now, let’s navigate to the public_html directory of our Ubuntu System. To install Laravel, we will navigate to the /var/www/html
directory.
1 |
cd /var/www/html |
Next, we will create a directory “your-project” with Laravel installation. The composer will proceed to use Git to download and install all packages and modules that Laravel requires for functioning.
1 |
$ sudo composer create-project laravel/laravel your-project --prefer-dist |
Sample Output
4. Configure Apache Web Server for Laravel
The next step is to configure our Apache Web server. We need to assign the necessary permissions to the project directory which will allow access to it from the www-data
group and give it write permissions to the storage directory. To get this done, run the following commands.
1 2 3 4 |
# sudo chgrp -R www-data /var/www/html/your-project # sudo chmod -R 775 /var/www/html/your-project/storage |
Now let’s navigate to /etc/apache2/sites-available
directory and run the command below to create a configuration file for our Laravel install.
1 |
$ vim /etc/apache2/sites-available/laravel.conf |
Add the following content:
1 2 3 4 5 6 7 8 |
ServerName localhost ServerAdmin webmaster@localhost DocumentRoot /var/www/html/your-project/public AllowOverride All ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined |
Save the file and Exit.
Finally, we are going to enable the newly created laravel.conf
file. But before that, let’s disable the default config file.
1 |
$ sudo a2dissite 000-default.conf |
Sample Output
Next, enable the Laravel config file.
1 |
$ sudo a2ensite laravel.conf |
Sample Output
Then enable rewrite mode:
1 |
$ sudo a2enmod rewrite |
Sample Output
Lastly, restart the Apache service.
1 |
$ sudo systemctl restart apache2 |
To verify that Apache is running execute the command:
1 |
systemctl status apache2 |
Sample Output
5. Test Laravel Website
At this point, You have successfully installed Laravel on your Ubuntu 18.04 LTS System. To confirm that the installation went as expected visit your server’s IP address.
1 |
https://server-IP-address |
In my case, the server’s IP is https://38.76.11.149
We hope that this guide has been helpful. Feel free to leave your feedback at the comment section.