Setting up a website often requires installing several components to ensure it runs smoothly. Nginx, MariaDB, and PHP make up a popular combination known as the LEMP stack. This guide will show you how to install all three on your Ubuntu server with just one command, making the process simple and fast.
What is the LEMP Stack?
The LEMP stack is a set of software that works together to power dynamic websites and web applications. It stands for:
- Linux (Operating System)
- Engine-X (Nginx – Web Server)
- MariaDB (Database Management System)
- PHP (Programming Language)
This combination is known for its performance, flexibility, and ease of use.
Why Choose Nginx, MariaDB, and PHP?
- Nginx: A high-performance web server that handles multiple connections efficiently.
- MariaDB: A robust and secure database system, a fork of MySQL, known for its speed and reliability.
- PHP: A widely-used scripting language that powers many websites and content management systems like WordPress.
Together, they provide a powerful foundation for building and managing websites.
Prerequisites
Before you begin, ensure you have the following:
- Ubuntu Server: This guide uses Ubuntu 20.04 LTS or later.
- Root or Sudo Access: You need administrative privileges to install software.
- Internet Connection: Required to download and install packages.
One-Command Installation of Nginx, MariaDB, and PHP
To install Nginx, MariaDB, and PHP with a single command, follow these steps:
- Update Package Lists:Open your terminal and update your package lists to ensure you have the latest information.
sudo apt update
- Install Nginx, MariaDB, and PHP:Use the following one-line command to install all three components together:
sudo apt install nginx mariadb-server php-fpm php-mysql -y
- nginx: Installs the Nginx web server.
- mariadb-server: Installs the MariaDB database server.
- php-fpm: Installs PHP FastCGI Process Manager.
- php-mysql: Enables PHP to communicate with MariaDB.
The
-y
flag automatically answers “yes” to any prompts during installation.
Configuring MariaDB Secure Installation
After installing MariaDB, it’s important to secure it.
- Run Security Script:Execute the security script to set a root password and remove insecure defaults.
sudo mysql_secure_installation
- Follow the Prompts:
- Set root password? Yes
- Remove anonymous users? Yes
- Disallow root login remotely? Yes
- Remove test database and access? Yes
- Reload privilege tables? Yes
This ensures your MariaDB installation is secure.
Testing Your LEMP Stack
To verify that Nginx, MariaDB, and PHP are working correctly, perform the following tests.
1. Check Nginx
- Open a Web Browser:Enter your server’s IP address in the address bar (e.g.,
http://your_server_ip
). - View Nginx Default Page:You should see the Nginx welcome page confirming it’s running.
2. Check PHP Processing
- Create a PHP Info File:
sudo nano /var/www/html/info.php
- Add the Following Code:
<?php phpinfo(); ?>
- Save and Exit:Press
CTRL + X
, thenY
, andEnter
to save. - View the PHP Info Page:Go to
http://your_server_ip/info.php
in your browser. You should see a page displaying PHP configuration details.
3. Check MariaDB
- Log into MariaDB:
sudo mysql -u root -p
- Enter Root Password:Type the password you set during
mysql_secure_installation
. - Verify Installation:Once logged in, run:
SELECT VERSION();
You should see the MariaDB version number.
- Exit MariaDB:
EXIT;
Managing Services
You can manage Nginx, MariaDB, and PHP services using systemctl commands.
Start Services
sudo systemctl start nginx
sudo systemctl start mariadb
sudo systemctl start php7.4-fpm
(Replace php7.4-fpm
with your installed PHP version if different.)
Enable Services to Start on Boot
sudo systemctl enable nginx
sudo systemctl enable mariadb
sudo systemctl enable php7.4-fpm
Check Service Status
sudo systemctl status nginx
sudo systemctl status mariadb
sudo systemctl status php7.4-fpm
Frequently Asked Questions (FAQ)
Can I install Nginx, MariaDB, and PHP with a single command?
Yes. You can install them together using the apt install
command with multiple package names.
Do I need to secure MariaDB after installation?
Yes. Running mysql_secure_installation
helps secure your MariaDB setup by setting a root password and removing insecure defaults.
How do I start Nginx, MariaDB, and PHP services?
Use the systemctl start
command followed by the service name. For example:
sudo systemctl start nginx
How can I check if PHP is working correctly?
Create a info.php
file in the web root with phpinfo();
and view it in your browser to see PHP configuration details.
What should I do if Nginx doesn’t start?
Check the status and error logs:
sudo systemctl status nginx
sudo tail -n 20 /var/log/nginx/error.log
Can I use a different version of PHP?
Yes. You can install other PHP versions by adding the appropriate repository and specifying the version during installation.
How do I update PHP packages?
Run the following commands:
sudo apt update
sudo apt upgrade php-fpm php-mysql
What is PHP-FPM?
PHP-FPM (FastCGI Process Manager) handles PHP requests efficiently, allowing Nginx to serve dynamic content.
Helpful Resources
Conclusion
Installing Nginx, MariaDB, and PHP on Ubuntu is now easier than ever with a single command. By following this guide, you can set up a powerful and efficient LEMP stack for your website or web application quickly. Remember to secure your MariaDB installation and regularly update your system to keep everything running smoothly.
If you encounter any issues during installation, refer to the helpful resources provided or reach out to the community forums for support. With Nginx, MariaDB, and PHP, you’re well on your way to creating a robust and scalable web environment.