A blurred closeup photo of a network router

Despite the rapid growth in popularity of Go, JavaScript, and Ruby, PHP remains one of the most essential server-side languages for web servers: Usage statistics of server-side programming languages for websites (2026-11-28)

Personally, I like PHP and find it more enjoyable to work with compared to other solid choices for web development like JavaScript, Python, and C++. Even though, the official documentation and many guides suppose that you'll be using Apache with PHP, I've never been a fan of Apache. In this post, I'll show you how to set up your own LEMP stack, where Apache has been replaced by nginx and PHP by PHP-FPM.

First, install nginx:

apt install nginx

It's nice to see that nginx requires only about 2MB of disk space: Installing nginx using apt

If you have a firewall, open ports. In case UFW do the following:

ufw allow http
ufw allow https

Additionally, you can issue self-signed certificates and configure the default server as described in this post: How to issue self-signed certificates for nginx for the default server.

Install php-fpm:

apt install php-fpm

Additional dependencies will be installed: Installing php-fpm using apt

To check that php-fpm was correctly installed, run:

systemctl status php*

As you can see, php-fpm version 8.5 was installed and it is working: Using systemctl to check php-fpm status

The next step is to install a database. Instead of MySQL, I'll be installing MariaDB as it is something I'm familiar with:

apt install mariadb-server

Compared to nginx and php-fpm, MariaDB requires significantly more dependencies: Installing mariadb-server using apt

In the past, you had to execute mariadb-secure-installation but it is not necessary anymore:

NOTE: MariaDB is secure by default in Debian. Running this script is
      useless at best, and misleading at worst. This script will be
      removed in a future MariaDB release in Debian. Please read
      /usr/share/doc/mariadb-server/README.Debian.gz for details.

On my server, the message looked like this in the terminal: mariadb-secure-installation is not necessary anymore

Check that the database was correctly installed:

systemctl status mariadb

MariaDB server was started and is working: Using systemctl to check MariaDB status

At this point the LEMP stack was installed. In the next section I will show how you can set up php-fpm, and deploy a project.

Setting up the installed LEMP stack

I would recommend running each php project from a dedicated unix user, so if the project was compromised, there wouldn't be a global outage across all php projects. Also, by doing so you'll be able to monitor the system better.

Create a new Unix user:

useradd -m -s /usr/sbin/nologin username

Create a custom php-fpm pool configuration file:

cp /etc/php/8.5/fpm/pool.d/www.conf /etc/php/8.5/fpm/pool.d/username.conf

Edit the created configuration:

nano /etc/php/8.5/fpm/pool.d/username.conf

The important fields to change:

; The name of the pool
[username]

; User and group of the processes
user = username
group = username

; The address on which you want FastCGI requests be accepted
; In this case it is a unix socket
listen = /run/php/php8.5-fpm-username.sock

If you're using the default nginx configuration, the nginx service is likely running as the www-data/www-data user. In that case, you usually won't need to modify the following two fields in the php-fpm pool's configuration:

listen.owner = www-data
listen.group = www-data

Here is an example of what the configuration might look like: Modifying php-fpm pool configuration file

Before restarting php-fpm, it may be a good idea to validate settings, so there are no downtimes:

# Edit the 8.5 part to represent your php-fpm version
php-fpm8.5 -t

If everything is ok, restart php-fpm:

systemctl restart php*

Check the newly created php-fpm pool:

ls -l /run/php/

As you can see, a php-pool was created: Checking php-fpm running unix sockets

Many php services require a database to store data. Most of the time, the way to set up a database will be described in the service documentation. In this section I'd like to show an example of creating a generic database.

Connect to the db as a root user:

mysql -u root -p

Use sudo to bypass the password check.

Create a database:

CREATE DATABASE dbname CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Create a user:

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';

Give access to the db to the user:

GRANT ALL PRIVILEGES ON dbname.* TO 'username'@'localhost';

Apply all changes and quit:

FLUSH PRIVILEGES;
QUIT;

You may create as many users and databases as you want. Here is how I created a user and db on my server: Creating a user and db using mariadb-server

Don't forget to install a connector to work with the db:

apt install php-mysql

At this point, you have created a php-fpm pool and a database The last thing it to set up nginx correctly. Unfortunately, there is no universal way of doing it. Most of the time you want to add and set the following settings in your server block:

# Location of the service's source code
root /home/wordpress/WordPress;

# Automatically load index.php as the default homepage
index index.php;

# If the requested file exist, then serve it
# Otherwise, route the request to index.php
location / {
    try_files $uri $uri/ /index.php?$query_string;
}

# Process php files using php-fpm and the created unix socket
location ~ \.php$ {
    try_files $uri =404;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

    fastcgi_pass unix:/run/php/php8.5-fpm-wordpress.sock;
}

Congratulations! Basically you have the main parts of the LEMP stack installed and set up, and you're ready to deploy a php service.

Just for the sake of clarity, in the next section, I'll install WordPress.

Installing WordPress

In this section I'll install Wordpress. Instead of downloading an archive with the source code, I'll use Git to clone the repository.

Install Git:

apt install git

Install the plugin to work with the database:

apt install php-mysql

Login as the created user and switch to the home directory:

su -s /bin/bash username
cd

Clone the WordPress repository:

git clone --recursive https://github.com/WordPress/WordPress.git

Cloning the Git repository may take some time: Cloning the WordPress repository using Git

The repository was cloned in the home directory of the created user: WordPress cloned Git directory in the home directory

For me, the repository is located in the /home/wordpress/WordPress/ directory. The important thing is to allow other Unix users, especially www-data, to freely access this directory. To do so, you may set up access rights in this way:

chmod 711 /home/wordpress
find /home/wordpress/WordPress -type d -exec chmod 755 {} +
find /home/wordpress/WordPress -type f -exec chmod 644 {} +

The next step is to set up a nginx server block.

Create a server block:

nano /etc/nginx/sites-available/example.com

Exit the created server block:

server {
    listen 80;
    listen [::]:80;

    root /home/wordpress/WordPress;

    index index.php;

    server_name example.com;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        fastcgi_pass unix:/run/php/php8.5-fpm-wordpress.sock;
    }
}

Create a symbolic link, so nginx can find configuration:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Check that there are no errors:

nginx -t

Restart nginx:

systemctl restart nginx

Here is how the server block looks like in my case: nginx default server block for WordPress I run this LEMP stack in an Incus container, that's why I use a single nginx server block and it is the default server Just omit the default_server parts and set the correct server_name for your particular website.

The rest of the setup will be performed in a browser.

Connect to the webserver: Chrome tab with the IP of the server

Start the setup process by selecting the default language: Installing WordPress - Selecting the default language

Read and accept the additional information: Installing WordPress - Accepting the additional information

Enter the previously created database information into the fields: Installing WordPress - Connecting to the db

Run the installation: Installing WordPress - Run the installation

Enter additional information, like the site title and the administrator account details: Installing WordPress - Site title and administrator account

Congratulations, WordPress has been installed: Installing WordPress - WordPress has been installed

Now you can log in as the administrator account you've created: Working with WordPress - Log in

You should have access to the dashboard: Working with WordPress - Dashboard

Congratulations, you've successfully installed the LEMP stack on an Ubuntu machine and are now able to host PHP services: The LEMP stack is working

Note: How to install php-fpm plugins/extensions

Most of the time, popular php plugins have already been added to the repository and you can simply install them using apt: Installing php plugins using apt

See also



Categories: hosting server nginx php