The LAMP Stack (Linux, Apache, MySQL, and PHP) is the most popular web hosting environment for the PHP based application. Here Linux is an operating system, Apache is the popular web server developed by Apache Foundation, MySQL is relational database management system used for storing data and PHP is the widely used programming language.
This article explain you to how to install Linux, Apache, MySQL and PHP (LAMP Stack) on Ubuntu 18.04 (Bionic Beaver) Linux Systems.
Let’s begin:
Prerequisites
You must have root or sudo privileged user access to your Ubuntu 18.04 system. Login to your Ubuntu system using GUI for Desktop and SSH for the server edition.
ssh [email protected]
Now upgrade current packages to latest version.
sudo apt update
sudo apt upgrade
Step 1 – Installing Apache2
After installing PHP on your system, let’s start the installation of Apache2 in your system. Your also required to install libapache2-mod-php module to work PHP with Apache2.
sudo apt install apache2 -y
To create additional virtual hosts visit this tutorial.
Open a web browser and access your server via IP address on port 80. You will see the default Apache page.
Step 2 – Installing PHP
Ubuntu 18.04 has default PHP 7.2 for installation. But we suggest adding additional PPA for PHP installation which includes multiple other versions of PHP. Use the following commands to update apt cache and install PHP packages on your system.
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install -y php7.2
You may also need to install some additional PHP modules for supporting various tasks
sudo apt install php7.2-curl php7.2-gd php7.2-json php7.2-mbstring php7.2-xml
Verify the installed PHP version
php -v
PHP 7.2.34-8+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Oct 31 2020 16:57:15) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.34-8+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies
Step 3 – Installing MySQL
Next is MySQL database serve for the LAMP Stack. The mysql-server packages are available under the official Ubuntu repositories. php-mysql package to required to connect MySQL using PHP.
Use the following command to install it.
sudo apt install mysql-server php7.2-mysql
The installer will prompt for the root password, This password will work for your MySQL root user. After installing MySQL execute the following command for initial settings of MySQL server.
sudo mysql_secure_installation
The configuration wizzard will ask to your setup root password, password policy and removal test account and database. Complete wizard as per your required settings.
MySQL installation has been completed on your system. Connect to the MySQL server via command line.
mysql -u root -p
Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 26 Server version: 5.7.32-0ubuntu0.18.04.1 (Ubuntu) Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
Step 4 – Installing phpMyAdmin
You can also install phpMyAdmin for the administration of MySQL using web interface.
sudo apt install phpmyadmin
When prompt, select Apache as web server. After that phpmyadmin will create a database for its own uses. Set a strong password for the phpmyadmin account as per password policy.
Once the installation finished, you can access phpMyAdmin by using server IP address like: http://server-ip/phpmyadmin/
Step 5 – Installing PHP Composer
Composer is use for installing and managing php application dependencies. To install PHP composer, download its binary file and place under the bin directory.
curl -sS https://getcomposer.org/installer | php
Now use the following commands to make composer available globally for all users in your system, which can be used for all PHP applications on that system.
sudo mv composer.phar /usr/local/bin/composer
chmod +x /usr/local/bin/composer
Step 6 – Manage Services
After installing all the services on your system. Restart MySQL and Apache service to reload any changes done manually.
sudo systemctl restart apache2
sudo systemctl restart mysql
Step 7 – Configure Firewall
The systems with iptables/ufw enabled, execute the following commands to open port 80 for public access of web server.
With FirewallD:
sudo firewall-cmd --permanent --zone=public --add-port=80/tcp
With UFW:
sudo ufw allow 80/tcp
Step 8 – Test Setup
After completing all setup. Let’s create a info.php file website document root with following content.
<?php phpinfo(); ?>
Now access this file in web browser. You will see the screen like below with all details of PHP on server.
4 Comments
If you are setting up LAMP on local server then this is a really good guide for that. But if you want to setup LAMP on a hosting server, then it is better to use managed services. There is Cloudways PHP website hosting platform where lamp/lemp server can be launched on infrastructures in just a click.
What is wrong if I see php code instead of info about all details of PHP
You need to install libapache2-mod-php packages as per your PHP version. For example:
PHP 7.2 => libapache2-mod-php7.2
PHP 7.1 => libapache2-mod-php7.1
PHP 5.6 => libapache2-mod-php5.6
It only takes a-few performs before you will be able to grab this overly busy game and you may have the same probabilities as those people who are expert participants in regards
to techniques.