Now we have got our server install it’s time to install Nginx. This is pretty straight forward and I have covered it before but for this series I thought I would make sure I cover this simply.
First to makes things simple we will make sure we can access the server using an canonical URL instead of an IP address so fire up the server we created in part 2, log in with the username and password you set.
We will install Avahi to do this so on the command line write
sudo apt install avahi-daemonpress enter. Once avahi is installed make sure the hostname and hosts file are correct so on the command line again
sudo nano /etc/hostnameand make sure it reads
php-projectspress Ctrl o then Ctrl x to save and close nano. Again for hosts file
sudo nano /etc/hostsand make sure the second line reads
127.0.1.1 php-projects.local php-projectspress Ctrl o then Ctrl x to save and close nano and reboot the server with
sudo rebootNow we should be able to navigate to our server using the URL of ‘http://php-projects’, but we will do that when Nginx is installed.
We need now log in to our server to install Nginx. This is a trivial as getting it from the Ubuntu repos, so on the command line write
sudo apt install nginxpress enter
That should install Nginx for us. We can check Nginx is working by putting the URL ‘http://php-projects.local’ in our browser and you should see the default Nginx site like

Now done we need to configure it to serve our files. We will ask it to serve from a folder called ‘public’ our user folder. To do this we will need to create a ‘public’ folder in our home directory (‘/home/tutorial’) and to create a config file in ‘/etc/nginx/sites-enabled’ folder. First we will need to remove the default site config so on the command line
rm /etc/nginx/sites-enabled/defaultnow lets make our site config file with
sudo nano /etc/nginx/sites-enabled/php-projectsand in that file put
server {
root /home/tutorial/public;
server_name php-projects.local;
index index.html;
location / {
try_files $uri $uri/ = 404
}
}here we are defining at
- line 2: a root directory located at ‘/home/tutorial/public’
- line 3: the name of the website ‘php-projects.local’
- line 4: the file to serve automatically
- line 5: a location block to add some rules about serving pages
- line6: we ask Nginx to try the URI if there serve it else give us a 404 ‘Page Not Found’.
All this has to be within a server block.
Save this file with Ctrl o then Ctrl x and restart nginx with
sudo systemctl restart nginxWe will now make our public directory so enter
mkdir publicThen we will make a ‘index.html’ file and add a bit of html so to do this we use
nano public/index.htmlthen in this file put
<h1>Welcome to my website</h1>Save this file with Ctrl o then Ctrl x. So now to test this out we can refresh our browser (‘http://php-projects.local’) and you should see something like below

And that’s it for this one. In the next part we shall install MySQL rerady for the installation of PHP.
Thanks and there is a video to see me set this up below. Happy coding people! And as usual you can leave a comment too.
Watch on youtube: Setting Up A Test Environment For PHP In VirtualBox - Part 3 Installing Nginx