Install Laravel 5.7, PHP 7.3 and Apache2 on Ubuntu

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, avrg: 5.00)

Laravel is a prominent member of a new generation of web frameworks. It is a free, open-source PHP web framework, created by Taylor Otwell back in 2011 and intended for the development of web applications following the MVC model.

Prerequisites

Before starting with this guide, it’s better you need to have domains or subdomain name pointing to your public server IP. on the following tutorial we will use lab.axfon.com. If you do not have public IP or domain you may also follow this tutorial using local server.


Step 1: Install Apache2 as Web Server

To install Apache2 stable version by default repository Ubuntu, use the following single command to update and install Apache2

sudo apt update && sudo apt install apache2

Once Apache2 already installed, to check and verify the Apache service status type the command below

sudo service apache2 status

If it is running and working properly you will see the output Apache2 active (running) like below

● apache2.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2019-01-02 17:58:26 UTC; 1min 54s ago
 Main PID: 14259 (apache2)
    Tasks: 55 (limit: 665)
   Memory: 6.1M
   CGroup: /system.slice/apache2.service
           ├─14259 /usr/sbin/apache2 -k start
           ├─14261 /usr/sbin/apache2 -k start
           └─14262 /usr/sbin/apache2 -k start
Jan 02 17:58:26 axfon-lab systemd[1]: Starting The Apache HTTP Server...
Jan 02 17:58:26 axfon-lab systemd[1]: Started The Apache HTTP Server.

To exit Apache2 service status just press q on the keyboard


Step 2: Configuration the Firewall with UFW

UFW (Uncomplicated Firewall) is an Iptable interface to easily configure a firewall on your system. If it does not yet enable, it’s recommended to enable and setup the rule for Apache

For the first, we have to add rule for SSH, let’s se the command line below

sudo ufw allow OpenSSH

Add the rule receive HTTP to Apache2

sudo ufw allow 'Apache Full'

Then you will see on terminal

Rule added
Rule added (v6)'

Now enable ufw for Firewall.

sudo ufw enable

If prompted just pres Y to accept and continue and to check UFW status type the command below

sudo ufw status

You will see the output UFW active (running) like below

Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
Apache Full                ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
Apache Full (v6)           ALLOW       Anywhere (v6)

Apaceh2 web server now is ready on Ubuntu, now you may go to your web browser and visit your domain or IP. If you have not configured domain name yet and don’t know your IP, use the following command find out

sudo ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'

If everything is OK you will be presented on your browser with the default Apache2 landing page as below


Step 3: Install PHP 7.3 FPM and Related Modules

When this tutorial made the PHP 7.3 is not yet available in Ubuntu default repositories, for installing it we have to add manually ondrej/php PPA by running the following command

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php

After adding the PPA we need to update Ubuntu, run the following command

sudo apt-get update

Once the PPA repository has been added and updated, now we are going to install PHP 7.3

 sudo apt-get install php7.3 libapache2-mod-php7.3 php7.3-cli php7.3-mysql php7.3-gd php7.3-imagick php7.3-recode php7.3-tidy php7.3-xmlrpc php7.3-common php7.3-curl php7.3-mbstring php7.3-xml php7.3-bcmath php7.3-bz2 php7.3-intl php7.3-json php7.3-readline php7.3-zip

To verified PHP 7.3 was installed correctly on the web server, you may check the PHP version

sudo php -v

You will be presented on the terminal screen as below

PHP 7.3.0-2+ubuntu18.10.1+deb.sury.org+1 (cli) (built: Dec 17 2018 09:23:19) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.0-dev, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.0-2+ubuntu18.10.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

Step 4: Configure PHP file (Optional)

The php.ini file is a default configuration file that read on PHP starts up. If you want to change the PHP settings on Ubuntu, Follow the below steps to modify the PHP configuration file php.ini using the command line on Ubuntu.

sudo nano /etc/php/7.3/apache2/php.ini

To edit the configuration file your may find or you can use Cmd/Ctrl W to search and replace it as the following

error_reporting = E_COMPILE_ERROR | E_RECOVERABLE_ERROR | E_ERROR | E_CORE_ERROR
max_input_time = 30
error_log = /var/log/php/error.log
file_uploads = On
allow_url_fopen = On
memory_limit = 256M
upload_max_filesize = 100M
max_execution_time = 360
date.timezone = America/Los_Angeles

Press Ctrl/Cmd + X and then press Y and ENTER to save changes


Step 5: Create Directory and Grant Permission

Set permission ownership for the current user to be able to modify any files in these directories. The $(whoami) variable will take the value of the user you are currently logged in.

sudo chown -R $(whoami):$(whoami) /var/www/html/

Grant Directory Permission

sudo chmod -R 755 /var/www

Step 6: Install Composer

To install composer globally, download the Composer installer using curl and directly move into directory /usr/local/bin

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

Now your composer installed successfully to /usr/local/bin/composer

All settings correct for using Composer
Downloading...
Composer (version 1.8.0) successfully installed to: /usr/local/bin/composer                  
Use it: php /usr/local/bin/composer

Step 7: Install Laravel

Install Laravel into the default directory /var/www/html Run the Composer create-project command to install Laravel in the directory lab-axfon (Change with your own prefer name)

cd /var/www/html
composer create-project --prefer-dist laravel/laravel lab-axfon

After running the commands above, a new project directory will be created… Run the commands below to set the correct permissions for that directory

sudo chown -R www-data:www-data /var/www/html/lab-axfon/
sudo chmod -R 755 /var/www/html/lab-axfon/

Step 8: Configuration Virtual Hosts on Apache2

If you wish to host multiple or one sites/domain, the way should to do is setting up virtual hosts. There are several ways to set up virtual hosts on Apache. The recommended method is the following steps outline will make things a lot easier for you.

The Virtual Host files are located in /etc/apache2/sites-available/  to allow web server respond to various domain requests.

To create a configuration file, copy default file to new files for our domain lab.axfon.com (replace it with your domain name)

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/lab.axfon.com.conf

Open these files to edit and configure using nano editor with the following command

sudo nano /etc/apache2/sites-available/lab.axfon.com.conf

Enter the document root path (change with your own) and log directories as shown below, and add Directory  block before <VirtualHost>:

<Directory /var/www/>
        Require all granted
        Options Indexes FollowSymLinks
        AllowOverride All
</Directory>
<VirtualHost *:80>
        ServerAdmin info@lab.axfon.com
        ServerName lab.axfon.com
        ServerAlias www.lab.axfon.com
        DocumentRoot /var/www/lab.axfon.com/public

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Press Ctrl/Cmd + X and then press Y and ENTER to save changes and close nano


Step 9: Enable and Restart Apache2

After configuring the Virtual Host, Enable and rewrite modules using the following commands

sudo a2ensite lab.axfon.com.conf
sudo a2enmod rewrite

Changes made in the Apche2 configuration file will not be applied until the command to Restart and Before testing on the Browser we have to verify and make sure no error.
To restart Apache2 use the following the command line

sudo service apache2 restart

Step 10: Testing Laravel on the Browser

You can now view this page in your web browser by visiting your server’s domain name or public IP address http://your_domain_or_IP/


If this tutorial could help you, please rate above Star button rating and share to help others find it! Feel free to leave a comment below.

Recommended For You

Axfon

About the Author: Axfon

Team committed to share our IT skills and experience through our website which may assist to formulate a task easy