Skip to main content

Ubuntu 18 - Create LAMP with different PHP versions

Environment description

After preparing this enviroment step by step you will have:

  • PHP 5.6, 7.0, 7.1, 7.2, 7.3,
  • easy switcher for PHP versions,
  • Apache2 working as you - so no more permissions problems,
  • easy vhost create/remove,
  • MySQL with custom account,
  • GIT, Composer, Curl,
  • New ssh key.

This configuration is enough for most of backend/frontend developers working on Ubuntu and you don't have to use Docker etc. Just tweak configuration of existing version and you will be good to go.

GIT / Composer

I assume you have fresh Ubuntu 18! First of all, you need to update all packages:

sudo apt-get update

Install and configure GIT:

sudo apt-get install git
git config --global user.name "Your name"
git config --global user.email "your email@address"

CURL and Composer:

sudo apt-get install curl
sudo apt-get install composer

RSA key

If you don`t have your ssh key, you might need new one:

ssh-keygen

Apache, MySQL, PHP

To install different PHP versions, you will need ondrej/php package:

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

Let's start with Apache, MySQL and PHP:

sudo apt install apache2
sudo apt-get install mysql-server

Login to MySQL:

sudo mysql

Create any users with this query:

CREATE USER 'user'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO 'user'@'localhost';
FLUSH PRIVILEGES;

If you have problems with accessing mysql, you can also use:

ALTER USER 'user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Now you have MySQL configured:

User: user
Password: password

Exit from MySQL cli:

exit;

Install PHP 5.6, 7.0 and 7.1:

sudo apt install php5.6
sudo apt install php7.0
sudo apt install php7.1
sudo apt install php7.2
sudo apt install php7.3

Common PHP extensions

Most popular extensions for PHP:

sudo apt install php5.6-cli php5.6-xml php5.6-mysql php5.6-curl php5.6-gd php5.6-intl php5.6-json php5.6-mbstring php5.6-mcrypt php5.6-soap php5.6-zip
sudo apt install php7.0-cli php7.0-xml php7.0-mysql php7.0-curl php7.0-gd php7.0-intl php7.0-json php7.0-mbstring php7.0-mcrypt php7.0-soap php7.0-zip
sudo apt install php7.1-cli php7.1-xml php7.1-mysql php7.1-curl php7.1-gd php7.1-intl php7.1-json php7.1-mbstring php7.1-mcrypt php7.1-soap php7.1-zip
sudo apt install php7.2-cli php7.2-xml php7.2-mysql php7.2-curl php7.2-gd php7.2-intl php7.2-json php7.2-mbstring php7.2-soap php7.2-zip
sudo apt install php7.3-cli php7.3-xml php7.3-mysql php7.3-curl php7.3-gd php7.3-intl php7.3-json php7.3-mbstring php7.3-soap php7.3-zip

Apache permissions

To avoid Apache permissions problem, when Apache generate files that you cannot edit, or you create files, that Apache cannot handle - make Apache to run as your user.
You can skip this step if you know how to deal with permissions.
Find envvars and edit it. Use gedit for Gnome or kate for KDE:

sudo gedit /etc/apache2/envvars
kate /etc/apache2/envvars

By default, there is www-data user set for Apache, found this lines:

export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data

 Put there your username (type whoami to check it):

export APACHE_RUN_USER=yourusername
export APACHE_RUN_GROUP=yourusername

Creating virtual hosts

Now really usefull script, that allow you to easilly create virtual hosts:

cd /usr/local/bin
sudo wget -O virtualhost https://raw.githubusercontent.com/RoverWire/virtualhost/master/virtualhost.sh
sudo chmod +x virtualhost

With this script you can create virtual hosts by:

sudo virtualhost create somedomain.test /path/to/www

Apache and etc/hosts will be configured automatically.

Switching PHP version

Switch for example to PHP 5.6 cli and apache:

sudo update-alternatives --set php /usr/bin/php5.6
sudo a2dismod php7.0
sudo a2enmod php7.1
sudo systemctl restart apache2

If you want to have easy PHP switcher, download this sh script:

cd /usr/local/bin
sudo wget -O phpswitch https://scriptun.com/sh/phpswitch --no-check-certificate
sudo chmod +x phpswitch

I had to add --no-check-certificate option because Ubuntu cannot verify Let's Encrypt certificates yet.

You can check source of phpswitch, it's very simple. Now you can use this commands from any directory, to switch between PHP versions (cli and apache):

phpswitch 5.6
phpswitch 7.0
phpswitch 7.1
phpswitch 7.2
phpswitch 7.3

 Fast and simple, only one command to switch cli and apache PHP version.

Database manager

Im using DBeaver CE on Ubuntu for database management. It's free and works really fine, so if you wan't to install it (better way instead of using phpmyadmin):

sudo add-apt-repository ppa:serge-rider/dbeaver-ce
sudo apt-get update
sudo apt-get install dbeaver-ce

 

Add new comment

CAPTCHA

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

We use cookies on our website to enhance your user experience. We also use Google analytics and ads.

Click here to read more I've read it