# Installing a web server/Nginx web server
## Installing Nginx web server

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

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

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

### Installing and Operating Nginx web server on CentOS Linux

Installation packages for different Linux platforms is are available on Nginx linux package webpage. Follow this three easy steps on CentOS Linux:
* Update your package repository using http://nginx.org/en/linux_packages.html information
* run yum update
* sudo yum install -y nginx

#### Running Nginx web server

* Starting Nginx: sudo systemctl start nginx
* sudo systemctl enable nginx

#### Check Nginx running

* 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

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

## Basic Nginx Operation

* Check status: systemctl reload nginx (not output expected if everything runs properly)
* Starting Nginx: systemctl start nginx or nginx -s [stop|quit|reload|reopen]
* 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
### Basic Nginx Configuration

Example of the simplest possible server configuration file, listening on 80 port (default http port)
Example of the virtual host for www.example.com or example.com domain

## Checking Nginx errors

Errors are write to error log file /var/log/nginx/error.log, you can use grep to search for errors. For example:
or
Permission denied errors can be cause by Selinux security module misconfiguration, adding httpd_sys_content_t context can solve permission denied errors.

## Next Steps

Now you can check official documentation and learn how to
* Modify default error pages using error_page directive
* Use Access Control with HTTP Basic Auth using location directive , http_auth_basic module, htpasswd utility and .htpasswd files
* Use w:Digest access authentication
* Configure w:https using self-signed certificates using openssl (for example:openssl req -x590 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/ssl/private.key -out /etc/nginx/ssl/public.pem)
* Monitoring Nginx web server

## Additional Nginx documentation

* Nginx directives: http://nginx.org/en/docs/dirindex.html. Common Nginx directives are: server, server_name, listen, root

## Activities
### Basic

* Read Nginx release Notes / Changelog
* Install Nginx using docker

### Advanced

* Configure Nginx as reverse proxy with Websocket support