There was a problem loading the comments.

How to Install a LEMP Stack (Nginx, MySQL, PHP) on Ubuntu

Support Portal  »  Knowledgebase  »  Viewing Article

  Print

Quick answer: Install a LEMP stack on Ubuntu by installing Nginx, MySQL (run mysql_secure_installation), and PHP-FPM, then adding a location block in your Nginx config that passes .php files to the PHP-FPM socket. Test with nginx -t and reload, then verify PHP with a temporary info.php file.

A LEMP stack - Linux, Nginx, MySQL, and PHP - is a fast, common foundation for websites and applications on Ubuntu. These steps install and connect all four components.

1. Update the system first

apt update && apt upgrade -y

2. Install Nginx

apt install nginx -y
systemctl enable --now nginx

Visit your server IP in a browser; the default Nginx welcome page confirms it is running. Make sure your firewall allows HTTP and HTTPS.

3. Install MySQL and secure it

apt install mysql-server -y
mysql_secure_installation

The secure-installation script sets the root password and removes insecure defaults. Answer yes to removing anonymous users, disallowing remote root login, and dropping the test database.

4. Install PHP for Nginx

Nginx does not process PHP itself; it hands PHP requests to PHP-FPM:

apt install php-fpm php-mysql -y

Note the PHP-FPM socket path shown after install (for example /run/php/php8.3-fpm.sock) - the server block needs it.

5. Configure an Nginx server block for PHP

In your site's server block, add the PHP handler so .php files are executed:

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}

Match the socket path to the one from step 4, then test and reload:

nginx -t && systemctl reload nginx

6. Verify PHP works

Create a file info.php in your web root containing <?php phpinfo();, load it in a browser, confirm the PHP page appears, then delete the file - it exposes server details and should not stay public.

Frequently asked questions

Why doesn't PHP run after installing it?
Nginx does not process PHP itself; you must add a location block that passes .php requests to PHP-FPM.

How do I find the PHP-FPM socket path?
It is shown after installing php-fpm, for example /run/php/php8.3-fpm.sock.

Prefer to skip the manual setup? A SoftSys managed Linux VPS can be delivered with your stack pre-installed and tuned, or with cPanel or Plesk if you would rather manage sites through a panel.


Share via
Did you find this article useful?  

Related Articles

Tags

© Softsys Hosting