Home PHP Install LAMP

Install LAMP [7 Easy Steps To Prepare Ubuntu For PHP Development]

23
0
Install LAMP in Ubuntu
Install LAMP in Ubuntu

In this tutorial, We see how to install LAMP and to prepare Ubuntu for PHP development.

First, what is LAMP? LAMP is a common and popular web service stack. You can use LAMP for building dynamic websites and web applications.

LAMP is an acronym, and it stands for:

  • L stands for Linux operating system.
  • A stands for Apache HTTP Server.
  • M stands for MySQL relational database or its fork MariaDB.
  • P stands for PHP PHP programming language.

Let’s see how to install each component in the LAMP stack on Ubuntu and integrate Apache, MySQL, and PHP.

Step 1 [Install LAMP] – Install apache2

Install the apache2 package by invoking apt – The Ubuntu package manager.

user@demo:~# sudo apt install -y apache2
...

Step 2 – Install mariadb

Let’s install the mariadb package with the help of apt

user@demo:~# sudo apt install -y mariadb-server
...

Step 3 – Prepare Ubuntu For PHP Development with PHP and mod-php

Now, We install PHP and mod-php apache2 module. The mod-php apache2 module allows the apache server to execute and dynamically serve PHP scripts.

user@demo:~# sudo apt install -y php libapache2-mod-php
...

Step 4 – Prepare Ubuntu For PHP Development With Common PHP Extensions

he default php package is a basic package and does not include all the extensions of the PHP language. Now, It is time to install common extensions. We install the following PHP extensions:

  • php-mysql allows PHP scripts to connect to MySQL server and fetch data from the database or store data in the database.
  • php-dom enables PHP scripts to read or write XML documents through the DOM API.
  • php-zip allows PHP scripts to access ZIP compressed archives and read or write files inside them.
user@demo:~# sudo apt install -y php-mysql php-dom php-zip
...

You can also install your favorite extension in this step. You can find here a list of PHP extensions here. To find the name of ubuntu package of the PHP extension, try to add the prefix php- . That is, suppose you want to install the PHP extension xsl , Try to install the package php-xsl.

Step 5 – Prepare Ubuntu For PHP Development, Install Composer

Composer is the defacto dependency management tool for PHP and has gain popularity with PHP developers. Let’s install it.

user@demo:~# sudo apt install -y composer

Step 6 – Install phpMyAdmin

While you can use mysql command-line client or MySQL workbench visual tool for database administration, I prefer to use the phpMyAdmin web application for this task. Let’s install it.

user@demo:~# sudo apt install -y phpmyadmin

Follow the install wizard:

| Please choose the web server that should be automatically configured to   │
│ run phpMyAdmin.                                                           │
│                                                                           │
│ Web server to reconfigure automatically:                                  │
│                                                                           │
│    [*] apache2                                                            │
│    [ ] lighttpd                                                           │
│                                                                           │
Install lamp in Ubuntu - step 1 of installing phpmyadmin [select apache]
Install lamp in Ubuntu – step 1 of installing phpmyadmin

Select apache2 and click OK


| The phpmyadmin package must have a database installed and configured     
│
│ before it can be used. This can be optionally handled with                │
│ dbconfig-common.                                                          │
│                                                                           │
│ If you are an advanced database administrator and know that you want to   │
│ perform this configuration manually, or if your database has already      │
│ been installed and configured, you should refuse this option. Details on  │
│ what needs to be done should most likely be provided in                   │
│ /usr/share/doc/phpmyadmin.                                                │
│                                                                           │
│ Otherwise, you should probably choose this option.                        │
│                                                                           │
│ Configure database for phpmyadmin with dbconfig-common?                   |
Install lamp in Ubuntu - step 2 of installing  phpmyadmin [create phpadmin database]
Install lamp in Ubuntu – step 2 of installing phpmyadmin

Click Yes.


Now provide the the password and verify

│ Please provide a password for phpmyadmin to register with the database    │
│ server. If left blank, a random password will be generated.               │
│                                                                           │
│ MySQL application password for phpmyadmin:                                |
Install lamp in Ubuntu - step 3 of installing phpmyadmin [set password]
Install lamp in Ubuntu – step 3 of installing phpmyadmin
| Password confirmation:                                                    |
Install lamp in Ubuntu - step 4 of installing phpmyadmin [password confirmation]
Install lamp in Ubuntu – step 4 of installing phpmyadmin [password confirmation]

We want to phpmyadmin to serve at port 8080. To do so, Edit /etc/phpmyadmin/apache.conf:

  • Comment the exising directives at the begining of the file (alias and directory)
  • Add the virtual host and Listen directives
# phpMyAdmin default Apache configuration

## Alias /phpmyadmin /usr/share/phpmyadmin

## <Directory /usr/share/phpmyadmin>
##    Options SymLinksIfOwnerMatch
##    DirectoryIndex index.php

##    # limit libapache2-mod-php to files and directories necessary by pma
##    <IfModule mod_php7.c>
##        php_admin_value upload_tmp_dir /var/lib/phpmyadmin/tmp
##        php_admin_value open_basedir /usr/share/phpmyadmin/:/etc/phpmyadmin:/var/lib/phpmyadmin/:/usr/share/php/php-gettext/:/usr/share/php/php-php-gettext/:/usr/share/javascript/:/usr/share/php/tcpdf/:/usr/s>
##    </IfModule>

## </Directory>

# Disallow web access to directories that don't need it
<Directory /usr/share/phpmyadmin/templates>
    Require all denied
</Directory>
<Directory /usr/share/phpmyadmin/libraries>
    Require all denied
</Directory>

Listen 8080
<VirtualHost *:8080>
    TransferLog  "/var/log/apache2/phpmyadmin_access.log"
    ErrorLog     "/var/log/apache2/phpmyadmin_errors.log"
    DocumentRoot "/usr/share/phpmyadmin"
</VirtualHost>

Now restart apache2

user@demo:~# sudo service apache2 restart

Now you can connect to phpmyadmin at port 8080.

step 7 – Check If Lamp Is Working

Now , we can start develop our site in PHP.


Your document root is under /var/www/html.

Let’s check whether we successfully configure apache. Open /var/www/html/main.php in favorite editor and enter the following content:

/var/www/html/main.php
<?php phpinfo()

Now, access http://demo/main.php in your webbrowser. You should see the familiar page:

Install lamp in Ubuntu - Simple PHP script to display PHP version
Install lamp in Ubuntu – PHP version

Start To Develop In PHP

We see how to prepare Ubuntu for PHP development. With the help of apt package manager, we easily installed apache2, MySQL, PHP and common PHP extension such as php-mysql. We also install tools that make our PHP development easier such as composer and phpmyadmin.

Now you have all the tools to start to develop in PHP. Happy coding!!

LEAVE A REPLY

Please enter your comment!
Please enter your name here