DEV Community

Seong Bae
Seong Bae

Posted on

Install LAMP stack on Ubuntu

This post is to save in one place all the commands used to build LAMP stack on a Ubuntu server. I am creating this post so that I can reference this whenever I need to set up a new Ubuntu server for web development which happens quite often.

First, update package manager cache:

$ sudo apt update
Enter fullscreen mode Exit fullscreen mode

Install Apache web server:

$ sudo apt install apache2
Enter fullscreen mode Exit fullscreen mode

At this point, if you go to http://localhost, you should be able to see the Apache2 default page.

You can configure firewall for Apache but I'm usually setting up LAMP stack for local development so I'm going to skip configuring firewall.

Install MySQL server:

sudo apt install mysql-server
Enter fullscreen mode Exit fullscreen mode

Then configure MySQL server using the following command:

sudo mysql_secure_installation 
Enter fullscreen mode Exit fullscreen mode

Set mysql root password using following commands:

$ sudo mysql
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
mysql> exit
Enter fullscreen mode Exit fullscreen mode

Install PHP:

sudo apt install php libapache2-mod-php php-mysql
Enter fullscreen mode Exit fullscreen mode

Confirm php is installed:

$ php -v
PHP 8.3.6 (cli) (built: Dec  2 2024 12:36:18) (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
Enter fullscreen mode Exit fullscreen mode

Install PHP Extensions:

sudo apt install php8.3-cli php8.3-{bz2,curl,mbstring,intl,xml,mysql,fpm}
Enter fullscreen mode Exit fullscreen mode

Enable php-fpm and apache mod for php:

sudo a2enconf php8.3-fpm
sudo a2enmod proxy_fcgi setenvif
sudo a2enmod php8.3
Enter fullscreen mode Exit fullscreen mode

Restart apache2:

sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

If creating a new Laravel website, use the following apache config file to get started with creating a virtual site:

<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName mysite.test
    DocumentRoot /home/baese/projects/mysite/public/

    <Directory /home/baese/projects/mysite/public/>
        DirectoryIndex index.php
         AllowOverride All
         Require all granted
         Order allow,deny
         Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE