What Is a LEMP Stack?
A LEMP stack is a combination of four open-source technologies — Linux, Nginx (pronounced "Engine-X"), MySQL/MariaDB, and PHP — that together form a powerful environment for hosting dynamic websites, web applications, and platforms like WordPress on your Momo Cloud VPS.
| Component | Role |
|---|---|
| Linux | The operating system (Ubuntu/Debian) your VPS runs on |
| Nginx | The web server that receives and responds to HTTP/HTTPS requests |
| MySQL / MariaDB | The relational database that stores your website's data |
| PHP | The scripting language that generates dynamic page content |
Prerequisites
- An active Momo Cloud VPS at cloud.momo.tz running Ubuntu 22.04 or 24.04
- SSH access to the server (root or a sudo-enabled user)
- A domain name — optionally pointed to your VPS IP address via an A record using Momo Cloud nameservers
ns1.momo.tzandns2.momo.tz - Basic comfort running commands in a terminal
Step 1: Update Your System
Before installing anything, refresh the package list and apply any pending updates.
sudo apt update && sudo apt upgrade -y
Step 2: Install Nginx
Install the Nginx web server and start it immediately.
sudo apt install nginx -y
sudo systemctl enable nginx
sudo systemctl start nginx
Allow Nginx Through the Firewall
If you have UFW (Uncomplicated Firewall) active, open HTTP and HTTPS ports so traffic can reach your server.
sudo ufw allow 'Nginx Full'
sudo ufw status
You should see Nginx Full listed as ALLOW. To verify Nginx is working, open your server's IP address in a browser — you should see the default Nginx welcome page.
Tip: If you do not yet have UFW enabled, you can skip the ufw allow step for now, but make sure to set it up before going live to protect your server.
Step 3: Install MariaDB
MariaDB is a fully compatible, community-maintained fork of MySQL and is the recommended choice on modern Ubuntu.
sudo apt install mariadb-server -y
sudo systemctl enable mariadb
sudo systemctl start mariadb
Secure the Database Installation
Run the security script to set a root password and remove insecure defaults.
sudo mysql_secure_installation
Answer the prompts as follows:
- Switch to unix_socket authentication? — Press
N - Set root password? — Press
Yand choose a strong password - Remove anonymous users? — Press
Y - Disallow root login remotely? — Press
Y - Remove test database? — Press
Y - Reload privilege tables? — Press
Y
Step 4: Install PHP-FPM and Extensions
Nginx does not process PHP directly — it passes PHP files to PHP-FPM (FastCGI Process Manager). Install PHP-FPM along with the most commonly needed extensions.
sudo apt install php-fpm php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip -y
Confirm PHP is installed correctly:
php -v
Note the version number shown (for example 8.3) — you will need it in the next step.
Step 5: Create a Server Block (Virtual Host)
A server block tells Nginx where your website files are and which domain name to respond to. Replace yourdomain.com with your actual domain throughout this section.
Create the Web Root Directory
sudo mkdir -p /var/www/yourdomain.com/public
sudo chown -R www-data:www-data /var/www/yourdomain.com/public
Write the Server Block Configuration
sudo nano /etc/nginx/sites-available/yourdomain.com
Paste the following configuration. If your PHP version is different from 8.3, update the socket path accordingly.
server {
listen 80;
listen [::]:80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/yourdomain.com/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Save the file with Ctrl+O then Enter, and exit with Ctrl+X.
Step 6: Enable the Site and Reload Nginx
Create a symbolic link to activate the site, test the configuration for errors, then reload Nginx.
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
The nginx -t command must return syntax is ok and test is successful before you proceed. If it reports an error, re-open the config file and check for typos.
Step 7: Test PHP Processing
Create a small PHP info file to confirm that Nginx is correctly handing PHP requests to PHP-FPM.
sudo nano /var/www/yourdomain.com/public/info.php
Add this single line:
<?php phpinfo(); ?>
Save and exit, then visit http://yourdomain.com/info.php (or http://YOUR_SERVER_IP/info.php if DNS is not yet pointed). You should see the full PHP information page.
Tip: Remove info.php immediately after testing — leaving it publicly accessible exposes sensitive server configuration details.
sudo rm /var/www/yourdomain.com/public/info.php
Next Steps
Your LEMP stack is now running. To make your site reachable by domain name, log in to your Momo Cloud account and create an A record pointing your domain to your VPS IP address — DNS changes typically propagate within a few minutes to a few hours when using Momo Cloud nameservers ns1.momo.tz and ns2.momo.tz. Once your domain is resolving, the most important next step is securing your site with a free SSL/TLS certificate via Let's Encrypt using Certbot, which will enable HTTPS and protect your visitors' data. You can also install WordPress or any PHP application by uploading files to your web root and creating a dedicated MariaDB database and user for it.
Troubleshooting
502 Bad Gateway
This error almost always means Nginx cannot reach PHP-FPM. Check that:
- PHP-FPM is running:
sudo systemctl status php8.3-fpm - The socket path in your server block matches the actual PHP version installed. Run
ls /var/run/php/to see the correct.sockfilename and updatefastcgi_passaccordingly.
Default Nginx Page Still Showing
If you see the default Nginx welcome page instead of your site:
- Confirm the symlink was created in
/etc/nginx/sites-enabled/ - Check that
server_namematches the domain or IP you are visiting exactly - Make sure the
rootpath points to the correct directory and that anindex.phporindex.htmlfile exists there - Reload Nginx after any change:
sudo systemctl reload nginx
Database Connection Errors
If your application cannot connect to MariaDB:
- Double-check the database name, username, and password in your application's configuration file
- Confirm the database user exists and has the correct privileges: log in with
sudo mysql -u root -pand runSHOW GRANTS FOR 'youruser'@'localhost'; - Ensure MariaDB is running:
sudo systemctl status mariadb
Was this article helpful?