How to Install Full LAMP Stack on Ubuntu Linux Desktop

Post Reply
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5214
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#1

LAMP stack is a group of open source software, namely; Linux, Apache Web server (for serving the web pages), MySQL database system ( for managing data), and PHP, used to get web servers up and running. The LAMP virtual private server is already running Ubuntu, making it a very standard stack for Linux because a lot of Linux prerequisites have been taken care of. LAMP candidates can be installed one by one by searching for and installing from the Software Centre, or by running a series of command lines.

Here we show how to install the LAMP stack through the command line.
  1. Install Apache

    To install Apache you must install the Metapackage apache2:

    Code: Select all

    sudo apt-get install apache2
  2. Install MySQL

    To install MySQL you must install the Metapackage mysql-server:

    Code: Select all

    sudo apt-get install 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.

    Connect to MYSQL, for example on Ubuntu Linux 16.04 by executing the following command:

    Code: Select all

    $ mysql -u root -p 
    Enter Password:
  3. 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,

    Code: Select all

    sudo add-apt-repository ppa:ondrej/php
    sudo apt-get update
    then install php and libapache2-mod-php, specifying the versions you want to install:

    Code: Select all

    sudo apt-get install php5.6 libapache2-mod-php5.6
    You will see a message like this one:

    Code: 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
    You can install the rest of the php and libapache2-mod-php versions by

    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:

    Code: Select all

    sudo php -v
    Or list all php packages installed:

    Code: Select all

    dpkg -l | grep php| awk '{print $2}' |tr "\n" " "
    You can remove any php package you do not want by typing

    Code: Select all

    sudo aptitude purge package's_name
    You can remove all the php dependencies in one shot:

    Code: Select all

    sudo apt-get purge `dpkg -l | grep php| awk '{print $2}' |tr "\n" " "`
  4. 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:

    Code: Select all

    sudo /etc/init.d/apache2 restart
    You will see a message like this:

    Code: Select all

    [ ok ] Restarting apache2 (via systemctl): apache2.service.
  5. 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:

    Image
  6. 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:

    Code: Select all

    sudo nano /var/www/html/info.php
    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

    <?php
    phpinfo();
    ?>
    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.

    Now, let's test whether our web server can correctly display content generated by a PHP script by opening the browser and typing:

    Code: Select all

    http://localhost/info.php              
    The page that opens should look something like this:

    Image
    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

    Code: Select all

    sudo rm /var/www/html/filename
    To alternatively check if PHP installation is working properly, execute the following command:

    Code: Select all

    php -r 'echo "\n\nYour PHP installation is working fine.\n\n\n";' 
    You should see this message:

    Code: Select all

    Your PHP installation is working fine.
Congratulations, up to this point, you have just installed a Ubuntu LAMP Server/Stack!

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
  1. sudo update-alternatives --config php
  2. #Then set Apache to work with right version by doing the following:
  3. sudo a2dismod php5.6        # unload the current version
  4. sudo a2enmod  php7.1      # load the version you need
  5. 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:

  1. There are 2 choices for the alternative php (providing /usr/bin/php).
  2.  
  3.   Selection    Path             Priority   Status
  4. ------------------------------------------------------------
  5.   0            /usr/bin/php7.2   72        auto mode
  6. * 1            /usr/bin/php5.6   56        manual mode
  7.   2            /usr/bin/php7.2   72        manual mode
  8.  
  9. Press <enter> to keep the current choice[*], or type selection number:
  10. 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
Attachments
Test_Apache.png
php5.6.png
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5214
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#2

Finally learn PHP in 15 minutes:



Full elementary course:

0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5214
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#3

If you get the error message

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

when connecting to MYSQL by sudo -u root -p and password,

installing the mysql-server:

Code: Select all

sudo apt-get install mysql-server

will in most cases solve the problem.
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5214
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#4

You can as well install phpMyAdmin to administer MySQL through GUI instead of using SQL and Terminal: how-to-install-and-secure-phpmyadmin-on-ubuntu-5755
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
Post Reply

Return to “LAMP (Linux, Apache, MySQL, PHP)”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 1 guest