Installing a web server/Nginx web server

Installing Nginx web server

edit

w:Nginx web server that was written in 2004 with an explicit goal of outperforming the traditional and widely used Apache web server. Official documentation is on https://nginx.org/en/docs/.

Configuration File: /etc/nginx/nginx.conf
Additional Configuration file: /etc/nginx/conf.d/default.conf
Error log file: /var/log/nginx/error.log

Nginx vs Apache

edit

Apache was first released in 1995 while Nginx was first released 9 years later in 2004, so traditionally there has been a more broad knowledge of Apache web server than Nginx. We will present main differences from Apache to Nginx for newcomers. Both servers support virtual host configuration and include directives.

Configuration differences

edit
  • No concept equivalent to .htaccess files
  • Configuration file based on directives: nginx.conf[1] vs httpd.conf, arguably Nginx configuration language is easier to read.

Installing and Operating Nginx web server on CentOS Linux

edit

Installation packages for different Linux platforms is are available on Nginx linux package webpage[2]. Follow this three easy steps on CentOS Linux:

  1. Update your package repository using http://nginx.org/en/linux_packages.html information
  2. run yum update
  3. sudo yum install -y nginx

Running Nginx web server

edit
  1. Starting Nginx: sudo systemctl start nginx
  2. sudo systemctl enable nginx

Check Nginx running

edit
  • Check default webpage/virtualhost: curl http:://localhost
In case of error check error log file:/var/log/nginx/error.log
  • Check connection to example.com virtualhost: curl --header "Host: example.com" localhost

Configuring Nginx

edit

Configuration of Nginx on CentOS is done in /etc/nginx/nginx.conf (upstream github file[3])

Basic Nginx Operation

edit
  • Check status: systemctl reload nginx (not output expected if everything runs properly)
  • Starting Nginx: systemctl start nginx or nginx -s [stop|quit|reload|reopen][4]
  • Stopping Nginx: systemctl stop nginx
  • Reload configuration after every configuration change: systemctl reload nginx, Nginx has to be reload every time we want any configuration change take effect.
  • Check Configuration: nginx -t

Nginx Configuration

edit

Basic Nginx Configuration

edit

Example of the simplest possible server configuration file, listening on 80 port (default http port)

/etc/nginx/conf.d/default.conf
server {
    listen 80;
    root /usr/share/nginx/html;
}

Example of the virtual host for www.example.com or example.com domain

/etc/nginx/conf.d/default.conf
server {
    listen 80;
    server_name example.com www.example.com
    root /var/www/example.com/html;
}

Checking Nginx errors

edit

Errors are write to error log file /var/log/nginx/error.log, you can use grep to search for errors. For example:

grep error /var/log/nginx/error.log
2011/03/08 11:12:21 [error] 1755#1754 *8 "/var/www/example.com/html/index.html" is forbidden (13: Permission denied), client: 1.123.51.252, server: http://Example/URL.com, request: "GET / HTTP/1.1", host: "http://Example/URL.com"

or

grep emerg /var/log/nginx/error.log

Permission denied errors can be cause by Selinux security module misconfiguration[5], adding httpd_sys_content_t context can solve permission denied errors.

semanage fcontext -a -t httpd_sys_content_t "/var/www(/.*)?"
semanage fcontext -l | grep /var/www
restorecon -R -v /var/www

Next Steps

edit

Now you can check official documentation and learn how to

Additional Nginx documentation

edit

Activities

edit

Basic

edit
  1. Read Nginx release Notes / Changelog
  2. Install Nginx using docker

Advanced

edit
  1. Configure Nginx as reverse proxy with Websocket support

See also

edit

References

edit