How to Create a LAMP Web Server Using Ubuntu

Quickly and easily set up a web server on Ubuntu

Ubuntu computer screen

Uka0310 / Creative Commons 

This guide will show you the easiest way to install a LAMP web server using a desktop version of Ubuntu.

LAMP stands for Linux, Apache, MySQL, and PHP.

The version of Linux used within this guide is of course Ubuntu.

This has been tested through Ubuntu 20.04 LTS.

Apache is one of many types of web servers available for Linux. Others include Lighttpd and NGinx. 

MySQL is a database server that will help you make your web pages interactive by being able to store and display stored information.

Finally, PHP (which stands for Hypertext Preprocessor) is a scripting language that can be used to create server-side code and Web APIs which can then be consumed by client-side languages such as HTML, JavaScript, and CSS.

We'll be showing you how to do install LAMP using the desktop version of Ubuntu so that budding web developers can set up a development or test environment for their creations.

The Ubuntu web server can also be used as an intranet for home web pages.

Whilst you could make the web server available for the whole world this is impractical using a home computer as broadband providers generally change the IP address for computers and so you would need to use a service such as DynDNS to get a static IP address. The bandwidth provided by your broadband provider would probably also not be suitable for serving web pages.

Setting up the web server for the whole world would also mean that you are responsible for securing the Apache server, setting up firewalls and make sure all the software is patched correctly.

If you want to create a website for the whole world to view then you would be advised to choose a web host with CPanel hosting which takes away all of that effort.

How to Install a LAMP Web Server Using Tasksel

Installing the whole LAMP stack is actually very straightforward and can be achieved using just 2 commands.

Other tutorials online show you how to install each component separately but you can actually install all of them at once.

  1. To do so you will need to open a terminal window. To do this press Ctrl + Alt + T at the same time.

  2. In the terminal window, type the following commands:

    sudo apt-get install tasksel
    sudo tasksel install lamp-server
    Ubuntu installing LAMP via tasksel

    The above commands install a tool called tasksel, and then, using tasksel, they install a meta-package called lamp-server.

So what is tasksel?

Tasksel lets you install a group of packages all at once. As described earlier, LAMP stands for Linux, Apache, MySQL, and PHP, and it is common that if you install one then you tend to install them all.

Tasksel running on Ubuntu

You can run the tasksel command on its own as follows:

sudo tasksel

This will bring up a window with a list of packages or, should we say, groups of packages that can be installed.

For instance, you can install the KDE desktop, the Lubuntu desktop, a mail server, or an openSSH server.

When you install software using tasksel, you aren't installing one package but a group of like-minded packages that all fit together to make one big thing. In our case, the one big thing is a LAMP server.

Set the MySQL Password

After running the commands in the previous step, the packages required for Apache, MySQL, and PHP will be downloaded and installed.

  1. Now, you're going to need to secure your new MySQL database and add an admin password, so you can manage it. Open your terminal back up, and run the following command to start MySQL's built-in security script.

    sudo mysql_secure_installation
  2. First, the script will ask you to set up password validation. Press N or the security plugin will interfere with tools like PHPMyAdmin later on.

    MySQL secure installation on Ubuntu
  3. Next, you'll be asked for an admin(root) password. This password is not the same as your login password, and you can set it to anything you wish. It is worth making the password as secure as possible as the owner of the password can administer the whole database server with the ability to create and remove users, permissions, schemas, tables and well pretty much everything.

  4. After you have entered the password, you'll be asked a few more questions to secure the server. Answer Y to all of them.

  5. Eventually, you will return to the command prompt, and you can test the server to see if it worked.

    MySQL Secure installation complete

How to Test Apache

The easiest way to test whether Apache is working is as follows:

  • Open up Firefox by clicking the icon on the launcher (3rd from top).
  • In the address bar enter http://localhost.
Default Apache page on Ubuntu

A web page should appear as shown in the image.

Basically, if you see the words "It Works" on the web page as well as the Ubuntu logo and the word Apache then you know that the installation was successful.

The page you are seeing is a placeholder page, and you can replace it with a web page of your own design.

