Setting Up A Testing Environment For PHP Using VirtualBox - Part 3 Installing Nginx

Setting Up A Testing Environment For PHP Using VirtualBox - Part 3 Installing Nginx

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-daemon

press enter. Once avahi is installed make sure the hostname and hosts file are correct so on the command line again

sudo nano /etc/hostname

and make sure it reads

php-projects

press Ctrl o then Ctrl x to save and close nano. Again for hosts file

sudo nano /etc/hosts

and make sure the second line reads

127.0.1.1       php-projects.local      php-projects

press Ctrl o then Ctrl x to save and close nano and reboot the server with

sudo reboot

Now 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 nginx

press 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

Screenshot%20from%202018-09-12%2015-10-05.pngNow  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/default

now lets make our site config file with

sudo nano /etc/nginx/sites-enabled/php-projects

and 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 nginx

We will now make our public directory so enter

mkdir public

Then we will make a 'index.html' file and add a bit of html so to do this we use

nano public/index.html

then 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 belowScreenshot%20from%202018-09-12%2018-16-46.pngAnd 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



12/09/2018 18:39:00 Shaun Freeman Filed Under: Virtual Box Linux, Nginx, Ubuntu

Twitter Feed
Shaun Freeman @Zendmaster

Shaun Freeman @Zendmaster

I liked a @YouTube video https://t.co/Uu3RzICAjp Installing a Hyper 212 EVO on AMD AM3+

Shaun Freeman @Zendmaster

Setting Up A Test Environment For PHP In VirtualBox - Part 6 Installing Multiple PHP Versionss: https://t.co/xNXeX4XDlQ via @YouTube

Shaun Freeman @Zendmaster

I added a video to a @YouTube playlist https://t.co/xNXeX4XDlQ Setting Up A Test Environment For PHP In VirtualBox - Part 6 Installing

Shaun Freeman @Zendmaster

Banished LP #1 - Humble Beginnings: https://t.co/5riUkijyLq via @YouTube

Shaun Freeman @Zendmaster

Installing Banished on Ubuntu 18.04: https://t.co/5judq1fMSc via @YouTube