How do I setup sub-domain for apache?

6,750

Solution 1

Please verify first that there a DNS record that point to your vhost. After add the following to the httpd.conf at the last line


Include /etc/httpd/conf/site-enabled/
Create the directory site-enabled that will contain our vhost config files

sudo mkdir /etc/httpd/conf/site-enabled/

Create the file /etc/httpd/conf/sites-enabled/mysite_company_com.conf


<VirtualHost mysite.company.com>
   ServerName mysite.company.com
   ServerAdmin [email protected]
   DocumentRoot /var/www
   ErrorLog /var/log/apache2/mysite.company.com-error.log
   CustomLog /var/log/apache2/mysite.company.com-access.log common

</VirtualHost>

and add the line above to /etc/httpd/conf/sites-enabled/dev_mysite_company_com.conf:

<VirtualHost dev.mysite.company.com>
   ServerName mysite.company.com
   ServerAdmin [email protected]
   DocumentRoot /var/wwwdev
   ErrorLog /var/log/apache2/dev.mysite.company.com-error.log
   CustomLog /var/log/apache2/dev.mysite.company.com-access.log common
</VirtualHost>

At last restart apache.

Solution 2

You will definitely need dev.mysite.company.com to have an A record setup in DNS to get this configuration working. If it doesn't resolve, your browser has no way to know what IP to request the website from.

Solution 3

Yes, the sub-domain will need a valid A or CNAME record in DNS.

Share:
6,750
Hai Vu
Author by

Hai Vu

Thrived on Python, love Sublime Text and vim, at home with Linux.

Updated on September 17, 2022

Comments

  • Hai Vu
    Hai Vu over 1 year

    I have searched this site and could not find the answer for my simple problem. I setup apache2, php 5, and mysql 5.1 on Red Hat Enterprise Linux 4 (x86_64). Currently, I set it up so that http://mysite.company.com points to our production version of our site. What I want to do is setup http://dev.mysite.company.com to point to our development environment. I tried as such:

    $ cat /usr/local/apache2/conf/http.conf
    ...
    NameVirtualHost *:80
    
    <VirtualHost *:80>
        ServerName mysite.company.com
        DocumentRoot "/var/www"
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerName dev.mysite.company.com
        DocumentRoot "/var/wwwdev"
    </VirtualHost>
    ...
    

    Now, http://mysite.company.com works, but http://dev.mysite.company.com is not found. Should I talk to the IT people to add dev.mysite.company.com to their DNS look up table? What did I do wrong here? You help is appreciated. Thanks.

  • ISJ
    ISJ over 14 years
    you can get around this by using /etc/hosts OR doing manual requests. Apache just looks at the headers, so it'll work if you do something like 'telnet $host 80' and then 'GET dev.mysite.company.com'. this of course, sucks for production.