{"id":1698,"date":"2018-10-26T07:46:23","date_gmt":"2018-10-26T07:46:23","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=1698"},"modified":"2018-10-26T08:35:51","modified_gmt":"2018-10-26T08:35:51","slug":"setup-nginx-virtual-hosts-on-centos","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/26\/setup-nginx-virtual-hosts-on-centos\/","title":{"rendered":"Setup Nginx Virtual Hosts On CentOS"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxadmin.io\/wp-content\/uploads\/2017\/05\/nginx-virtual-host.png\" alt=\"\" width=\"621\" height=\"257\" \/><\/p>\n<h3>Introduction<\/h3>\n<p>Nginx is a high performance web server. ANginx virtual host can be setup to host as many domains as your server can handle. If you have not already please see our guides on <a href=\"https:\/\/linuxadmin.io\/nginx-compile-source\/\">Nginx Compile From Source<\/a> as well as <a href=\"https:\/\/linuxadmin.io\/php-fpm-php7-source\/\">PHP-FPM With PHP7 From Source<\/a>. These 2 guides will lay the foundation of core services for Nginx to function as a web server. Once both of those have been completed or you already have a functioning nginx server you can continue with adding virtual hosts.<\/p>\n<h3>Nginx Virtual Host Preparation<\/h3>\n<p>First create a new directory to house the domains content<\/p>\n<p>mkdir -p \/etc\/nginx\/domain.com\/public_html<\/p>\n<p>Replacing domain.com with the domain you are intending to host. Make sure the directory is executable by all users<\/p>\n<p>chmod 755 \/etc\/nginx\/domain.com\/public_html<\/p>\n<p>Go ahead and create a php file to make sure Nginx is working with PHP-FPM<\/p>\n<p>edit \/etc\/nginx\/domain.com\/public_html\/index.php<\/p>\n<p>vim \/etc\/nginx\/domain.com\/public_html\/index.php<\/p>\n<p>and insert the following php code<\/p>\n<p>&lt;?PHP<br \/>\nphpinfo();<br \/>\n?&gt;<\/p>\n<h3>Nginx Virtual Host Configuration<\/h3>\n<p>Go ahead and create the site configuration file<\/p>\n<p>vim \/etc\/nginx\/domain_com.conf<\/p>\n<p>and insert the following:<\/p>\n<p>server {<br \/>\nlisten 80;<br \/>\nserver_name domain.com;<br \/>\nerror_log logs\/domain_com.error.log;<br \/>\naccess_log logs\/domain_com.access.log;<br \/>\nroot \/etc\/nginx\/domain.com\/public_html;<br \/>\nindex index.php;<\/p>\n<p>location ~ .php$ {<br \/>\ntry_files $uri =404;<br \/>\nfastcgi_pass 127.0.0.1:9000;<br \/>\nfastcgi_index index.php;<br \/>\nfastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;<br \/>\ninclude fastcgi_params;<br \/>\n}<\/p>\n<p>}<\/p>\n<p>Replacing all instances of domain.com with the domain you are wanting to host. After you have saved that file you will want to edit the main nginx.conf to include the virtual configuration<\/p>\n<p>vim \/etc\/nginx\/nginx.conf<\/p>\n<p>and add the following at the end<\/p>\n<p>include \/etc\/nginx\/domain_com.conf<\/p>\n<p>After you have done that you will want to update DNS to point to a IP address Nginx is listening on your server.<\/p>\n<p>You can go ahead and restart nginx to load the new configuration file.<\/p>\n<p>\/etc\/init.d\/nginx restart<\/p>\n<p>After that is all done you should be able to visit your site and it should load a PHP informational page that will display all of your configuration. If you would also like to setup your site in SSL, you can use Lets Encrypt to do this for free. The instructions on how to do that are <a href=\"https:\/\/linuxadmin.io\/letsencrypt-ssl-nginx\/\">Hwo To Setup Nginx To Use SSL With LetsEncrypt<\/a><\/p>\n<p><a href=\"https:\/\/linuxadmin.io\/nginx-virtual-host-centos\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Nginx is a high performance web server. ANginx virtual host can be setup to host as many domains as your server can handle. If you have not already please see our guides on Nginx Compile From Source as well as PHP-FPM With PHP7 From Source. These 2 guides will lay the foundation of core &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/26\/setup-nginx-virtual-hosts-on-centos\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Setup Nginx Virtual Hosts On CentOS&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1698","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1698","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=1698"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1698\/revisions"}],"predecessor-version":[{"id":1746,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1698\/revisions\/1746"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=1698"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=1698"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=1698"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}