Ubuntu is one of the most popular Linux distributions for web development, thanks to its stability, flexibility, and extensive community support. If you’re looking to set up a local development environment on Ubuntu, there are a couple of stacks to choose from—namely LAMP (Linux, Apache, MySQL, PHP) and LEMP (Linux, Nginx, MySQL/MariaDB, PHP). Both stacks are excellent for web development, but the choice between Apache and Nginx can depend on personal preferences or the type of project you’re working on. This tutorial will guide you through setting up either LAMP or LEMP on Ubuntu, with step-by-step instructions to help you get started with web development.
What You'll Need
An Ubuntu machine (you can use a VPS, a local machine, or a virtual machine)
Basic knowledge of the Linux terminal and text editors like
nanoorvimInternet connection to download required packages
Setting Up LAMP Stack (Apache, MySQL, PHP) on Ubuntu
Step 1: Update Your System
Before you start installing any packages, make sure your system is up to date. Open the terminal and run:
sudo apt updatesudo apt upgradeThis will ensure all your existing packages are up to date and prevent potential compatibility issues.
Step 2: Install Apache Web Server
Apache is the most widely used web server and a central part of the LAMP stack. To install Apache, run the following command:
sudo apt install apache2Once installed, you can check if Apache is running by visiting http://localhost or http://127.0.0.1 in your web browser. You should see the Apache2 default welcome page.
To enable Apache to start on boot and start the service, run:
sudo systemctl enable apache2sudo systemctl start apache2Step 3: Install MySQL Database Server
MySQL is a powerful relational database management system. To install MySQL, run the following command:
sudo apt install mysql-serverAfter installation, you can secure your MySQL installation by running the security script:
sudo mysql_secure_installationThis script will allow you to set a root password, remove insecure default settings, and improve your MySQL installation's security.
To test that MySQL is running, log in to the MySQL shell with:
sudo mysqlYou should be greeted by the MySQL prompt, indicating that the database server is running.
Step 4: Install PHP
PHP is the server-side scripting language used to develop dynamic websites and web applications. Install PHP along with the necessary PHP modules by running:
sudo apt install php libapache2-mod-php php-mysqlAfter installation, check if PHP is working by creating a PHP test file:
echo "" | sudo tee /var/www/html/info.phpNow, open http://localhost/info.php in your web browser. You should see a page displaying your PHP configuration. Once done, remove the test file:
sudo rm /var/www/html/info.phpStep 5: Restart Apache
To apply the changes, restart Apache:
sudo systemctl restart apache2Now you have a fully functional LAMP stack set up on Ubuntu.
Setting Up LEMP Stack (Nginx, MySQL, PHP) on Ubuntu
For many developers, Nginx is preferred over Apache due to its speed and scalability, particularly for handling high volumes of traffic. If you prefer to set up a LEMP stack (Linux, Nginx, MySQL, PHP), follow the steps below.
Step 1: Install Nginx Web Server
To install Nginx, run the following command:
sudo apt install nginxOnce installed, start Nginx and enable it to start on boot:
sudo systemctl start nginxsudo systemctl enable nginxYou can verify that Nginx is working by visiting http://localhost or http://127.0.0.1 in your web browser. You should see the default Nginx welcome page.
Step 2: Install MySQL Database Server
As with the LAMP stack, you'll need to install MySQL or MariaDB (a popular MySQL fork). To install MySQL, run:
sudo apt install mysql-serverSecure the MySQL installation by running:
sudo mysql_secure_installationStep 3: Install PHP and PHP-FPM
Unlike Apache, Nginx doesn’t come with built-in PHP support, so you’ll need to install PHP and PHP-FPM (FastCGI Process Manager), which allows Nginx to process PHP scripts. Install PHP and the necessary modules:
sudo apt install php-fpm php-mysqlAfter installation, make sure the php-fpm service is running:
sudo systemctl start php7.4-fpm # Replace 7.4 with your PHP versionsudo systemctl enable php7.4-fpm # Replace 7.4 with your PHP versionYou can verify PHP-FPM is running by checking the status:
sudo systemctl status php7.4-fpm # Replace 7.4 with your PHP versionStep 4: Configure Nginx to Process PHP Files
To enable Nginx to process PHP files, you need to modify the Nginx configuration. Open the default configuration file:
sudo nano /etc/nginx/sites-available/defaultFind the location ~ \.php$ block and ensure it is configured like so:
location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
# Replace 7.4 with your PHP version
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
include fastcgi_params;}Once you’ve made the changes, save and close the file. Test the Nginx configuration to make sure there are no syntax errors:
sudo nginx -tIf the test is successful, restart Nginx to apply the changes:
sudo systemctl restart nginxStep 5: Test PHP with Nginx
Create a PHP info file to test that PHP is working with Nginx:
echo "" | sudo tee /var/www/html/info.phpNow, visit http://localhost/info.php in your web browser. If everything is set up correctly, you should see the PHP info page.
Once done, delete the test file:
sudo rm /var/www/html/info.phpConclusion
Now you have a fully functional local web development environment on Ubuntu, with either a LAMP stack (Apache, MySQL, PHP) or LEMP stack (Nginx, MySQL, PHP) set up on your machine. Both stacks are widely used in web development, and each has its own advantages—LAMP is a great choice for beginners and those familiar with Apache, while LEMP is a better choice for handling high traffic loads and is often preferred by those looking for a lightweight web server with greater performance.
With these stacks, you can start building and testing dynamic websites and web applications locally before deploying them to a production server. From here, you can add more tools like phpMyAdmin, Composer, or Laravel to further enhance your development workflow.
Happy coding!