Jump to content
Forums in Read-Only Mode - Please use Reddit ×
  • entry
    1
  • comments
    0
  • views
    10,946

Setting up a Web Server (Nginx)


spacemage

4,261 views

When I got a Funtoo container, the first thing I wanted to do with it, was host a website. I'm new at this, so I looked up numerous tutorials, but none of them were specific to Funtoo, so I thought I'd write my own, for future reference and for the benefit of other new Funtoo users. It's a pretty basic setup, but I welcome any suggestions as to how it can be expanded or improved. Here are all the steps that I have gone through in setting up my web server.

 

First, install nginx by typing:

 

# emerge nginx

 

The configuration file for Nginx is located at "/etc/nginx/nginx.conf". I found that the default configuration in Funtoo worked just fine, so I moved straight on to configuring the server. If you have your own domain name, you can configure your DNS to point that address at your Funtoo container, but the Funtoo container has a default address of "(name).host.funtoo.org", so that is the address that I will use for the rest of this tutorial. You can set up a server for that address by typing:

 

# nano /etc/nginx/sites-enabled/(name).host.funtoo.org

 

And adding this to the file:

>server {        listen 80;        server_name (name).host.funtoo.org;        root /var/www/(name).host.funtoo.org/;        location / {                index index.html index.htm;        }}

Then create the server root directory:

 

# mkdir /var/www/(name).host.funtoo.org

 

And add a simple test page:

 

# nano /var/www/(name).host.funtoo.org/index.html

 

Start the nginx service by typing:

 

# /etc/init.d/nginx start

 

Now the index page should be accessible from any web browser by typing "(name).host.funtoo.org" into the address bar. That's all it takes to host web sites on a Funtoo container, but I want my websites accessible through https, rather than http, I need certificates to verify my site's identity. You can get a free certificate from LetsEncrypt using a program called Certbot.

 

Install certbot by typing:

 

# emerge certbot

 

And use it by typing:

 

# certbot certonly --webroot -w /var/www/(name).host.funtoo.org -d (name).host.funtoo.org

 

Now there should be certificates for the website located at /etc/letsencrypt/live/(name).host.funtoo.org/

Next, to reconfigure the server to redirect visitors to ssl, type:

 

# nano /etc/nginx/sites-enabled/(name).host.funtoo.org

 

And change the file's contents to this:

>server {        listen 80;        server_name (name).host.funtoo.org;        return 301 https://$host$request_uri;}server {        listen 443 ssl;        server_name (name).host.funtoo.org;        root /var/www/(name).host.funtoo.org/;        ssl_certificate /etc/letsencrypt/live/(name).host.funtoo.org/fullchain.pem;        ssl_certificate_key /etc/letsencrypt/live/(name).host.funtoo.org/privkey.pem;        location / {                index index.html index.htm;        }}

Restart Nginx by typing:

 

# /etc/init.d/nginx restart

 

And the web site can now be accessed securely through ssl.

 

One thing that I found, was that when I have multiple domain names pointing at my server, if I don't have entries set up for them in Nginx, it has the default behavior of redirecting requests for unspecified domain names to the first available server. I'd rather not have this happen, so I edited the Nginx configuration file:

 

# nano /etc/nginx/nginx.conf

 

And just above the line that says "include /etc/nginx/sites-enabled/*;" I added this:

>server {        server_name _;        listen *:80 default_server deferred;        return 444;}

This makes it so that all defaulted requests return no response.

 

That's all that I've done to set up my web server so far. If you're a beginner like me, I hope you find it helpful, and if you're more advanced than me, any advice you can offer will be greatly appreciated

 

If anybody has any questions or suggestions, write a comment or send me a message. All requests for clarification, correction, expansion, and improvement are welcome, and I'll edit and add to this tutorial as necessary.

0 Comments


Recommended Comments

There are no comments to display.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...