How to Deploy Web Server on Ubuntu with NGINX, PHP and MySQL in No time

How to Install Nginx Web Server with Php and Mysql on Ubuntu Server 2204 Lts

How to Deploy Web Server on Ubuntu with NGINX, PHP and MySQL in No time

Home » Blog » Cookbook » Linux » Ubuntu » How to Deploy Web Server on Ubuntu with NGINX, PHP and MySQL in No time
Looking for an easy way to set up a web server on Ubuntu? Installing Nginx, PHP, and MySQL can be difficult, but that doesn't have to exist. In this tutorial, I'll provide a step-by-step guide on which to set up NGINX with the PHP and MySQL web server on Ubuntu 22.04. Together we will create a Webserver for hosting a blog with WordPress for the Development stage.
Table of Contents

You might wonder why I am setting up an NGINX, PHP, and MySQL-based web server on ubuntu when I could easily purchase a hosting plan and host the WordPress website on that hosting. The problem is that as soon as you host a website on a live server and associate it with a domain, Google starts crawling the website unless you manually ask it not to crawl and index the site. You might think that if Google starts crawling and it starts indexing, it is a good thing. Right? Well, hold on a second. When you first install WordPress or another CMS, say, on your hosting, you will notice that there is far too much demo content. Even when you are developing the site or designing the site as per your requirements, you will need too much demo content, which shouldn’t be indexed by Google. You will consistently modify your design, but the demo content will remain on your website. You have content on your website that will rank for the incorrect keywords. You could say that I can use the “nofollow” tag to prevent Google from indexing my site until I’m ready. Yeah, you can do that. But do you understand how much work you have to do to remove those “nofollow” tags once your site is ready? You can also select Discourage search engines from indexing this site in WordPress. If you choose this option after your site is ready, Google will take time to crawl and index it; in my experience, it takes about three months for Google to start crawling and four months for Google to index it.

Here are the steps to install the Nginx web server with PHP and MySQL on Ubuntu

Update the package index

This is actually the Basic step. Whenever you want to install any package, you need to update the package index so that you get the latest package of whatever you want to install

Terminal Command
sudo apt-get update

Install NGINX, PHP, and the required libraries

With the following command NGINX, PHP and other required libraries such as php-fpm and php-mysql

Terminal Command
sudo apt-get install nginx php-fpm php-mysql

When asked to continue type y and press Enter.

Once done you can check from any browser on local network with the IP of that server if the NGINX server is installed properly. If the installation is successful you will see something like the following

Install MySQL Server

Install MySQL by following command. When asked to continue type y and press Enter.

Terminal Command
sudo apt-get install mysql-server

Configure Nginx to use PHP-FPM

Now we have to configure NGINX to use PHP-FPM for handling PHP requests. Depending on the time of installation there might be a different version of PHP indexed while we have updated the package index in the beginning. To see which PHP has been installed use the following command.

Terminal Command
ps aux | grep php-fpm

In our installation the PHP version 8.1

To configure Nginx to use PHP-FPM open the default Nginx configuration file in a text editor. I use nano, you can use vm or whatever you are comfortable with

Terminal Command
sudo nano /etc/nginx/sites-enabled/default

In the file, find the line that says index index.html index.htm

Then change it to index index.php index.html index.htm

Find the location block that starts with location / { and add the following lines inside it

SSH Config
# pass PHP scripts to FastCGI server 
# 
location ~ \.php$ { 
  include snippets/fastcgi-php.conf; 
  fastcgi_pass unix:/run/php/php7.0-fpm.sock; 
} 

There are two ways you can do that. The first way is just to uncomment relevant lines and change the PHP version which I do not recommend.

This is how it would look after uncommenting and changing the PHP version.

Or, you can just copy and insert the above mentioned lines and change the php version.

Now press Ctrl+X and when asked to save press Y and then enter to save the file and exit the text editor.

Restart NGINX and PHP-FPM

Restart Nginx and PHP-FPM to apply the changes. Remember to change the PHP version.

Terminal Command
sudo systemctl restart nginx 
sudo systemctl restart php7.0-fpm

In conclusion, setting up a web server on Ubuntu 22.04 with NGINX, PHP, and MySQL is achievable with minimal effort. With the help of this guide, you can just follow the steps to install each component in order to successfully deploy your web server. You can now enjoy the benefits that come from having an effective web server for hosting content or applications.

That’s actually it. We are done with creating the Web Hosting server. Now let’s get along with installing WordPress as an example in this article, so that in future we can install any PHP, MySQL based CMS or Application on this server.

author avatar
roosho
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 
share this article.
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 

Enjoying my articles?

Sign up to get new content delivered straight to your inbox.

Please enable JavaScript in your browser to complete this form.
Name