Install and configure a LAMP stack

With your Linode created and booted, access it to install the Linux, Apache2, MySQL, and PHP (LAMP) stack. These are free and open source components that power many modern web applications. The individual components are designed to work together and are easy to install and use.

How long will this take? Approximately 30 minutes

Here you'll install the LAMP components, perform some lightweight configuration, add your domain to your Apache2 web server, and test access to it.

Before you begin

You'll need both the unique id and public IP address for your Linode. If you don't know them, run this API operation to list all your Linodes:

1. Installation

Follow these steps to get all of the LAMP components installed.

📘

Select the defaults

Whenever prompted to verify an install step, press y followed by Enter to confirm. You may also see the Package confirmation screen asking which services should be restarted. Just press Enter to select the defaults.

  1. Open a Terminal session and SSH into your Linode:

    ssh root@<Linode public IP address>
    
  2. Type yes and press Enter to confirm.

  3. Enter the root_password you set for this Linode.

  4. Run this command to make sure the Ubuntu Linux install is up-to-date.

    sudo apt update && sudo apt upgrade
    
  5. Run this command to install apache2 as your web server:

    sudo apt install apache2
    
  6. Next, install the MySQL web server:

    sudo apt install mysql-server
    
  7. Install PHP with additional modules for apache2 and MySQL:

    sudo apt install php libapache2-mod-php php-mysql
    
  8. You can optionally install the following commonly-used PHP modules. They offer support for cURL, JavaScript Object Notation (JSON), and the Common Gateway Interface (CGI).

    sudo apt install php-curl php-json php-cgi
    

Configure the timezone

Your Linode defaults to the universal time zone (UTC). You can change this to match the region you've set when creating your Linode.

  1. Run this command to launch the tzdata tool.

    dpkg-reconfigure tzdata
    
  2. Use the arrow keys to navigate the interface and select the timezone for the region of your Linode.

  3. Run the date command to verify the timezone.

    root@localhost:~# date
    Mon Jul 10 03:37:16 PM CDT 2023
    

Configure a custom hostname

Set a new hostname for your Linode using an easy-to-remember name.

  1. Run the hostname command to view the current name:

    root@localhost:~# hostname
    localhost
    
  2. Change this to make it easier to identify laterfor example, primary-origin for your primary Linode, standby1-origin for the first standby, and standby2-origin for the second.

    hostnamectl set-hostname primary-origin
    
  3. Run the hostname command again to very the change.

    root@localhost:~# hostname
    primary-origin
    

Configure and enable UFW

You'll want a firewall application on your Linode. Uncomplicated Firewall (UFW) is a frontend for managing firewall rules in Ubuntu. It's automatically installed, but it needs to be configured. Typically, you only need to open a small number of ports for incoming connections, and leave all others closed.

  1. Start by enabling UFW:

    sudo ufw enable
    
  2. Run these commands in succession to open applicable ports:

    sudo ufw allow ssh
    sudo ufw allow OpenSSH
    sudo ufw allow 80
    sudo ufw allow 443
    sudo ufw allow in "Apache Full"
    
  3. Run ufw status to verify firewall settings.

    Status: active
    
    To                         Action      From
    --                         ------      ----
    22/tcp                     ALLOW       Anywhere                                    
    443                        ALLOW       Anywhere                  
    80                         ALLOW       Anywhere                  
    OpenSSH                    ALLOW       Anywhere                  
    Apache Full                ALLOW       Anywhere                  
    22/tcp (v6)                ALLOW       Anywhere (v6)                        
    443 (v6)                   ALLOW       Anywhere (v6)             
    80 (v6)                    ALLOW       Anywhere (v6)             
    OpenSSH (v6)               ALLOW       Anywhere (v6)             
    Apache Full (v6)           ALLOW       Anywhere (v6)   
    

Reboot your Linode

Run this operation to reboot your Linode to apply all updates.

Test your Linode

Open up a browser and point it to the public IP address for your Linode. You should see the index page that Apache2 ships with, which means it can fulfill requests. You're ready to go onto the next phase.

2. Create a Virtual Host

You should create a virtual host for your domain, even if you're only looking to host a single website on your Linode. This also makes it easier to update your site and add m ore domains later.

  1. Open a Terminal session and SSH into your Linode:

    ssh root@<Linode public IP address>
    
  2. Type yes and press Enter to confirm.

  3. Enter the root_password you set for this Linode.

  4. Navigate to the default directory Apache2 uses to store website files:

    cd /var/www
    
  5. Create a new directory for your site, using your domain name to make it easy to recognize, and then navigate into it. (We'll be using our example domain docasssociates throughout this tutorial.)

    mkdir docassociates.com
    cd docsassociates.com
    
  6. Create a new index.html file to serve as your site's root page.

    nano index.html
    
  7. In the nano editor that launches, create a simple HTML layout for use. Later, you can overwrite this page with your site's actual index.html home page.

    <html>
      <body>
      <h1>Welcome to Documentation Associates!</h1>
      <p>This is my example website.</p>
      </body>
    </html>
    

    👍

    Make it different on your three Linodes

    This tutorial uses the high availability model for failover. To test it, you may want to make this example file different on each of your Linodes. However, to properly support high availability when you go live, you'll want to make sure that your actual website content is the same on all three.

  8. Press Ctrl + X, followed by Y then Enter to save and exit nano.

  9. Navigate to the directory where sites are managed in Apache2:

    cd /etc/apache2/sites-available
    
  10. Create a new configuration file for your domain:

    nano docasassociates.com.conf
    
  11. In the nano editor, set up the configuration file like the sample below. Replace ServerAdmin with the email address of the person serving as your web site admin, and ensure that your domain is set in place of docsassociates.com, where applicable.

    <VirtualHost *:80>
        ServerAdmin jdoe@docsassociates.com
        DocumentRoot /var/www/docsassociates.com
    
        <Directory /var/www/docsassociates.com>
            Options Indexes FollowSymlinks
            AllowOverride All
            Require all granted
        </Directory>
        
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        
        <IfModule mod_dir.c>
            DirectoryIndex index.html index.php
        </IfModule>
        
    </VirtualHost>
    
  12. Press Ctrl + X, followed by Y then Enter to save and exit nano.

  13. Activate the site using the config file you just created:

    a2ensite docsassociates.com.conf
    

    📘

    You'll see a message stating that you need to restart apache2. Don't do this just yet.

  14. Run ls to view all of the config files in \sites-available. You'll probably see results like this:

    000-default.conf  default-ssl.conf  docsassociates.com.conf
    
  15. As a security precaution, make sure that all the *.conf files, except for the new one you just created are deactivated from Apache2. Run the a2dissite command for each one:

    a2dissite 000-default.conf
    a2dissite default-ssl.conf
    

    📘

    Some of these files may return a message of Site <name> already disabled. This is OK.

  16. Run this command to restart Apache2, to apply your updates:

    systemctl reload apache2