The Perfect Ubuntu Server LAMP Stack

The best home for your website or web app is a fresh Ubuntu server (21.04 at the time of this writing) with some essentials for web hosting. What follows is a simple step-by-step guide to installing Apache, MySQL, PHP, PhpMyAdmin, and vsftpd.

Need a Server?

I’m a huge fan of DigitalOcean. Great service, a fantastic control panel, and a bunch of locations all around the world. It’s worth noting they have a ton of different premade images you can use that include a LAMP stack, however I always recommend starting with a minimal clean install and installing and configuring it yourself.

Server Prep

To ensure security and the most likelihood everything works right, update your Ubuntu 21.04 Server:
sudo apt update
sudo apt upgrade -y

Apache Web Server

First things first, let’s install Apache:

sudo apt install apache2 apache2-utils -y

Make sure apache is already running if it isn’t already:

sudo systemctl start apache2

Set apache to start at server boot:

sudo systemctl enable apache2

If you’re running a firewall, which you should be, let’s allow apache through the firewall:

sudo iptables -I INPUT -p tcp --dport 80 -j ACCEPT

sudo ufw allow http

To verify things are working, visit your servers IP/hostname in your web browser:

(these are just examples and will not work) http://1.2.3.4 or http://myserver.example.com

MariaDB Database Server

We’re going to use MariaDB, a superior drop-in replacement for MySQL:

sudo apt install mariadb-server mariadb-client -y

Start mariadb if it isn’t already:

sudo systemctl start mariadb

Set mariadb to start at server boot:

sudo systemctl enable mariadb

Go through the initial MariaDB setup. Answer Y to everything.

sudo mysql_secure_installation

PHP 7.4

We’re going to install PHP and some common required packages:

sudo apt install php7.4 php-common php7.4-mysql php7.4-cli php7.4-common php7.4-json php7.4-readline libapache2-mod-php7.4 -y

Restart apache to get everything working:

sudo a2enmod php7.4

sudo systemctl restart apache2

PhpMyAdmin

PhpMyAdmin is a useful tool to easily manage MySQL databases and users.

sudo apt install phpmyadmin -y

Create the required symlink:

sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin

If everything went well, you should be able to access phpMyAdmin at http://example/phpmyadmin where example is your servers IP or hostname.

vsftpd FTP Server

vsftpd is a great choice because it’s easy to set up and generally just works. So install it!

sudo apt install vsftpd -y

Make sure vsftpd is running if it isn’t already:

sudo systemctl start vsftpd

And then set vsftpd to start at server boot:

sudo systemctl enable vsftpd

Allow FTP connections through the firewall:

sudo ufw allow 20/tcp

sudo ufw allow 21/tcp

Now, let’s make sure that authenticated FTP users can actually upload files, because that’s pretty useful. Open the vsftpd config file:

sudo nano /etc/vsftpd.conf

Find the write_enable entry and set it to write_enable=YES. Save and exit, and then restart vsftpd:

sudo systemctl restart vsftpd

Done 🙂

Have any feedback? Found my article useful? Or know a better way? I’d love to hear from you in the comments section below.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.