Setup a LAMP stack on Ubuntu (e.g. for hosting WordPress)

Mainly as a quick reminder for myself, I write down all the necessary steps to setup a LAMP stack on Ubuntu Linux. LAMP is a combination of software required to serve a dynamic PHP website.

L = Linux, A = Apache Webserver, M = MySQL or MariaDB database, P = PHP

Step 0: Update package sources

sudo apt update

Step 1: Install Apache, MariaDB, PHP

sudo apt install apache2
sudo apt install mariadb-server
sudo apt install php libapache2-mod-php php-mysql php-cli

Or all in one:

sudo apt install apache2 mariadb-server php libapache2-mod-php php-mysql php-cli

Step 2: Configure Apache to look for index.php first

nano /etc/apache2/mods-enabled/dir.conf

Move index.php to the first position. Save the file (Ctrl + O, Ctrl + X).

Step 3: Configure PHP for larger file upload size

sudo nano /etc/php/7.4/apache2/php.ini 

Look for upload_max_filesize and change it as you like (e.g. 10M). Do the same for post_max_size.

Then reload apache service:

sudo service apache2 restart

Step 4: Set MariaDB root password

First, login to the MariaDB shell as root (currently, it does not have any password, so just press enter when you are asked for a password.)

mysql -u root -p
(Enter)

Now, execute the following SQL query, and replace <password> with your desired root password, and then flush the privileges so that the changes can take effect.

grant all on *.* to root@localhost identified by '<password>' with grant option;
flush privileges;
quit;

Step 5 (optional): Create a MariaDB user and database for WordPress

Enter the mysql command line again using the command

mysql -u root -p

and enter the root password you just configured in the previous step.
Next, if you type

SHOW DATABASES;

you can see all current databases.

Next, we create a new database called “wordpress”, and a new user called “wp_user” and give it a password (replace <password> with your desired password).

CREATE DATABASE wordpress;
GRANT ALL PRIVILEGES ON wordpress.* TO "wp_user"@"localhost" IDENTIFIED BY "<password>";
FLUSH PRIVILEGES;

If you want, you can also limit the privileges of the user to be more secure. In this case, use this GRANT query instead:

GRANT ALL SELECT, INSERT, UPDATE ON wordpress.* TO "wp_user"@"localhost" IDENTIFIED BY "<password>";

That’s it. You can now extract the wordpress files to /var/www/html and configure it accordingly.

Step 6 (optional): Install WordPress

cd /var/www/html
wget https://de.wordpress.org/latest-de_DE.zip
unzip latest-de_DE.zip

Make sure, wordpress / PHP can write into files in /var/www/html:

sudo chown -R www-data:www-data /var/www/html/

Then visit the IP of your server and fill out the wordpress configuration. In case you get “Error establishing a database connection” but you are sure the user password is correct, this is usually because the database server is only allowing connections on 127.0.0.1, and not on localhost as well. To fix this, do the following:

cd /var/www/html/
cp wp-config-sample.php wp-config.php
nano wp-config.php

And fill in your database, username and password in the corresponding defines (DB_NAME, DB_USER, DB_PASSWORD). In the define for the host (DB_HOST), replace localhost with 127.0.0.1. Save the file (Ctrl + O, Ctrl + X). Now it should work.

Leave a Reply