Install and configure a LAMP stack

Once your Linode instance is created, booted, and secured with a limited sudo user, access it to install the Linux, Apache2, MySQL, and PHP (LAMP) software stack.

How long will this take? Approximately 30 minutes

During this step, 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

If you don't them it already, run the Linodes List operation to get the id and public IP address for your Linode. Replace $TOKEN with your Linode API token:

1. Installation

Install all of the LAMP components, including Apache2, MySQL, and PHP.

📘

Select the default options

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. Press Enter to select the defaults.

  1. In a new terminal window, log into your Linode instance as your limited sudo user. Replace USER and IP_ADDRESS with your sudo username and Linode IPv4 address, respectively:

    ssh USER@IP_ADDRESS
  2. Download and install available software updates:

    sudo apt update && sudo apt upgrade
  3. Install the Apache2 web server:

    sudo apt install apache2
  4. Next, install the MySQL database:

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

    sudo apt install php libapache2-mod-php php-mysql
  6. You can optionally install some commonly-used PHP modules. The modules 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 set when creating your Linode.

  1. Using the dpkg-reconfigure utility, launch the tzdata tool.

    dpkg-reconfigure tzdata
  2. Use your keyboard's arrow keys to navigate the interface and select your preferred time zone.

  3. Run the date command to verify your changes:

    date

    You should see your set time zone:

    Mon Jul 10 03:37:16 PM CDT 2023

Configure a custom hostname

Set a new hostname for your Linode instance to help identify it later on.

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

    hostname

    A common output for a default hostname may look like:

    localhost
  2. Using the hostnamectl utility, change this to make it easier to identifyfor 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 your change:

    root@localhost:~# hostname
    primary-origin

Configure and enable UFW

As a best practice, you can configure a firewall on your Linode as an extra layer of security. Uncomplicated Firewall (UFW) is a preinstalled frontend utility for managing firewall rules in Ubuntu operating systems, and it requires configuration to be active. To ensure good security practices, you only need to open a small number of ports for incoming connections, and you can leave all others closed.

  1. Start by enabling UFW:

    sudo ufw enable
  2. Open applicable ports by running the following ufw allow commands in succession:

    sudo ufw allow ssh
    sudo ufw allow OpenSSH
    sudo ufw allow 80
    sudo ufw allow 443
    sudo ufw allow in "Apache Full"
  3. To verify your firewall settings, run ufw status:

    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 the Reboot a Linode operation to reboot your Linode and apply all updates.

Test your Linode

Open up a browser on your local machine, and navigate to the public IP address for your Linode. You should see the default index page that Apache2 ships with. This means it can fulfill requests.

2. Create a Virtual Host

Another best practice is to create a virtual host (also known as a vhost) for your domain, even if you're only looking to host a single website on your Linode. A virtual hosts lets one server run multiple sites, making it easier to update your site and add more domains later.

  1. While logged into your primary instance, navigate to the default directory Apache2 uses to store website files, /var/www:

    cd /var/www
  2. Create and navigate into a new directory for your site, using your domain name to make it easy to recognize. Replace the example docasssociates with your own domain:

    mkdir docassociates.com
    cd docsassociates.com
  3. Using a text editor, create a new index.html file to serve as your site's root page:

    nano index.html
  4. In the text editor that launches, create a basic HTML layout. Later, you can overwrite this page with your site's actual index.html home page.

    <html>
      <body>
      <h1>Welcome to Doc Associates!</h1>
      </body>
    </html>
    👍

    Make it different on your three Linodes

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

  5. Navigate to the directory where sites are managed in Apache2, /sites-available:

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

    nano docasassociates.com.conf
  7. In your text editor, set up the configuration file like the example below. Replace jdoe@docassociates with the email address of your web site admin, and replace docsassociates.com with your domain, 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>
  8. Activate the site using the config file you just created:

    a2ensite docsassociates.com.conf

    This prompts a message stating that you need to restart Apache2. You don't need do this yet as it's done in the final step.

  9. Run ls to view all of the config files in /sites-available. You may see results similar to the following:

    000-default.conf  default-ssl.conf  docsassociates.com.conf
  10. Make sure that all the *.conf files are deactivated from Apache2 except for the new one you just created. 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.

  11. Finally, restart Apache2 to apply your updates:

    systemctl reload apache2

4. Test it

Launch a browser, and enter your domain in the search bar. If you did everything correctly, you should see the home page you set up.

A browser screenshot showing a basic index.html home page.
👍

I got a 404

If you don't see your page, it might be because your DNS entry hasn't fully propagated yet. Give it a few minutes, and try again. If it still doesn't work, make sure that you set up your A record entry with the correct domain and IP address for your primary Linode instance.