Phone :  +370 (5) 204-1903
Email: sales@1gbits.com
  1. Dedicated server
  2. Blog
  3. How To Install Nginx on Centos 7
How To Install Nginx on Centos 7


Sep, 16

NGINX is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. NGINX is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption. NGINX is one of a handful of servers written to address the C10K problem. Unlike traditional servers, NGINX doesn’t rely on threads to handle requests. Instead it uses a much more scalable event-driven (asynchronous) architecture. This architecture uses small, but more importantly, predictable amounts of memory under load. Even if you don’t expect to handle thousands of simultaneous requests, you can still benefit from NGINX’s high-performance and small memory footprint. NGINX scales in all directions: from the smallest VPS all the way up to large clusters of servers.

  • Install Nginx on CentOS 7

First let’s start by ensuring your system is up-to-date.

yum -y update

Installing and Configuring NGINX in CentOS 7 Nginx is not yet available in CentOS 7 official repositories, so we have to add / install Nginx yum repository by issuing command below:

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx

Start Nginx and add it to automatically start on your system start-up using:

systemctl restart nginx
systemctl enable nginx

You can verify that Nginx is really running by opening your favorite web browser and entering the URL http://your-server’s-address, if it is installed, then you will see this:

  • Configure Nginx web server

Let’s say you have a domain mydomain.com and you like to use it to host a PHP based web application in /var/www/mydomain.com like WordPress, Joomla. To set-up Nginx serve requests for mydomain.com, and serve the PHP scripts in /var/www/mydomain.com you would have to create a server block in /etc/nginx/conf.d/mydomain.com.conf which would look something like:

### nano /etc/nginx/conf.d/mydomain.com.conf

server {
    server_name mydomain.com;
    listen 80;
    root /var/www/mydomain.com;
    access_log /var/log/nginx/mydomain.com-access.log;
    error_log /var/log/nginx/mydomain.com-error.log;
    index index.php;

    location / {
        try_files  $uri $uri/ /index.php?$args;

    location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
        access_log off;
        expires max;
    location ~ /\.ht {
        deny  all;
    location ~ \.php {
        try_files $uri = 404;
        fastcgi_index index.php;
        include /etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

Test and re-start Nginx using:

nginx -t
systemctl restart nginx
  • Configure IPTables or Firewall

CentOS 7 has a local firewall enabled by default. Proper TCP ports may need to be opened to allow external requests. This will usually be port 80 for normal HTTP traffic:

firewall-cmd --permanent --zone=public --add-port=80/tcp
firewall-cmd --reload

Congratulation’s! You have successfully installed Nginx.

author img


Leave A Comment