How To Set Up The Nginx Web Server On Ubuntu

Prerequisites
You must set up the following first.
Context
  • Domain name: yourdomain.com
  • Hostname: bluedroplet.yourdomain.com
  • Username: alex
Steps
  1. SSH into your server
ssh alex@bluedroplet.yourdomain.com
  1. Install Nginx
  • Add the repository containing the mainline version
sudo add-apt-repository ppa:nginx/development -y
  • Update the package lists
sudo apt-get update
  • Install the package
sudo apt-get install nginx -y
  1. Confirm that Nginx has been installed
  • Check version
nginx -v
  • Open a browser and type in your host name or public IP address in the address bar, you should see an Nginx welcome page.
E.g. Go to http://bluedroplet.yourdomain.com or http://312.231.23.12
Screen Shot 2017-03-20 at 12.10.47 PM.png
  1. Configure Nginx
  • Enter the following commands and make note of the output.
grep processor /proc/cpuinfo | wc -l
ulimit -n
  • Edit the Nginx configuration file in /etc/nginx/nginx.conf.
sudo nano /etc/nginx/nginx.conf
Change the following settings in the config file:
For single user access servers, set user to the username that you’re currently logged in with
user alex; 
Set worker_processes to the amount of CPU cores your server has available
worker_processes 1;
Under events, set worker_connections to the server’s open file limit (output of the ulimit -n command) and set multi_accept on
events {
 worker_connections 1024;
 multi_accept on;
}
Under http, ensure the following settings are there and uncommented, and see the recommended values below. Add the setting if it’s not in the file.
http {
 ...
 keepalive_timeout 15;
 server_tokens off;
 client_max_body_size 64m;
 gzip_proxied any;
 gzip_comp_level 2;
 gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
 ...
}
  • Edit the fastcgi_params file.
sudo nano /etc/nginx/fastcgi_params
Ensure the following setting exists, if not add it to the file
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
Ensure that the configuration file contains no errors.
sudo nginx -t
  • If everything is ok, restart Nginx.
sudo service nginx restart
  • Open a browser and type in your host name or public IP address in the address bar, you should still see the Nginx welcome page.
E.g. Go to http://bluedroplet.yourdomain.com or http://312.231.23.12
  1. Create the catch all server block
  • Remove the default site config files
sudo rm /etc/nginx/sites-available/default
sudo rm /etc/nginx/sites-enabled/default
  • Edit the nginx.conf file:
sudo nano /etc/nginx/nginx.conf
Towards the bottom of the file you’ll find a line that reads:
include /etc/nginx/sites-enabled/*;
Underneath add the following:
server {
 listen 80 default_server;
 listen [::]:80 default_server;
 server_name _;
 return 444;
}
  • Ensure that the configuration has no errors.
sudo nginx -t
  • If everything is ok, restart Nginx.
sudo service nginx restart
  • Open a browser and type in your host name or public IP address in the address bar, you should see an error.
E.g. Go to http://bluedroplet.yourdomain.com or http://312.231.23.12

Comments

Popular posts from this blog

How To Set Up Quasar v0.14 On A Laravel+Vue Framework

How To Set Up Quasar v0.13 On A Laravel(5.4)+Vue Framework

How To Fix Random Disappearing Of HTML Elements On iPhone/iPad