Write Review

How To Installing, Using and Unistalling Laravel on Ubuntu 18.04 with Apache

What is Laravel?

Laravel is a free PHP framework that offers a host of unique features, making it one of the most popular PHP web frameworks. These features include multiple ways to approach relation databases, utility helping in the development and maintenance of applications, a dedicated dependency manager present in a dedicated package, and the list goes on. Laravel is probably the most efficient and user-friendly PHP framework on the internet.

Why Laravel?

This question will certainly strike many people as there are loads of PHP frameworks available. The problem arises when you are documenting the application code. The problem with most of the PHP frameworks is that their structure ignores the source code of the application and doesn’t read it. This is where Laravel comes in with Apache.

So without further ado let’s get started with the installation of Laravel with Apache on Ubuntu 18.04.

Installation

  1. First things first, type SSH into your VPS server.
  2. Apache, an HTTP server tool, is required for Laravel to function. It is very likely that it is already installed on your VPS. Use the following command to check if Apache is already installed on you VPS:

sudo systemctl status apache2

If Apache is already installed, you can directly skip to the next step. If your VPS doesn’t already have Apache installed, you can install it by using the following command:

sudo apt install apache2

If you have a firewall installed, it can handicap the smooth functioning of Apache. If you don’t have Firewall installed feel free to skip to the next step. But if you have, you’ll have to add a rule in the Firewall. Run the following command:

sudo ufw allow 443

Sudo ufw allow 80

This will do the work. Enter the following command to check the status:

sudo systemctl status apache2

Installing, Using and Unistalling Laravel on Ubuntu 18.04 with Apache

Visit the IP address of your server on a browser and if you see the following page there then yahoo! Apache is up and running.

Installing, Using and Unistalling Laravel on Ubuntu 18.04 with Apache

  1. Another requirement for Laravel is PHP. Since Ubuntu’s official repositories already contain PHP 7, all you need to do is install the language and a few more repositories. Run the following command to see if all the packages are present or not:
sudo apt install php libapache2-mod-php php-mbstring php-xmlrpc php-soap php-gd 
php-xml php-cli php-zip php-bcmath php-tokenizer php-json php-pear

Run the following command if some packages are not present and then perform step 3 again:

apt-get update

So now you have PHP installed but you still need to make sure now is that it’s working properly. You’ll have to create a new file in Apache’s root directory to do this. Then use the following command after replacing “filename” with the name of your file:

sudo nano /var/www/html/testphp.php

Now you’ll have to add the call to the function phpinfo. Use the following command for this:

<?php

phpinfo();

?>

Press Ctrl + O to save and then Ctrl + X to exit.

To check if PHP is up and working you can visit http://your_server_IP/testphp.php

(here testphp.php is the name of the new file you created in Apache’s root directory) and you should see this:Installing, Using and Unistalling Laravel on Ubuntu 18.04 with Apache

  1. A database manager is a must to develop on Ubuntu. For the sake of this tutorial, we’ll be installing MariaDB but feel free to use any of the databases supported by Laravel.

Run the following command to install MariaDB:

sudo apt install mariadb-server

Feel free to skip this step but if you wish to secure your root run the following command:

sudo mysql_secure_installation

Then you’ll be asked a few configuration questions:

Remove anonymous users? [Y/n] y

Disallow root login remotely? [Y/n] n

Remove test database and access to it? [Y/n] y

Reload privilege tables now? [Y/n] y
  1. You’ll also need to install Composer. It will help facilitate the download of PHP libraries. Use the following command to download composer:
curl -sS https://getcomposer.org/installer | php

Use the following commands to make composer global:

sudo mv composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer

 

  1. Now you’re ready to install Laravel. To install Laravel run the following command after replacing [your_project_name] with the name of your project:
composer create-project --prefer-dist laravel/laravel [your_project_name]

Yahoo! You’ve finally installed Laravel. Now we’ll guide on how to use Laravel to locally develop applications or to deploy applications.

-  local development

Use the following commands after replacing [IP] with your server IP and [port] with the port you want to use, to specify the host and port of your server which need to be done if you wish to locally develop applications:

cd example

php artisan serve --host=[IP] --port=[port]

To check, visit the IP address of your server and the port you mentioned and you should see the following screen:

Installing, Using and Unistalling Laravel on Ubuntu 18.04 with Apache

-deploy applications

To your VPS as the server of your Laravel application, you’ll need to move your project directory to the Apache webroot. Use the following command after replacing example with the name of the folder:

sudo mv example /var/www/html/

But that’s not all. There are certain permissions that need to be taken care of. The following commands should take care of it:

sudo chgrp -R www-data /var/www/html/example/

sudo chmod -R 775 /var/www/html/example/storage

Now you’ll have to create a new virtual host for the project. Run the following commands for this:

cd /etc/apache2/sites-available

sudo nano laravel_project.conf

Run the following code and save the file, after replacing exampledomain.com with the name of your domain and IP address with the IP address of your server, to create new virtual hosts:

   ServerName exampledomain.com

   ServerAdmin webmaster@exampledomain.com

   DocumentRoot /var/www/html/example/public

   <Directory /var/www/html/example>

       AllowOverride All

   </Directory>

   ErrorLog ${APACHE_LOG_DIR}/error.log

   CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Run the following commands to disable the configuration file of the default hosts and enable the new virtual host respectively:

sudo a2dissite 000-default.conf

sudo a2ensite laravel_project

Almost done. Now all you need to do is to enable the rewrite module of Apache and restart the Apache service afterward. The following commands shall take care of this:

sudo a2enmod rewrite

sudo systemctl restart apache2

To check if everything worked, visit your server’s IP and see if you see the same Laravel screen you saw last time.

Uninstalling Laravel and Composer

Delete the folder of the project generated and this shall uninstall Laravel. And the following command is to uninstall Composer:

sudo rm /usr/local/bin/composer

We hope our guide helped you in the development of rich web applications :)

Was this article helpful?