HOW TO SET UP UBUNTU FOR PHP/LARAVEL PROJECT

Coming from a python background where one install solves a lot of your problems when it comes to setting up your environment for development or production. Am a python developer but recently a client approached me for a project, and after evaluating the project, I concluded that the project is best executed with php and Laravel because of cost, and deployment cost on the client,now am not that familiar with PHP and Larave but I have a friend who is a backend PHP and Laravel developer, so I brought the job to him and decided to collaborate with him on the job remotely. So this means I have to set up working environment for the project (PHP and Laravel), being an active user of LINUX(Ubuntu) I had to figure out how to set up the environment myself because my friend uses windows, I hope this article saves you some stress and googling effort I went through myself.

CLEANING UP SYSTEM TO AVOID CONFLICT

First you need to make sure your system is clean, by clean I mean, deleting any previously installed MySql server and apache2 on your system.

Use apt to uninstall and remove all MySQL packages:

$ sudo apt-get remove --purge mysql-server mysql-client mysql-common -y
$ sudo apt-get autoremove -y
$ sudo apt-get autoclean

Remove the MySQL folder:

$ rm -rf /etc/mysql

Delete all MySQL files on your server:

$ sudo find / -iname 'mysql*' -exec rm -rf {} \;

Your system should no longer contain default MySQL related files. Feel free to move to the next step if you don’t have these installed on your system.

Next step is to delete apache2 from our system. This will also solve the problem of having to kill a service running on a port that xampp will be using on starting the server.

apache2​ is a metapackage that just selects other packages. If you installed apache by installing that package, you just need to run this to clean up the automatically selected packages:

$sudo apt autoremove

If that doesn't work, you might have installed one of the dependents manually. You can target all the apache2- packages from space and nuke the lot:

$sudo apt remove apache2.*

INSTALLATION

1. XAMPP

Download the installation package:

The first step is to download the XAMPP package for Linux from the official Apache Friends website:

apachefriends.org/index.html

Click on the XAMPP for Linux option after which you will be prompted to Run the package or Save it to your system. I recommend downloading the package by clicking the Save File option. After which, your downloaded file will be saved to the Downloads folder by default.

Make the installation package executable We will install the package through the Ubuntu command line, The Terminal. In order to open the terminal, either use the Dash or the ​ Ctrl+Alt+T ​ shortcut. After the Terminal is open, you need to move to your Downloads folder to access the file.

Move to the Downloads folder by using the following command:

$ cd /home/[username]/Downloads

The installation package you downloaded needs to be made executable before it can be used further.

Run the following command for this purpose:

$ chmod 755 [package name]

Example:

$ chmod 755 xampp-linux-x64-7.2.10-0-installer.run

Now the install package is in an executable form​

Launch the Setup Wizard

As a privileged root user, run the following command in order to launch the graphical setup wizard. Work through the graphical setup wizard to finish installation.

$ sudo ./[package name]

Example:

sudo ./xampp-linux-7.2.10-0-installer.run

Run the below command in your terminal to start/run xampp

$ sudo /opt/lampp/lampp start

If you get any errors, run​ $ sudo apt install net-tools

In the terminal to make sure you have net-tools installed, to stop xampp run ​$ sudo /opt/lampp/lampp stop

2. COMPOSER

Installing the Dependencies:

Before you download and install Composer, you’ll want to make sure your server has all dependencies installed.

First, update the package manager cache by running:

$sudo apt update

Then install prerequisites:

$sudo apt install curl php-cli php-mbstring git unzip

Downloading and Installing Composer:

Composer provides an ​installer​, written in PHP. We’ll download it, verify that it’s not corrupted, and then use it to install Composer.

Make sure you’re in your home directory, then retrieve the installer using curl:

$cd ~
$curl -sS https://getcomposer.org/installer -o composer-setup.php

Next, verify that the installer matches the SHA-384 hash for the latest installer found on the Composer Public Keys / Signatures​ page. Copy the hash from that page and store it as a shell variable:

$HASH=544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a
ffbc1f233e9b180f

Make sure that you substitute the latest hash for the highlighted value.

Now execute the following PHP script to verify that the installation script is safe to run:

$php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else {
echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

If you see Installer corrupt, then you’ll need to redownload the installation script again and double check that you’re using the correct hash. Then run the command to verify the installer again. Once you have a verified installer, you can continue.

To install composer globally, use the following command which will download and install Composer as a system-wide command named composer, under​ /usr/local/bin ​:

$sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

You’ll see the following output:

Output

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

To test your installation, run ​:

$composer

3. PHP Packages

Installing PHP Dependencies:

Your PHP needs some ext to run your packages, just run the following commands to install them.

For PHP 5 :

$ sudo apt-get install php5-gmp apt-get install php5-curl

For PHP 7:

$sudo apt-get install php7.0-gmp apt-get install php-curl

Install PHP MySql Connector:

$sudo apt-get install php{version}-mysql

Example:

$sudo apt-get install php7.0-mysql

SETTINGS

Laravel 4:

Change "host" in the app/config/database.php file from "localhost" to "127.0.0.1"

Laravel 5 And Above:

Change "DB_HOST” in the .env file from "localhost" to "127.0.0.1"

Grant User Access to HTDOCS Folder ​:

Open your terminal in the lampp folder and run this command

$sudo chown username:groupname /opt/lampp/htdocs

Example ​:

emetowinner@babygal:/opt/lampp​$sudo chown username:groupname /opt/lampp/htdocs

This will grant you access to the htdocs folder so you can copy and also paste files there. Now you have Xampp, PHP and Composer installed and set-up properly on your system, you can begin your Laravel/php project.