How to Install Virtualhost NGINX Server on CentOS 7

Add the Nginx repository to your server’s list of software sources
If you only need Nginx, you can install it through Nginx’s yum repository:

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

Download and install Nginx, you can use yum to download and install Nginx.

$ sudo yum install nginx

we need to make a directory structure that will hold the site data to serve to visitors:

sudo mkdir -p /var/www/yourdomain1.com/html
sudo mkdir -p /var/www/yourdomain2.com/html

change the ownership with chown:

sudo chown -R $USER:$USER /var/www/yourdomain1.com/html
sudo chown -R $USER:$USER /var/www/yourdomain2.com/html
sudo chmod -R 755 /var/www

create demo page on every directory:

nano /var/www/yourdomain1.com/html/index.html
nano /var/www/yourdomain2.com/html/index.html

example page:

<html>
  <head>
    <title>Welcome!</title>
  </head>
  <body>
    <h1>Success!</h1>
  </body>
</html>

create NGINX server configuration for virtual host:

sudo mkdir /etc/nginx/sites-available
sudo mkdir /etc/nginx/sites-enabled

include those directory in nginx.conf:

sudo nano /etc/nginx/nginx.conf

add these line:

include /etc/nginx/sites-enabled/*.conf;
server_names_hash_bucket_size 64;

create the vhost configuration:

sudo nano /etc/nginx/sites-available/yourdomain1.com.conf

example:

server {
    listen  80;

    server_name yourdomain1.com www.yourdomain1.com;

    location / {
        root  /var/www/yourdomain1.com/html;
        index  index.html index.htm;
        try_files $uri $uri/ =404;
    }

    error_page  500 502 503 504  /50x.html;
    location = /50x.html {
        root  /usr/share/nginx/html;
    }
}

enable those configuration:

sudo ln -s /etc/nginx/sites-available/yourdomain1.conf /etc/nginx/sites-enabled/yourdomain1.conf

restart your NGINX server:

sudo systemctl restart nginx

GREAT!!! you can test your results

Wagiman Wiryosukiro

Petani Sistem Informasi, tukang las plugin & themes Wordpress. Co-Founder SistemInformasi.biz. Saat ini aktif sebagai Developer & kontributor di OpenMandriva Linux.

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: