Install LAMP Stack and phpMyAdmin on Ubuntu 14.04 LTS

If you are looking for a way to develop PHP based web application and take it for a ride in your local machine before deploying it to a live web server, then this is for you.

Here I am going to guide you through the process of setting up LAMP (Linux, Apache, MySQL, PHP) stack on your Ubuntu machine without using third party tools like Xampp or Ampps. I’ll try to make this as much noob friendly as I can.

First of all, update your system.

sudo apt-get update

Now, we are going to install the web server.

  • Install Apache 2 from Ubuntu repository.
sudo apt-get install apache2
  • Go to  /etc/apache2/ and find the file apache2.conf , Edit the file and set the KeepAlive setting to Off.
KeepAlive Off
  • Go to /etc/apache2/mods-available/ and find the file mpm_prefork.conf , edit the file and save with the lines of code given below.
<IfModule mpm_prefork_module>
        StartServers            4
        MinSpareServers         20
        MaxSpareServers         40
        MaxRequestWorkers       200
        MaxConnectionsPerChild  4500
</IfModule>
  • Disable the event module and enable prefork module.
sudo a2dismod mpm_event
sudo a2enmod mpm_prefork
  • Now we need to add a file servername.conf inside etc/apache2/conf-available/ , you can just run the commands given below. More details are available here.
echo "ServerName localhost" | sudo tee /etc/apache2/conf-available/servername.conf
sudo a2enconf servername
  • Now restart Apache and we are good to go to the next stage.
sudo service apache2 restart

Configuring Virtual Hosts

We can configure as many virtual hosts we want. Let’s say we want to browse localhost/web.dev from our browser, for that we need to configure a virtual host that will point us to that location. A virtual host works just like a domain name. Here web.dev is working as a domain name. You can use any name that resembles a domain name.

  • Create a copy of the default Apache configuration file. We will name the new file web.dev.conf , just run the command given below.
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/web.dev.conf
  • Go to etc/apache2/sites-available/ and edit web.dev.conf . Uncomment the line containing ServerName, replace http://www.example.com with your desired name. In our case it is web.dev, now enter the path for DocumentRoot, ErrorLog and CustomLog. You also need to add a <Directory> block before <VirtualHost> block. After adding all these your configuration file should look like the code segment given below.
<Directory /var/www/webdev>
    Require all granted
</Directory>

<VirtualHost *:80>

ServerName web.dev
ServerAlias web.dev
ServerAdmin webmaster@localhost
DocumentRoot /var/www/webdev

ErrorLog /var/www/webdev/logs/error.log
CustomLog /var/www/webdev/logs/access.log combined

</VirtualHost>

Note: Lines starting with # are comments and are omitted from the example.

  • Create a directory for logs.
sudo mkdir -p /var/www/webdev/logs
  • Enable your virtual host.
sudo a2ensite web.dev.conf
  • Reload Apache for your virtual host to start working.
sudo service apache2 reload

With that, you have successfully configured a virtual host. You can make more if you like. Let’s get to the next phase.


Install MySQL

  • Install MySQL from Ubuntu repository. You can use a password when prompted but it’s not mandatory to use one on the local machine.
sudo apt-get install mysql-server

We can use MySQL from the terminal. There is a wildly popular alternative to that, using phpMyAdmin! We will get back to that later, let’s install PHP first.


Install PHP

We will use the latest version of PHP, that is PHP 7! For this, we need to add a PPA because PHP 7 is not yet available through official Ubuntu repositories.

  • Run these commands. This will add the PPA to your system.
sudo apt-get install -y language-pack-en-base sudo LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php
  • Update your system.
sudo apt-get update
  • Now you can install PHP and related extensions from terminal.
sudo apt-get install php7.0 php-pear libapache2-mod-php7.0 php7.0-mysql php7.0-curl php7.0-json php7.0-cgi php7.0-mbstring
  • After installation is finished, go to /etc/php/7.0/apache2/ and edit the file php.ini. Add the lines given below at the end of the file to have a better debugging experience.
max_input_time = 30
error_reporting = E_COMPILE_ERROR | E_RECOVERABLE_ERROR | E_ERROR | E_CORE_ERROR
error_log = /var/log/php/error.log
  • Create PHP log folder and give ownership to Apache.
sudo mkdir /var/log/php
sudo chown www-data /var/log/php
  • We need to enable PHP module for apache.
sudo a2enmod php7.0.conf
  • Restart Apache to start parsing PHP files.
sudo service apache2 restart

With this, you can start coding in PHP. Additionally, we can use phpMyAdmin to make things a little bit easier for us.


Install phpMyAdmin

  • Change directory to usr/share
cd /usr/share
  • Change to the root user.
sudo su
  • Download latest version of phpMyAdmin with wget. You can go to the website of phpMyAdmin to get the download link of the latest version.
wget https://files.phpmyadmin.net/phpMyAdmin/4.7.0/phpMyAdmin-4.7.0-all-languages.zip
  • Unzip the file and rename the extracted folder.
unzip phpMyAdmin-4.7.0-all-languages.zip 
mv phpMyAdmin-4.7.0-all-languages phpmyadmin
  • Change the folder permissions.
chmod -R 0755 phpmyadmin
  • Now we have to configure Apache so that it can find phpMyAdmin. Go to /etc/apache2/sites-available/ and edit the file 000-default.conf. Add the lines below after the </VirtualHost> tag.
Alias /phpmyadmin "/usr/share/phpmyadmin/"
<Directory "/usr/share/phpmyadmin/">
     Order allow,deny
     Allow from all
     Require all granted
</Directory>
  • Now restart apache and you’re good to go.
sudo service apache2 restart

Test You Development Environment

  • Go to /var/www/webdev/ and create a file there. Let’s call the file test.php.
<?php echo '<p>Hello World</p>';

    // In the variables section below, replace user and password with your own MySQL credentials as created on your server
    $servername = "localhost";
    $username = "root";
    $password = "";

    // Create MySQL connection
    $conn = mysqli_connect($servername, $username, $password);

    // Check connection - if it fails, output will include the error message
    if (!$conn) {
        die('<p>Connection failed: <p>' . mysqli_connect_error());
    }
    echo '<p>Connected successfully</p>';
?>
  • Create another file in the same directory called phpinfo.php.
<?php
    phpinfo();
?>
  • Go to localhost/webdev/test.php from your browser. If you see the message “Connected successfully”, then everything is working as it should.
  • Browse localhost/webdev/test/phpinfo.php. You should see detailed information about your PHP version.

If you have completed all the steps, you are ready to make awesome web applications with PHP! Let me know in the comments if you face any trouble while setting up everything.

References:

  1. Digital Ocean
  2. Linode
  3. Stackoverflow

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s