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 optionsWhenever prompted to verify an install step, press
yfollowed by Enter to confirm. You may also see thePackage confirmationscreen asking which services should be restarted. Press Enter to select the defaults.
-
In a new terminal window, log into your Linode instance as your limited sudo user. Replace
USERandIP_ADDRESSwith your sudo username and Linode IPv4 address, respectively:ssh USER@IP_ADDRESS -
Download and install available software updates:
sudo apt update && sudo apt upgrade -
Install the Apache2 web server:
sudo apt install apache2 -
Next, install the MySQL database:
sudo apt install mysql-server -
Install PHP with additional modules for Apache2 and MySQL:
sudo apt install php libapache2-mod-php php-mysql -
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.
-
Using the
dpkg-reconfigureutility, launch thetzdatatool.dpkg-reconfigure tzdata -
Use your keyboard's arrow keys to navigate the interface and select your preferred time zone.
-
Run the
datecommand to verify your changes:dateYou 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.
-
Run the
hostnamecommand to view the current active hostname:hostnameA common output for a default hostname may look like:
localhost -
Using the
hostnamectlutility, change this to make it easier to identify—for example,primary-originfor your primary Linode,standby1-originfor the first standby, andstandby2-originfor the second:hostnamectl set-hostname primary-origin -
Run the
hostnamecommand 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.
-
Start by enabling UFW:
sudo ufw enable -
Open applicable ports by running the following
ufw allowcommands in succession:sudo ufw allow ssh sudo ufw allow OpenSSH sudo ufw allow 80 sudo ufw allow 443 sudo ufw allow in "Apache Full" -
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.
-
While logged into your primary instance, navigate to the default directory Apache2 uses to store website files,
/var/www:cd /var/www -
Create and navigate into a new directory for your site, using your domain name to make it easy to recognize. Replace the example
docasssociateswith your own domain:mkdir docassociates.com cd docsassociates.com -
Using a text editor, create a new
index.htmlfile to serve as your site's root page:nano index.html -
In the text editor that launches, create a basic HTML layout. Later, you can overwrite this page with your site's actual
index.htmlhome page.<html> <body> <h1>Welcome to Doc Associates!</h1> </body> </html>Make it different on your three LinodesThis 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.
-
Navigate to the directory where sites are managed in Apache2,
/sites-available:cd /etc/apache2/sites-available -
Create a new configuration file for your domain:
nano docasassociates.com.conf -
In your text editor, set up the configuration file like the example below. Replace
jdoe@docassociateswith the email address of your web site admin, and replacedocsassociates.comwith 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> -
Activate the site using the config file you just created:
a2ensite docsassociates.com.confThis 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.
-
Run
lsto 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 -
Make sure that all the
*.conffiles are deactivated from Apache2 except for the new one you just created. Run thea2dissitecommand for each one:a2dissite 000-default.conf a2dissite default-ssl.confSome of these files may return a message of
Site <name> already disabled. This is OK. -
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.
I got a 404If 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
Arecord entry with the correct domain and IP address for your primary Linode instance.
Updated 13 days ago
