--- title: "How to Install a LAMP Stack on Ubuntu 24.04" description: "Install Apache, MySQL and PHP on Ubuntu 24.04, open the firewall, create a virtual host for your domain and confirm that PHP can connect to the database." category: "Application Deployment" language: en source: https://cubepath.com/docs/application-deployment/lamp-stack-installation-linux-apache-mysql-php updated: 2026-09-25 --- # How to Install a LAMP Stack on Ubuntu 24.04 A LAMP stack is the classic combination of Linux, the Apache web server, the MySQL database and the PHP language. It runs a large share of PHP applications, from WordPress and Drupal to custom code. In this tutorial you will install Apache, MySQL 8.0 and PHP 8.3 on Ubuntu 24.04, create an Apache virtual host for your own domain and verify that PHP can talk to MySQL. ## Prerequisites To follow this tutorial you need: - A server running Ubuntu 24.04 LTS, for example a CubePath VPS, with at least 1 GB of RAM. - A non-root user with `sudo` privileges. - Optionally, a domain name with an A record pointing to your server's public IP. This tutorial uses `your_domain` as a placeholder. If you don't have a domain yet, you can use `your_server_ip` everywhere instead. ## Step 1 - Installing Apache and allowing web traffic Apache is available in Ubuntu's default repositories. Refresh the package index and install it: ```bash sudo apt update sudo apt install apache2 ``` The package starts the service and enables it at boot. Confirm that it is running: ```bash sudo systemctl status apache2 --no-pager ``` ```output ● apache2.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/apache2.service; enabled; preset: enabled) Active: active (running) since ... ``` If you use UFW, Apache registers application profiles for it. `Apache` opens port 80 only, `Apache Full` opens ports 80 and 443. Allow SSH first so you don't lock yourself out, then allow Apache Full: ```bash sudo ufw allow OpenSSH sudo ufw allow "Apache Full" sudo ufw enable ``` Check the rules: ```bash sudo ufw status ``` ```output Status: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere Apache Full ALLOW Anywhere ... ``` Open `http://your_server_ip` in your browser. You should see the "Apache2 Default Page". You can also test from the server itself: ```bash curl -sI http://localhost | head -n 1 ``` ```output HTTP/1.1 200 OK ``` ## Step 2 - Installing MySQL Install the MySQL server package. On Ubuntu 24.04 this installs MySQL 8.0: ```bash sudo apt install mysql-server ``` Verify that the service is active and check the version: ```bash sudo systemctl is-active mysql mysql --version ``` ```output active mysql Ver 8.0.x-0ubuntu0.24.04.x for Linux on x86_64 ((Ubuntu)) ``` Next, run the included security script. It can enable the password validation component, remove anonymous users, disallow remote root login and remove the test database: ```bash sudo mysql_secure_installation ``` Answer `Y` to the questions about removing anonymous users, disallowing remote root login, removing the test database and reloading the privilege tables. On Ubuntu the MySQL `root` account authenticates through the `auth_socket` plugin, so the script skips setting a root password. That is expected: only the system root user (or someone using `sudo`) can log in as MySQL root. Confirm that you can open the MySQL console: ```bash sudo mysql ``` ```output Welcome to the MySQL monitor. Commands end with ; or \g. ... mysql> ``` Type `exit` to leave the console. ## Step 3 - Installing PHP Apache runs PHP through the `libapache2-mod-php` module. Install PHP together with that module and the MySQL extension: ```bash sudo apt install php libapache2-mod-php php-mysql ``` On Ubuntu 24.04 these meta packages install PHP 8.3. Check the version: ```bash php -v ``` ```output PHP 8.3.6 (cli) (built: ...) (NTS) Copyright (c) The PHP Group Zend Engine v4.3.6, Copyright (c) Zend Technologies with Zend OPcache v8.3.6, Copyright (c), by Zend Technologies ``` Most applications also need a few common extensions. Install the ones used by WordPress, Laravel and similar projects: ```bash sudo apt install php-curl php-gd php-mbstring php-xml php-zip php-intl ``` Restart Apache so it loads the new modules: ```bash sudo systemctl restart apache2 ``` ## Step 4 - Creating a virtual host for your domain Apache serves the default page from `/var/www/html`. Instead of editing it, create a separate directory and virtual host for your site so you can host more sites on the same server later. Create the document root and give your user ownership of it: ```bash sudo mkdir -p /var/www/your_domain sudo chown -R $USER:$USER /var/www/your_domain ``` Create a new virtual host file: ```bash sudo nano /etc/apache2/sites-available/your_domain.conf ``` Add the following configuration, replacing `your_domain` with your domain name: ```apache ServerName your_domain ServerAlias www.your_domain DocumentRoot /var/www/your_domain AllowOverride All Require all granted ErrorLog ${APACHE_LOG_DIR}/your_domain_error.log CustomLog ${APACHE_LOG_DIR}/your_domain_access.log combined ``` `AllowOverride All` lets applications use `.htaccess` files, which WordPress and many frameworks rely on for pretty URLs. Enable `mod_rewrite` for the same reason, then enable the new site and disable the default one: ```bash sudo a2enmod rewrite sudo a2ensite your_domain sudo a2dissite 000-default ``` Test the configuration for syntax errors: ```bash sudo apache2ctl configtest ``` ```output Syntax OK ``` If the test prints `AH00558: Could not reliably determine the server's fully qualified domain name`, the configuration is still valid. You can silence the warning by adding `ServerName localhost` to `/etc/apache2/apache2.conf`. Reload Apache to apply the changes: ```bash sudo systemctl reload apache2 ``` ## Step 5 - Testing PHP processing Create a small PHP file in the new document root: ```bash nano /var/www/your_domain/info.php ``` Add this line: ```php PDO::ERRMODE_EXCEPTION] ); echo 'Connected to MySQL ' . $pdo->query('SELECT VERSION()')->fetchColumn() . PHP_EOL; ``` Request the page: ```bash curl http://your_domain/dbtest.php ``` ```output Connected to MySQL 8.0.x-0ubuntu0.24.04.x ``` If you see this line, Apache, PHP and MySQL are working together. Remove the test file, since it contains a password: ```bash rm /var/www/your_domain/dbtest.php ``` ## Step 7 - Securing the site with HTTPS (optional) If your domain already points to the server, you can get a free Let's Encrypt certificate with Certbot. Install Certbot and its Apache plugin: ```bash sudo apt install certbot python3-certbot-apache ``` Request a certificate for both names. Certbot edits the virtual host and sets up the HTTP to HTTPS redirect for you: ```bash sudo certbot --apache -d your_domain -d www.your_domain ``` Certbot installs a systemd timer that renews certificates automatically. Test the renewal process with a dry run: ```bash sudo certbot renew --dry-run ``` Then open `https://your_domain` to confirm the certificate is served. ## Troubleshooting - **The browser downloads the PHP file instead of running it.** The PHP module is not loaded. Run `sudo a2enmod php8.3` and `sudo systemctl restart apache2`. - **You still see the Apache default page.** The default site is still enabled or your `ServerName` does not match the address you are using. Run `sudo a2dissite 000-default`, check `ServerName` and reload Apache. - **`Access denied for user` in the PDO test.** The user or password does not match what you created. Recreate the user with `ALTER USER 'example_user'@'localhost' IDENTIFIED BY 'your_strong_password';` from `sudo mysql`. - **Apache fails to start.** Read the exact error with `sudo journalctl -u apache2 --no-pager -n 50` and check the site configuration with `sudo apache2ctl configtest`. ## Conclusion You now have a working LAMP stack on Ubuntu 24.04: Apache serving your domain from its own virtual host, MySQL 8.0 secured with `mysql_secure_installation`, and PHP 8.3 connecting to the database. From here you can deploy a PHP application such as WordPress into `/var/www/your_domain`, set up regular database backups with `mysqldump`, or switch Apache to PHP-FPM if you need to run several PHP versions side by side.