In this tutorial, we will learn how to setup LEMP stack, wordpress and gain enough knowledge to host basic websites using NGINX and be able to setup other basic PHP pages and connect to database as well.
Since we will be using nginx as our webserver, we need to configure LEMP stack on our ubuntu server.
L = Linux
E = Nginx
M = mysql
P = PHP
Page Contents
Part 1: Installing Nginx
Lets install nginx first:
Use these commands and you can also check screenshots for reference:
sudo apt update

sudo apt install nginx -y

Now enable the service nginx and check status:
sudo systemctl enable nginx
UFW (Uncomplicated Firewall) might be blocking your nginx. So lets allow nginx and all of its ports:
sudo ufw allow 'Nginx FULL'

You can check that nginx is listening on port 80 as well:
sudo netstat -tulnp

Enter your server IP address to check nginx:

If you see this page then it means your nginx is working properly.
Part 2: Setting up Nginx to work process PHP:
We are going to install PHP 8 since it is the latest version.
Follow these commands to add PPA (Personal Package Archives) for PHP 8:
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
Now install PHP 8 using these commands:
sudo apt install php8.0 -y
Verify your php installation:
php -v

Now use these commands to install necessary php packages including php8.0-fpm:
sudo apt install php8.0-common php8.0-mysql php8.0-xml php8.0-xmlrpc php8.0-curl php8.0-gd php8.0-imagick php8.0-cli php8.0-dev php8.0-imap php8.0-mbstring php8.0-opcache php8.0-soap php8.0-zip php8.0-intl php8.0-fpm -y
Employing PHP-FPM as the language interpreter means php requests will be processed via a TCP/IP socket. Nginx server handles HTTP requests only, while PHP-FPM interprets the PHP code.
In simple words, PHP-FPM is used in NGINX to handle all php codes.
Part 3: Installing MariaDB Mysql:
sudo apt install mariadb-server –y

Then perform secure installation of mysql by entering the command given below:
mysql_secure_installation

Set new password for root and take action according to your needs.
Part 4: Creating database for wordpress:
Enter into your mysql:
sudo mysql -u root -p
Enter your root password.
Enter these commands inside mariadb:
MariaDB [(none)]> CREATE DATABASE wp_blog;
MariaDB [(none)]> CREATE USER 'your_username'@'%' IDENTIFIED BY 'your_password';
MariaDB [(none)]> GRANT ALL ON wp_blog.* TO 'username'@'%';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;
You can check your database by logging in and viewing databases:

Part 5: Setting up WordPress and Nginx:
Download wordpress and extract it:
wget -c http://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz
A folder named wordpress will be available in your current directory.
Lets create a folder inside /var/www/html/ . I am going to name it blog.milan
sudo mkdir /var/www/html/blog.milan
Now move the extracted wordpress files to blog.milan
sudo mv wordpress/* /var/www/html/blog.milan
Part 6: Configuring nginx to serve website from our folder
Create a config file and open it:
nano /etc/nginx/sites-available/wordpress.conf
Paste these configuration codes and save it:
server {
listen 80;
server_name blog.milanmahat.com.np;
root /var/www/html/blog.milan;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ =404;
}
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
}
}

Here i have set server_name = blog.milanmahat.com.np. It means that my nginx will listen for this domain name and my domain name already has A record pointed to my server IP address. So, nginx will serve from root directory i.e. root = /var/www/html/blog.milan and display my wordpress site.
By default, nginx uses config files stored in /etc/nginx/sites-enabled folder. So, we need to make a symbolic link (Shortcut file) of our config file to this folder. This makes us easier to remove these config files from sites-enabled without deleting them whenever we dont want to run these configuration.
ln -s /etc/nginx/sites-available/wordpress.conf /etc/nginx/sites-enabled/wordpress.conf
You can check syntax of your nginx configuration by using:
nginx -t

Reload nginx configuration:
systemctl reload nginx
Now check your domain or ip address.
And click on Lets go.

Enter your details and press Submit.

Seems like we ran into some issue. This is because wordpress doesn’t have permission to create file inside our folder. For now, we will create it on our own.

Create a file named wp-config inside your wordpress directory. Check below command for example:
nano /var/www/html/blog.milan/wp-config.php
Now paste the contents from above figure into the wp-config.php file:

Save this file and then click on run the installation on your site.
This above error occured because wordpress (nginx) is not owner of the folder blog.milan. Make nginx the owner of the folder to prevent future permission errors:
sudo chown -R www-data:www-data /var/www/html/blog.milan
Here, www-data:www-data is the default username and group of nginx.
Now fill your info and install wordpress.

Now Login to your wordpress using your credentials:

Congratulations!! You did it. Now you are ready to use wordpress and start making your websites.
This is how we can setup wordpress in Ubuntu Server. In my next guide, i will teach you how to provide SSL certificate for your website using Letsencrypt.