LAMP Server Installation Guide on Ubuntu 24.04 LTS

From Acenet Knowledgebase
Jump to navigation Jump to search

This guide shows how to set up a LAMP server (Linux, Apache, MariaDB and PHP) on Ubuntu 24.04 LTS, so you can host websites without a control panel. When you finish, your server will run:

  • Apache 2.4.58 web server
  • MariaDB 10.11 database server (a drop-in replacement for MySQL)
  • PHP 8.3
  • A free Let's Encrypt SSL certificate for your website

Ubuntu 24.04 LTS (Noble Numbat) receives standard security updates until April 2029.

The version numbers are from October 2026; regular updates may install newer releases of the same versions.

Before you start

  • A server running a fresh installation of Ubuntu 24.04 LTS, such as an Acenet cloud server, virtual server or dedicated server, with no control panel installed.
  • Root access over SSH. Run every command in this guide as root, or put sudo in front of it.
  • A domain name whose A record points to your server's IP address, for the website and SSL steps. In this guide, replace example.com with your own domain.

Step 1: Update the server

apt update
apt -y upgrade

If the update installed a new kernel, restart the server with reboot and log in again.

Step 2: Install Apache

apt -y install apache2
systemctl status apache2

Apache starts automatically and is set to start at boot. Open http://your-server-ip/ in a browser. You should see the "Apache2 Default Page". If the page doesn't load, check the firewall (step 3).

Step 3: Open the firewall

Ubuntu includes the ufw firewall, which is turned off by default. If you turn it on, allow SSH first so you don't lock yourself out (change 22 if your SSH server uses a different port):

ufw allow 22/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw enable
ufw status

If you use a different firewall, allow incoming connections on ports 80 (HTTP) and 443 (HTTPS).

Step 4: Install MariaDB

apt -y install mariadb-server
mariadb-secure-installation

The mariadb-secure-installation script asks a few questions:

  • Enter current password for root: press Enter (there is none yet).
  • Switch to unix_socket authentication and Change the root password: answering n is fine. The database's root user can only log in as the server's root user, through mariadb on the command line, which is secure without a password.
  • Answer Y to remove anonymous users, disallow root login remotely, remove the test database and reload the privilege tables.

Step 5: Install PHP

Install PHP, Apache's PHP module and the extensions most websites need (for example WordPress), then restart Apache:

apt -y install php libapache2-mod-php php-mysql php-gd php-mbstring php-xml php-intl php-zip php-curl
systemctl restart apache2
php -v

Step 6: Test PHP

Create a test page:

echo '<?php phpinfo();' > /var/www/html/info.php

Open http://your-server-ip/info.php. You should see the PHP information page showing PHP 8.3. Then delete the test page, because it shows details about your server that visitors shouldn't see:

rm /var/www/html/info.php

Step 7: Create a database and user for your website

Log in to MariaDB as root:

mariadb

Run these commands, replacing the database name, user name and password with your own (use a long, random password):

CREATE DATABASE exampledb;
CREATE USER 'exampleuser'@'localhost' IDENTIFIED BY 'a-strong-password';
GRANT ALL PRIVILEGES ON exampledb.* TO 'exampleuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Your website's software connects with these details and host localhost.

Step 8: Set up your website

Each website gets its own folder and its own Apache configuration file (a "virtual host").

1. Create the folder and give it to Apache's user:

mkdir -p /var/www/example.com/public_html
chown -R www-data:www-data /var/www/example.com

2. Create /etc/apache2/sites-available/example.com.conf:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html

    <Directory /var/www/example.com/public_html>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>

3. Turn on the website and the rewrite module (used by most applications for friendly URLs), check the configuration and reload Apache:

a2ensite example.com.conf
a2enmod rewrite
apache2ctl configtest
systemctl reload apache2

Upload your website's files to /var/www/example.com/public_html. AllowOverride All lets the website use .htaccess files, which many applications need. Optionally, turn off Apache's default page with a2dissite 000-default.conf and reload Apache.

Step 9: Add a free SSL certificate

apt -y install certbot python3-certbot-apache
certbot --apache -d example.com -d www.example.com

Certbot asks for an email address for renewal notices, gets the certificate, and adds an HTTPS virtual host for your website. When it asks, choose to redirect HTTP to HTTPS.

Let's Encrypt certificates last 90 days. Automatic renewal is set up when Certbot is installed. To test it:

certbot renew --dry-run

Keep your server updated

Install updates regularly, so you get security fixes for Apache, MariaDB, PHP and the operating system:

apt update
apt -y upgrade

To install security updates automatically, you can use the unattended-upgrades package.

Where to find things

Item Location
Apache configuration /etc/apache2/ (websites in sites-available/, turned on with a2ensite)
Apache logs /var/log/apache2/
PHP settings /etc/php/8.3/apache2/php.ini
MariaDB settings /etc/mysql/mariadb.conf.d/
Restart a service systemctl restart apache2 (or mariadb)

If you have any questions, please open a support ticket from your client area.