Skip to content

Instantly share code, notes, and snippets.

@appel
Forked from abobija/wsl2-ubuntu-lamp.md
Last active May 2, 2025 17:28
Show Gist options
  • Save appel/03b831ea402345628f1eadd0bdc09579 to your computer and use it in GitHub Desktop.
Save appel/03b831ea402345628f1eadd0bdc09579 to your computer and use it in GitHub Desktop.
LAMP/Docker stack on Ubuntu 24.04.3 LTS - Apache, MySQL, PHP

LAMP stack (+ docker) on Ubuntu 24.04.3 LTS

These are my personal notes and, as such, highly opinionated. Currently still editing these, so not everything is battle tested or ready and some things might seem contradictory. Nonetheless, hope it is useful!

Apache

sudo apt update && sudo apt upgrade 
sudo apt install -y apache2

Install single PHP version

sudo apt install -y php libapache2-mod-php
sudo apt install -y php-curl php-gd php-json php-mbstring php-xml

... Or, install multiple PHP versions

You can switch PHP versions via the php-switcher script, or on an individual basis via .htaccess

sudo add-apt-repository ppa:ondrej/php
sudo apt update -y
sudo apt install libapache2-mod-fcgid -y
sudo apt install -y \
  php{5.6,7.0,7.1,7.2,7.3,7.4,8.0,8.1,8.2,8.3,8.4} \
  php{5.6,7.0,7.1,7.2,7.3,7.4,8.0,8.1,8.2,8.3,8.4}-fpm \
  php{5.6,7.0,7.1,7.2,7.3,7.4,8.0,8.1,8.2,8.3,8.4}-mysql \
  php{5.6,7.0,7.1,7.2,7.3,7.4,8.0,8.1,8.2,8.3,8.4}-xml \
  php{5.6,7.0,7.1,7.2,7.3,7.4,8.0,8.1,8.2,8.3,8.4}-curl \
  php{5.6,7.0,7.1,7.2,7.3,7.4,8.0,8.1,8.2,8.3,8.4}-gd \
  php{5.6,7.0,7.1,7.2,7.3,7.4,8.0,8.1,8.2,8.3,8.4}-json \
  php{5.6,7.0,7.1,7.2,7.3,7.4,8.0,8.1,8.2,8.3,8.4}-redis \
  php{5.6,7.0,7.1,7.2,7.3,7.4,8.0,8.1,8.2,8.3,8.4}-intl \
  php{5.6,7.0,7.1,7.2,7.3,7.4,8.0,8.1,8.2,8.3,8.4}-mcrypt \
  php{5.6,7.0,7.1,7.2,7.3,7.4,8.0,8.1,8.2,8.3,8.4}-mbstring \
  libapache2-mod-php{5.6,7.0,7.1,7.2,7.3,7.4,8.0,8.1,8.2,8.3,8.4}

sudo service php7.0-fpm start && \
sudo service php7.1-fpm start && \
sudo service php7.2-fpm start && \
sudo service php7.3-fpm start && \
sudo service php7.4-fpm start && \
sudo service php8.0-fpm start && \
sudo service php8.1-fpm start && \
sudo service php8.2-fpm start && \
sudo service php8.3-fpm start && \
sudo service php8.4-fpm start

Then restart Apache

sudo service apache2 restart

MariaDB

sudo apt install -y mariadb-server php-mysql
sudo service mariadb restart
sudo mysql_secure_installation
    # Enter current password for root: (enter)
    # Switch to unix_socket authentication? N
    # Change the root password? Y
    # Remove anonymous users? Y
    # Disallow root login remotely? Y
    # Remove test database and access to it? Y
    # Reload privilege tables now? Y
# This is probably no longer neccesary
#sudo service mariadb stop
#sudo usermod -d /var/lib/mysql mysql
#sudo service mariadb start

... or MySQL

sudo apt install -y mysql-server php-mysql
sudo service mysql restart
sudo mysql_secure_installation
    #> Validate password component: N
    #> New password: MyPassword
    #> Remove anonymous users: Y
    #> Disallow root login remotely: Y
    #> Reload privilege tables now: Y
# This is probably no longer neccesary
#sudo service mysql stop
#sudo usermod -d /var/lib/mysql mysql
#sudo service mysql start

Allow remote root login

sudo mysql -u root -pMyPassword  -e "UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE User = 'root'; FLUSH PRIVILEGES;"