To add your own web pages you need to store them in the folder /var/www/html.

The page you are seeing now is called index.html.

To edit this page you will need permissions to the /var/www/html folder. There are various ways to provide permissions. This is the preferred method.

Open a terminal window and enter these commands:

sudo gpasswd -a usnername www-data
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R g+rwx /var/www/html

Replace "username" in the first command with your actual username. You will need to log out and back in again for the permissions to take effect. If that doesn't work, restart the computer.

How to Check If PHP Is Installed

The next step is to check that PHP is installed correctly. 

  1. To do this open a terminal window and enter the following command:

    sudo nano /var/www/html/phpinfo.php

    If you'd prefer, Vim, Emacs, or even Gedit, you can use that in place of Nano.

  2. Within the nano editor, enter the following text:

    <?php phpinfo(); ?>
  3. Save the file by pressing Ctrl + O, and exit the editor by pressing Ctrl + X.

  4. Next, change the permissions and ownership of the file to let the server access and run it.

    sudo chown www-data:www-data /var/www/html/phpinfo.php
    sudo chmod +x /var/www/html/phpinfo.php

  5. Open the Firefox web browser, and enter the following into the address bar:

    http://localhost/phpinfo.php
    PHPinfo page on Ubuntu

    If PHP has installed correctly, you will see a page similar to the one in the image above.

    The PHPInfo page has all sorts of information, including listing the PHP modules that are installed and the version of Apache that is running.

    It is worth keeping this page available whilst developing pages so that you can see if modules you require in your projects are installed or not.

Introducing MySQL Workbench

MySQL on its own is difficult to administer from the command line so we recommend installing 2 more tools:

  • MySQL Workbench
  • PHPMyAdmin
  1. Testing MySQL can be achieved using the following simple command in a terminal window:

    sudo mysqladmin -u root -p status

    When you are prompted for a password you will need to enter the root password for the MySQL root user and not your Ubuntu password.

    MySQL status on Ubuntu
  2. If MySQL is running you will see the following text:

    Uptime: 6269 Threads: 3 Questions: 33 Slow queries: 0 Opens: 112 Flush tables: 1 Open tables: 31 Queries per second avg: 0.005
  3. To install MySQL Workbench open a terminal, and run the following command:

    sudo apt install mysql-workbench

    In case Ubuntu still hasn't packaged MySQL Workbench for the latest release, you can always check the official download page. In some instances, installing the package for the previous Ubuntu release will also work.

    Once you have the package, install it with apt, replacing the package name and numbering with your actual package name:

    cd ~/Downloads
    sudo apt install ./mysql-workbench-community_X.X.XX-ubuntuXX.XX_amd64.deb
  4. Before you launch MySQL Workbench, you're going to need a user to manage it. For security reasons, you can't log in as root, so you'll need another user. The best way to handle this is to create a new user for each database or set of databases that you want to manage. So, if you were going to set up a WordPress site, you'd do something like this:

    sudo mysql -u root -p
    CREATE DATABASE wordpress;
    CREATE USER `wpadmin`@`localhost` IDENTIFIED BY 'userpassword';
    GRANT ALL ON wordpress.* TO `wpadmin`@`localhost`;
    FLUSH PRIVILEGES;
    \q

    This will create a new database for WordPress and a MySQL user named "wpadmin" with the password "userpassword" to manage it.

  5. When the software has completed installing, press the Applications icon on your launcher, and type "MySQL" into the search box.

  6. An icon with a dolphin is used to denote MySQL Workbench. Select this dolphin icon when it appears.

    Search for MySQL Workbench on Ubuntu
  7. When MySQL Workbench first opens, you'll see a generic connection box at the bottom of the window. Select it, and press the wrench(edit) icon just above, next to MySQL Connections.

    MySQL Workbench on Ubuntu
  8. A new window will pop up allowing you to edit the connection information. Replace root as the Username with whichever username you chose.

    Change username on MySQL Workbench
  9. If you don't want to enter your password every time you connect, press Store in Keychain next to Password, and enter the password for your database user.

  10. When you're done, Close the window.

  11. Now, select your connection again to connect to the database.

    Connected to MySQL Workbench on Ubuntu

