Here we show how to install the LAMP stack through the command line.
- Install Apache
To install Apache you must install the Metapackage apache2:
Code: Select all
sudo apt-get install apache2
- Install MySQL
To install MySQL you must install the Metapackage mysql-server:
You will however be prompted to enter a password for MYSQL root user. If you want to use a system-wide root password, leave the place for password blank, press ok and then continue.Code: Select all
sudo apt-get install mysql-server
Connect to MYSQL, for example on Ubuntu Linux 16.04 by executing the following command:
Code: Select all
$ mysql -u root -p
Enter Password: - Install PHP
To install PHP you must install the Metapackages php5.6/7.0/7.1 and libapache2-mod-php5.6/7.0/7.1. But before installing, add the PPA to be able to upgrade for future versions,
then install php and libapache2-mod-php, specifying the versions you want to install:Code: Select all
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
You will see a message like this one:Code: Select all
sudo apt-get install php5.6 libapache2-mod-php5.6
You can install the rest of the php and libapache2-mod-php versions byCode: Select all
The following NEW packages will be installed:
libapache2-mod-php5.6 libssl1.1 php-common php5.6 php5.6-cli php5.6-common php5.6-json php5.6-opcache php5.6-readline
Code: Select all
sudo apt-get install phpx libapache2-mod-phpx
where x is the php version or libapache2-mod-php version.
Verify the php version you installed:
Or list all php packages installed:Code: Select all
sudo php -v
You can remove any php package you do not want by typingCode: Select all
dpkg -l | grep php| awk '{print $2}' |tr "\n" " "
You can remove all the php dependencies in one shot:Code: Select all
sudo aptitude purge package's_name
Code: Select all
sudo apt-get purge `dpkg -l | grep php| awk '{print $2}' |tr "\n" " "`
- Restart Server
Server is expected to restart Apache automatically after the installation of both MySQL and PHP. If it doesn't, execute the following command:
You will see a message like this:Code: Select all
sudo /etc/init.d/apache2 restart
Code: Select all
[ ok ] Restarting apache2 (via systemctl): apache2.service.
- Check Apache
Open a web browser and type http://localhost/. You should see a message saying It works! on a screen that looks like the image below:
- Check PHP
Check your PHP installation by executing any PHP file from within /var/www/.
Let's create a script called info.php. In order for Apache to find the file and serve it correctly, it must be saved to a specific directory, which is called the "web root".
In Ubuntu 16.04, this directory is located at /var/www/html/. Let's create the file at that location by typing:
This will open a blank file, info.php. Let's put inside the file the following text (which is valid PHP code ):Code: Select all
sudo nano /var/www/html/info.php
To save the changes you've made, press Ctrl + O. To exit nano, type Ctrl + X. If you ask nano to exit from a modified file, it will ask you if you want to save it. Just press N in case you don't, or Y in case you do. It will then ask you for a filename. In our case, we have already created the filename "info.php, Just press Enter.Code: Select all
<?php
phpinfo();
?>
Now, let's test whether our web server can correctly display content generated by a PHP script by opening the browser and typing:
The page that opens should look something like this:Code: Select all
http://localhost/info.php
This page basically provides information about your server from the perspective of PHP version installed. It is useful for debugging and to ensure that settings are being applied correctly.
If this was successful, then your PHP is working as expected.
You can remove any file located at /var/www/html/ by typing
To alternatively check if PHP installation is working properly, execute the following command:Code: Select all
sudo rm /var/www/html/filename
You should see this message:Code: Select all
php -r 'echo "\n\nYour PHP installation is working fine.\n\n\n";'
Code: Select all
Your PHP installation is working fine.
If you installed more than one PHP version, you may from time to time switch between these different versions.
To switch between installed versions, type
- sudo update-alternatives --config php
- #Then set Apache to work with right version by doing the following:
- sudo a2dismod php5.6 # unload the current version
- sudo a2enmod php7.1 # load the version you need
- sudo service apache2 restart # restart webserver to apply changes
Updates:
In Ubuntu 18.04, it was enough to switch between php versions by just typing:
sudo update-alternatives --config php
If you have more than one php versions installed, you will see a message like this:
- There are 2 choices for the alternative php (providing /usr/bin/php).
- Selection Path Priority Status
- ------------------------------------------------------------
- 0 /usr/bin/php7.2 72 auto mode
- * 1 /usr/bin/php5.6 56 manual mode
- 2 /usr/bin/php7.2 72 manual mode
- Press <enter> to keep the current choice[*], or type selection number:
- update-alternatives: using /usr/bin/php7.2 to provide /usr/bin/php (php) in auto mode
You can then type a selection number to load/keep the php version you want.
References:
How to install LAMP Server on Ubuntu
How to install LAMP on Ubuntu 16.04
Installing PHP 5.6