PhpMyAdmin

sudo apt install -y phpmyadmin
    #> Use apache2
    #> Configure db with dbconfig-common: Yes
    #> Random password (leave password blank)
sudo service mysql restart && sudo service apache2 restart

Now you can login into phpmyadmin with username root and password MyPassword.


Optional steps

Create the Apache Virtual Host File

# Copy the default config to a new file
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/localhost-dev.conf

# Edit the new configuration file using nano
sudo nano /etc/apache2/sites-available/localhost-dev.conf

Change or add the following (replace USERNAME with your username and double check the paths).

DocumentRoot /home/USERNAME/Localhost

# Maps /~projectname(.*) to /home/USERNAME/Localhost/Users/projectname/public$1
# ^/~             - Matches the literal start /~
# ([^/]+)         - Captures one or more characters that are NOT a slash (this is projectname, group $1)
# (.*)            - Captures the rest of the path/query string (group $2)
AliasMatch ^/~([^/]+)(.*)$ /home/<user>/Localhost/Users/$1/public$2

<Directory /home/USERNAME/Localhost>
    # Useful options: Indexes lets you see file listings if no index file exists.
    # FollowSymLinks allows Apache to follow symbolic links within this dir.
    Options Indexes FollowSymLinks
    # Allow .htaccess files to override configuration
    AllowOverride All
    # Allow access to this directory
    Require all granted
</Directory>

# Apply settings to the target directories of the AliasMatch
<DirectoryMatch "^/home/USERNAME/Localhost/Users/[^/]+/public">
    Options Indexes FollowSymLinks
    # Allow .htaccess files (essential for mod_rewrite in projects)
    AllowOverride All
    Require all granted
</DirectoryMatch>

Save and close the editor (Ctrl+X, then Y, then Enter in nano).

Enable Modules

# Enable rewrite module (if not already enabled)
sudo a2enmod rewrite

# Enable alias module (usually enabled by default, but doesn't hurt)
sudo a2enmod alias

# Disable the default site configuration
sudo a2dissite 000-default

# Enable your new site configuration
sudo a2ensite localhost-dev

Test Configuration and Reload Apache

# Check for syntax errors
sudo apache2ctl configtest

# If syntax is OK, reload Apache to apply changes
sudo systemctl reload apache2

PHP Switcher

curl -L https://raw.githubusercontent.com/appel/ubuntu-php-switcher/master/sphp | sudo tee /usr/local/bin/sphp
sudo chmod +x /usr/local/bin/sphp

Disallow root login remotely

sudo mysql -u root -pMyPassword  -e "UPDATE mysql.user SET plugin = 'auth_socket' WHERE User = 'root'; FLUSH PRIVILEGES;"

* If remotely login for root is disallowed then you need to create new MySql user, otherwise you will not be able to login into PhpMyAdmin.

LAMP Control Aliases

cd ~ && touch .bash_aliases
echo 'alias lampstatus="sudo service apache2 status ; sudo service mysql status"' >> .bash_aliases
echo 'alias lampstart="sudo service mysql start ; sudo service apache2 start"' >> .bash_aliases
echo 'alias lampstop="sudo service mysql stop ; sudo service apache2 stop"' >> .bash_aliases
echo 'alias lamprestart="lampstop ; lampstart"' >> .bash_aliases

Now you need to logout/login and then you can use lampstatus, lampstart, lampstop and lamprestart for controling LAMP stack.

Change document root of Apache2

In this example document root will be changed from /var/www/html to ~/www

cd ~
mkdir www
sudo sed -i "s;/var/www;$HOME/www;g" /etc/apache2/apache2.conf
sudo sed -i "s;/var/www/html;$HOME/www;g" /etc/apache2/sites-available/000-default.conf

Now you can go to ~/www and create index.html

Configure Apache2 for Wordpress module rewrite custom permalink structure

First with cd go into your wordpress installation folder and then:

sudo touch .htaccess
sudo chown -v :www-data .htaccess
sudo chmod -v 664 .htaccess
sudo sed -i "s/AllowOverride None/AllowOverride All/g" /etc/apache2/apache2.conf
sudo a2enmod rewrite
sudo service apache2 restart

Now go to WpAdmin -> Settings -> Permalinks. Choose permalink structure and hit "Save Changes". Now .htaccess should be populated with wordpress rewrite rules and conditions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment