Ubuntu is a flexible foundation for any web development stack.
You have a brilliant idea for a new web application. The concept is mapped out, the design is taking shape, and you're eager to write the first line of code. But first, you face a critical task that can either be a smooth runway to launch or a swamp of configuration errors: setting up your local development environment.
As we discussed in our previous post, "Why Ubuntu is a Developer's Best Friend," this is an area where Ubuntu truly shines. Its stability and powerful package management make it the perfect canvas for any web stack you can imagine. Whether you're a fan of the time-tested classics or the modern JavaScript juggernauts, Ubuntu has you covered.
This guide is your one-stop-shop for getting a professional-grade web server running on your local Ubuntu machine. We'll walk you through the installation and basic configuration of the three most popular stacks: LAMP, LEMP, and MERN.
The Foundation: Preparing Your System
Before we install any specific stack, let's get our system into a clean, updated state and install a few essential tools that every developer needs.
First, open your terminal and run these commands to update your package lists and upgrade any existing software:
$ sudo apt update
$ sudo apt upgrade
Next, let's install a few foundational packages:
- build-essential: A meta-package that installs everything you need to compile software from source (like the GCC compiler and Make).
- git: The ubiquitous version control system.
- curl: A command-line tool for transferring data with URLs, essential for testing APIs and downloading files.
$ sudo apt install build-essential git curl
With our foundation laid, we're ready to build our first house.
The Classic Powerhouse: Setting Up a LAMP Stack
LAMP is the original open-source web stack, powering a huge portion of the web for decades. It's robust, well-documented, and the foundation for platforms like WordPress and Drupal.
LAMP stands for:
- Linux: Our operating system (Ubuntu).
- Apache: The web server software.
- MySQL: The relational database server.
- PHP: The server-side scripting language.
Step 1: Install Apache
Apache is available directly from Ubuntu's repositories. Installation is a single command:
$ sudo apt install apache2
To verify it's working, open your web browser and navigate to `http://localhost`. You should see the default Apache2 Ubuntu page.
Step 2: Install MySQL
Next, we'll install the MySQL database server:
$ sudo apt install mysql-server
After installation, it's highly recommended to run the included security script. This will help you set a root password, remove anonymous users, and secure your database.
$ sudo mysql_secure_installation
Step 3: Install PHP
Finally, we install PHP and the module that allows it to communicate with Apache and MySQL.
$ sudo apt install php libapache2-mod-php php-mysql
To test that PHP is configured correctly with Apache, create a test file:
$ sudo nano /var/www/html/info.php
Add the following line to the file, then save and close it:
<?php phpinfo(); ?>
Now, visit `http://localhost/info.php` in your browser. You should see a detailed page with your PHP configuration. Your LAMP stack is now live!
The Modern Performer: Setting Up a LEMP Stack
LEMP is very similar to LAMP, but it swaps out the Apache web server for a more lightweight and high-performance alternative: Nginx. It's known for its excellent performance under load and efficiency in serving static files.
LEMP stands for:
- Linux: Ubuntu.
- ENginx (pronounced Engine-X): The web server.
- MySQL: The database.
- PHP: The scripting language.
(Note: If you followed the LAMP guide, you'll need to stop and disable Apache first with `sudo systemctl stop apache2` and `sudo systemctl disable apache2`.)
Step 1: Install Nginx
$ sudo apt install nginx
Visiting `http://localhost` should now show the "Welcome to nginx!" page.
Step 2: Install MySQL
The process is identical to the LAMP stack. If you've already installed it, you can skip this step.
Step 3: Install PHP-FPM
This is the key difference. Nginx doesn't have a built-in module to process PHP files like Apache does. Instead, it passes PHP requests to a separate process manager. The standard one is called PHP-FPM (FastCGI Process Manager).
$ sudo apt install php-fpm php-mysql
Step 4: Configure Nginx to Use PHP
You need to edit the Nginx server block configuration file to tell it how to handle PHP files. Open the default configuration file:
$ sudo nano /etc/nginx/sites-available/default
Find the section for processing PHP, uncomment it, and make sure it looks like this (for Ubuntu 22.04, the PHP version might be 8.1):
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
Save the file, test your Nginx configuration, and then restart the service:
$ sudo nginx -t
$ sudo systemctl restart nginx
You can now use the same `info.php` test file as before. Your high-performance LEMP stack is ready to go!
The JavaScript Juggernaut: Setting Up a MERN Stack
MERN is an entirely different beast. It's a full-stack JavaScript framework for building modern, interactive single-page applications.
MERN stands for:
- MongoDB: A NoSQL, document-based database.
- Express.js: A back-end web framework for Node.js.
- React: A front-end JavaScript library for building user interfaces.
- Node.js: The JavaScript runtime environment.
Step 1: Install Node.js and npm
The best way to install Node.js is using nvm (Node Version Manager), as it allows you to easily switch between different Node versions. First, install nvm:
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
Close and reopen your terminal, then install the latest Long-Term Support (LTS) version of Node.
$ nvm install --lts
Verify the installation:
$ node -v
$ npm -v
Step 2: Install MongoDB
MongoDB is not in the default Ubuntu repositories, so we need to add its official repository first.
$ wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
$ echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
$ sudo apt update
$ sudo apt install -y mongodb-org
Then, start and enable the MongoDB service:
$ sudo systemctl start mongod
$ sudo systemctl enable mongod
Step 3: Setting Up Your Project (Express & React)
Unlike the other stacks, Express and React are not installed globally. They are libraries installed per-project using npm. A typical workflow would be:
- Create a project directory: `mkdir my-mern-app && cd my-mern-app`
- Create a server directory for your Express backend: `mkdir server && cd server`
- Initialize a Node.js project and install Express: `npm init -y && npm install express`
- Go back to the root and create a React frontend: `cd .. && npx create-react-app client`
You now have a fully functional MERN stack ready for development!
Conclusion: Your Launchpad is Ready
Congratulations! You've gone from a clean Ubuntu installation to a professional-grade development environment. Whether you choose the battle-tested reliability of LAMP, the performance of LEMP, or the modern JavaScript power of MERN, your local machine is now a perfect replica of the servers that power the internet.
This is the beauty of developing on Ubuntu: flexibility, power, and a direct line to the tools that build the modern web. Now, the only thing left to do is build something amazing.
Which stack did you choose? What are you planning to build? Share your projects and questions in the comments below!

Comments
Post a Comment