If you go to http://<host>
in a browser, you won't be able to see anything. The reasons is your server does not know how to respond to HTTP requests and terminate them. To handle HTTP requests, we need a Web Server.
A Web Server is a program (in case you did not understand so far, everything on a server is more or less a program) that can satisfy a Web request. It implements the HTTP protocol.
⚠️ warning
- frequent confusion: a "protocol" is a standard, not a program
The most famous Web Server is probably apache2
(you may have seen before files like .htpasswd
or .htaccess
which are configuration file for Apache).
Other famous Web Servers are:
nginx
: a more performant variant of Apachehttpd
: the oldest web server programcaddy
: a Web Server with an extremely easy configuration and SSL automation
In this chapter, we will install and configure nginx
.
⁉️ why
apache
is slower and older thannginx
, andnginx
can do many thingsapache
cannothttpd
is just not used by the industry anymorecaddy
is too easy (also, it is not always necessary to have SSL certificates - like when developing locally)
apt install nginx
If you open your favorite browser and input the IP of your server, you should see a Welcome to Nginx
message.
Configuration files for nginx
are located in etc/nginx
. The following directories/files are especially interesting for us:
nginx.conf
the default configuration for all virtualhostssites-available/
list of all websites/virtualhosts on the current serversites-enabled/
list of all running* websites/virtualhosts on the current server
* after nginx
is reloaded, cf below.
The site located in sites-enabled/default
is the default one provided by nginx
. We can safely remove it with:
rm /etc/nginx/sites-enabled/default
ℹ️ information
the
default
file is a symlink to a file located in/etc/nginx/sites-available
. So deleting the symlink is safe.
To restart nginx
with the latest changes, you can:
systemctl restart nginx
http://<host>
should be unavailable after this operation.
Let's duplicate the default site configuration:
cp /etc/nginx/sites-available/default /etc/nginx/sites-available/<something>
And let's put it in the "enabled" folder by symlinking it:
ln -s /etc/nginx/sites-available/<something> /etc/nginx/sites-enabled/
Restart the server, and we should be back to where we started and the website should be available again.
⁉️ whyIt can be disturbing to understand why we did not use the
default
site by default and why we needed all these manipulations. There are multiple reasons:
- so you can understand how
sites-available
andsites-enabled
workdefault
is a "default", not the website you want to deploy- if you screw up with the config of
<something>
(and it will happen), you can easily restore the default site configuration- I personally recommend to name your sites files based on their domain name
Let's have a look at the site configuration file:
cat /etc/nginx/sites-enabled/<something>
server {}
delimits a "virtualhost" (or server)listen
tellnginx
what ports it should listen todefault_server
indicates that in case of multiple virtualhosts, this one should be used when accessing the server via a domain name not listed in the configroot
indicates which folder to serve on your serverindex
define files that will be used as an index (accessinghttp://<host>/tada
will try to get<root>/tada/index.html
for exampleserver_name
is useful to declare which domain name this site will be using. This is extremely useful in case you have multiple websites with different domains on the same server._
is a "catch all".location {}
indicates a sub-block and different rules may be applied by sub-blocktry_files
is to indicate which filesnginx
should try to serve:try_files $uri $uri.html $uri/ =404;
sill for example try to serve the file$uri
, or the filename +$uri.html
, or the subdirectory$uri
, or will return a 404.
📖 Resources
Let's edit this file in order to adapt it to serve a static website:
nano /etc/nginx/sites-enabled/<something>
In location {}
, let's add
# Media: images, icons, video, audio, HTC
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|mp4|ogg|ogv|webm|htc)$ {
add_header Cache-Control "max-age=2592000";
access_log off;
}
# CSS and Javascript
location ~* \.(?:css|js)$ {
add_header Cache-Control "max-age=31536000";
access_log off;
}
This will tell the browsers to cache CSS, JS and other assets.
Also, let's enable gzip
by adding:
gzip on;
gzip_vary on;
gzip_min_length 10240;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
gzip_disable "MSIE [1-6]\.";
in the server {}
block.
There are a lot of other rules we could use (cf. the docs), but we'll keep it simple for now.
By default, on most servers, the website folders are located in /var/www/html
or /usr/share/nginx
. Let's go to this folder and create a basic html file:
nano /var/www/html/test.html
ℹ️ information
you can also use
touch
to create empty files
And place this little HTML page in it:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
After saving your file, you should be able to access http://<host>/test.html
and see the content of the page.
We can now create files and access them via the browser, let's enable HTTP2 and SSL certificates!