The MySQL workbench tool is fairly powerful albeit a little bit on the slow side.

A bar down the left lets you choose which aspect of your MySQL server you wish to manage such as:

  • Providing a server status
  • List client connections
  • Administer users and privileges
  • Manage system variables
  • Export data
  • Import data

The server status option tells you whether the server is running, how long it has been running, the server load, the number of connections, and various other bits of information.

The client connections option lists the current connections to the MySQL server.

Within users and privileges, you can add new users, change passwords and choose the privileges the users have against different database schemas.

In the bottom left corner of the MySQL Workbench tool is a list of database schemas. You can add your own by right-clicking and choosing Create Schema.

You can expand any schema by clicking on it to view a list of objects such as tables, views, stored procedures, and functions.

Right-clicking on one of the objects will allow you to create a new object such as a new table.

The right panel of MySQL Workbench is where you do the actual work. For instance, when creating a table you can add columns along with their data types. You can also add procedures that provide the basic template for a new stored procedure within an editor for you to add the actual code. 

How to Install PHPMyAdmin

A common tool used for administering MySQL databases is PHPMyAdmin, and by installing this tool, you can confirm once and for all that Apache, PHP and MySQL are working correctly.

  1. Open a terminal window and enter the following command:

    sudo apt install phpmyadmin
    Install PHPMyAdmin on Ubuntu
  2. A window will appear asking which web server you have installed.

    Configure PHPMyAdmin for webserver on Ubuntu

    The default option is already set to Apache so use the tab key to highlight the OK button and press return.

  3. Another window will pop up asking whether you want to create a default database to be used with PHPMyAdmin.

    PHPMyAdmin configure database

    Press the tab key to select the Yes option and press return.

  4. Then, PHPMyAdmin will ask for a username to manage the database. The default is "phpmyadmin." You can leave that or change it to something more memorable for you.

  5. Finally, you will be asked to provide a password for the PHPMyAdmin database. Enter something secure to use whenever you log in to PHPMyAdmin.

    Set PHPMyAdmin password

    The software will now be installed and you will be returned to the command prompt.

  6. You may or may not need this next part. In testing on Ubuntu 20.04 LTS, it wasn't necessary. Try using PHPMyAdmin first, and if it doesn't work right away, double back to this step.

    Run the following set of commands to configure Apache for PHPMyAdmin.

    sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf-available/phpmyadmin.conf
    sudo a2enconf phpmyadmin.conf
    sudo systemctl reload apache2.service

    The above commands create a symbolic link for the apache.conf file from the /etc/phpmyadmin folder into the /etc/apache2/conf-available folder.

    The second line enables the phpmyadmin configuration file within Apache and finally, the last line restarts the Apache web service.

  7. What this all means is you should now be able to use PHPMyAdmin to manage databases as follows:

    • Open Firefox
    • Enter http://localhost/phpmyadmin into the address bar
    • Enter the PHPMyAdmin password into the password field and press the Go button.
    Sign in to PHPMyAdmin on Ubuntu
  8. You'll arrive on the PHPMyAdmin management interface, and you can manage your database from there.

    PHPMyAdmin interface on Ubuntu

PHPMyAdmin is a web-based tool for managing MySQL databases.

The left panel provides a list of database schemas. Clicking on a schema expands the schema to show a list of database objects.

The top icon bar lets you manage various aspects of MySQL such as:

  • Databases
  • A SQL Editor
  • Server status
  • User accounts
  • Export data
  • Import data
  • Settings
  • Replication
  • Variables
  • Character sets
  • Engines 
  • Plugins

Further Reading

Screenshot of W3Schools.com

Now that you have a database server up and running you can start to use it for developing full-fledged web applications.

A good starting point for learning HTML, CSS, ASP, JavaScript, and PHP is W3Schools.

This website has full, yet easy to follow tutorials on client-side and server-side web development. 

Whilst you won't learn in-depth knowledge you will grasp enough of the basics and concepts to get you on your way.