How to install Nginx on CentOS 7

CentOS 7 has been online since a few months now. It includes many advantages and changes over the previous 6.5.x version. And if you are one of the lucky ones that have CentOS 7 installed, on this tutorial I will guide you on how to install Nginx on CentOS 7.  The only requirement for this tutorial is root access via shell and a fresh CentOS installation.


Installing Nginx on CentOS 7

Get the Nginx repo for EL 7:

mkdir /root/temp; cd /root/temp wget rpm -i nginx-release-centos-7-0.el7.ngx.noarch.rpm

Install Nginx

yum install nginx

Update Nginx on CentOS 7

Already have Nginx installed? Then you probably have the Nginx EL7 repo installed and all you need to do in order to update your Nginx version is run this command:

yum update nginx

Then reload Nginx service to apply changes:

service nginx reload

Nginx Configuration

The Nginx configuration files can be found at /etc/nginx, where /etc/nginx/nginx.conf is the main configuration file. This is a generic nginx.conf configuration file for CentOS:

user  nginx; worker_processes  8; # tweak according to # of cores you have error_log  logs/error.log crit;  worker_rlimit_nofile  8192;  events { worker_connections  800; # you might need to increase this setting for busy servers use epoll; #  Linux kernels 2.6.x }  http { server_names_hash_max_size 2048; server_names_hash_bucket_size 512;  server_tokens off;  include    mime.types; default_type  application/octet-stream;  sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout  10;  # Gzip on gzip on; gzip_min_length  1100; gzip_buffers  4 32k; gzip_types    text/plain application/x-javascript text/xml text/css;  # Generic & timeout configs ignore_invalid_headers on; client_max_body_size    20m; client_body_buffer_size 15m; client_header_timeout  400; client_body_timeout 400; send_timeout     400; connection_pool_size  256; client_header_buffer_size 4k; large_client_header_buffers 4 32k; request_pool_size  4k; output_buffers   4 32k; postpone_output  1460;  # Cache most accessed static files open_file_cache          max=10000 inactive=10m; open_file_cache_valid    2m; open_file_cache_min_uses 1; open_file_cache_errors   on;  # Vhosts includes include "/etc/nginx/conf.d/*.conf"; }

Setting up Nginx Virtual Hosts

You can create your virtual hosts files (domain configurations) like this:

nano -w /etc/nginx/conf.d/

This is a generic vhost configuration for Nginx:

###  server { access_log off; log_not_found off; error_log  logs/error_log warn;         listen       80;          server_name;          location / {             root  /var/www/;             index  index.php index.html index.htm;         }          location ~* .(gif|jpg|jpeg|png|ico|wmv|3gp|avi|mpg|mpeg|mp4|flv|mp3|mid|js|css|wml|swf)$  {         root   /var/www/;                 expires max;                 add_header Pragma public;                 add_header Cache-Control "public, must-revalidate, proxy-revalidate";         }   	# PHP-FPM: uncomment if you use php-fpm for php processing          #location ~ .php$  {         #    root           /var/www/;         #    try_files $ uri =404;         #    fastcgi_pass   unix:/tmp/php5-fpm.sock;         #    fastcgi_index  index.php;         #    fastcgi_param  SCRIPT_FILENAME  $ document_root$ fastcgi_script_name;         #    include        fastcgi_params;         #    fastcgi_buffer_size 128k;         #    fastcgi_buffers 256 4k;         #    fastcgi_busy_buffers_size 256k;         #    fastcgi_temp_file_write_size 256k;         #}   }

Reload Nginx to apply changes:

service nginx reload

That’s all, now you have latest Nginx version installed on CentOS 7 and a few configuration examples to get rolling.

You might also like More from author