{"id":965,"date":"2017-01-02T12:43:18","date_gmt":"2017-01-02T07:13:18","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=965"},"modified":"2017-03-21T10:56:25","modified_gmt":"2017-03-21T05:26:25","slug":"configure-multiple-domainsvirtualhost-nginx-webserver","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/configure-multiple-domainsvirtualhost-nginx-webserver\/","title":{"rendered":"Configure multiple domains(virtualhost) on nginx webserver"},"content":{"rendered":"<p>This post assumes that nginx already installed on the server. If its not installed, please refer the following post to <a href=\"https:\/\/pheonixsolutions.com\/blog\/install-nginx-php-mariadb-ubuntu-16-04-1\/\">install nginx<\/a>. We assumes that host is ubuntu and installation path is \/etc\/nginx. You can follow the same post incase of different OS or installation path is different. You may need to alter a bit on the implementation plan.<\/p>\n<h5><strong><em>Assumption:<\/em><\/strong><\/h5>\n<ol>\n<li>\u00a0Ubuntu Host<\/li>\n<li>LEMP setup.<\/li>\n<li>Domain Names(2 or more domains)<\/li>\n<\/ol>\n<h5>Implementation:<\/h5>\n<p>Let&#8217;s create the required directories first for both the domains<\/p>\n<p><code>mkdir -p \/var\/www\/domain1.tld\/html<\/code><\/p>\n<p><code>mkdir -p \/var\/www\/domain2.tld\/html<\/code><\/p>\n<p>Change the ownership of the directory. Find out which user nginx service is running on the host.<\/p>\n<p><code># grep user \/etc\/nginx\/nginx.conf<br \/>\nuser <\/code><span style=\"color: #ff0000;\">www-data<\/span><code>;<br \/>\n<\/code><\/p>\n<p>We may need to modify below command based on output previous output.<\/p>\n<p><code>chown www-data.\u00a0\/var\/www\/domain1.tld\/html<\/code><\/p>\n<p><code>chown www-data.\u00a0\/var\/www\/domain2.tld\/html<\/code><\/p>\n<p>Create virtualhost configuration for both the domains.<\/p>\n<p><code><\/code><code>cd \/etc\/nginx\/sites-available\/<\/code><\/p>\n<p><code>cp default domain1.tld<\/code><\/p>\n<p><code>cp default domain2.tld<\/code><\/p>\n<p>Modify each domain virtual host(server blocks) as per our domain. we need to update each domain configuration as per domain name. The following is the configuration of\u00a0<strong>domain1.tld<\/strong><\/p>\n<p><code>vi\u00a0domain1.tld<\/code><\/p>\n<blockquote><p>server {<\/p>\n<p>listen 80;<br \/>\nlisten [::]:80;<\/p>\n<p>root \/var\/www\/<span style=\"color: #ff0000;\"><strong>domain1.tld<\/strong><\/span>\/html; <strong># Modify the documentroot for each domain<\/strong><br \/>\nindex index.html index.php index.htm index.nginx-debian.html;<\/p>\n<p>server_name <strong><span style=\"color: #ff0000;\">domain1.tld<\/span>; # Modify the domain name for each domain<\/strong><\/p>\n<p>location \/ {<br \/>\ntry_files $uri $uri\/ \/index.php?$args;<br \/>\n}<br \/>\nlocation ~ \\.php$ {<br \/>\ninclude snippets\/fastcgi-php.conf;<br \/>\nfastcgi_pass unix:\/run\/php\/php7.0-fpm.sock;<br \/>\n}<\/p>\n<p>}<\/p><\/blockquote>\n<p>Similarly, the following configuration is for\u00a0<strong>domain2.tld.<\/strong><\/p>\n<p><code>vi domain2.tld<\/code><\/p>\n<blockquote><p>server {<\/p>\n<p>listen 80;<br \/>\nlisten [::]:80;<\/p>\n<p>root \/var\/www\/<span style=\"color: #ff0000;\"><strong>domain2.tld<\/strong><\/span>\/html; <strong># Modify the documentroot for each domain<\/strong><br \/>\nindex index.html index.php index.htm index.nginx-debian.html;<\/p>\n<p>server_name <strong><span style=\"color: #ff0000;\">domain2.tld<\/span>; # Modify the domain name for each domain<\/strong><\/p>\n<p>location \/ {<br \/>\ntry_files $uri $uri\/ \/index.php?$args;<br \/>\n}<br \/>\nlocation ~ \\.php$ {<br \/>\ninclude snippets\/fastcgi-php.conf;<br \/>\nfastcgi_pass unix:\/run\/php\/php7.0-fpm.sock;<br \/>\n}<\/p>\n<p>}<\/p><\/blockquote>\n<p>Create symlink configuration for domain1.tld and domain2.tld.<\/p>\n<p><code>ln -s \/etc\/nginx\/sites-available\/<\/code><span class=\"highlight\">domain1.tld<\/span><code>\u00a0\/etc\/nginx\/sites-enabled\/<\/code><\/p>\n<p><code>ln -s \/etc\/nginx\/sites-available\/<\/code><span class=\"highlight\">domain2.tld<\/span><code>\u00a0\/etc\/nginx\/sites-enabled\/<\/code><\/p>\n<p>Check for any nginx configuration error.<\/p>\n<p><code>nginx -t<\/code><\/p>\n<p>Once, we confirm that there is no error then we can restart nginx service.<\/p>\n<p><code>systemctl restart nginx<\/code><\/p>\n<p>Create\/upload files to domain documentroot( <span style=\"color: #000000;\">\/var\/www\/domain1.tld\/html &amp;\u00a0 \/var\/www\/domain2.tld\/html).<\/span><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post assumes that nginx already installed on the server. If its not installed, please refer the following post to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[271,159],"class_list":["post-965","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-nginx","tag-webserver","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-fz","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/965","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=965"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/965\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=965"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=965"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=965"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}