My LEMP Setup Guide would not be complete if it didn’t show you how to setup your first virtual host (using your own domain name). This step will give you a good reference for setting up as many virtual hosts as you need on your server.
Getting Started
This guide assumes that you’ve followed along from the previous tutorial: How to Install and Configure a NGINX Server (LEMP Stack)
Important: to properly setup your virtual host you will need to be able to create a DNS A RECORD for a domain or subdomain you own. I use GoDaddy to register my domain names and they offer free DNS service. Check with your domain registrar as a similar service may be available to you.
First lets familiarize ourselves with the following three directories:
/var/www
: this is where virtual host site files are stored/etc/nginx/sites-available
: this is where virtual host config files are stored/etc/nginx/sites-enabled
: this is where symbolic links (or file shortcuts) to the virtual host config files are placed
Setting Up The Site Files
First up is the /var/www
directory where your site files will be kept. Lets start by creating a new directory for the new virtual host, as well as its web root directory:
mkdir /var/www/example.com mkdir /var/www/example.com/html
I typically setup the html
directory as the web root, to be able to put non web accessible files outside of the root directory.
Lets create a simply index.php
file which we will use to confirm that our new virtual host is working:
echo "Hello World" > /var/www/example.com/html/index.php
Setting Up The Nginx Virtual Host Config File
Now we must setup the Nginx config file for our virtual host. We will be creating individual config files for each virtual host in the /etc/nginx/sites-available
directory. Create the following file:
vim /etc/nginx/sites-available/example.com.conf
This file will have the following content:
server { server_name .example.com; access_log /var/log/nginx/example.com.access.log; error_log /var/log/nginx/example.com.error.log; root /var/www/example.com/html; index index.php index.html index.htm; # use fastcgi for all php files location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # deny access to apache .htaccess files location ~ /\.ht { deny all; } }
Important: I’ve created a new DNS A RECORD (using GoDaddy’s free Total DNS service) and pointed it to our new server’s IP address. I then change the following line, in the virtual host config file (see above), to reflect the domain name I am using:
server_name .yourdomain.com;
If you’ve been following along so far, basically all your virtual host configs will be defined in the /etc/nginx/sites-available
directory, to enable a config, you would create a symbolic link (or file shortcut) in the /etc/nginx/sites-enabled
directory, equally to disable a config, you would remove the symbolic link. Nginx will do the rest and search the /etc/nginx/sites-enabled
directory for active virtual host configs.
Now we will use the /etc/nginx/sites-enabled
directory and create a symbolic link to the virtual host config file:
ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/example.com.conf
And the final step here is to restart Nginx:
/etc/init.d/nginx restart
You should now be able to open up a browser and go to http://yourdomain.com/
where you should see a simple message reading “Hello World”.
you can speedup (expire) the pictures … with this settings
I also read about adding this… ?!?
Great guide, however if php-fpm are running through the socket name the php-block should be something like this:
location ~ \.php$
{
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
Nice and simple tut !
I found it helpful for Nginx beginner, look forward for some security configs too,
thanks.
First of all really nice tutorial i searched about 3 hours and now found this and its running thx 🙂
but i ve got the problem, i cant get access on subfolder.
How to solve this?
thanks for sharing, i’m looking for tutorial like this. very helpfull
How would one go about adding a subdomain to this?