Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up a subdomain with Apache on Linux

Tags:

I can't believe that I haven't done this before, but I would like a definitive answer so I'm all set going forward.

I have an apache config file at /etc/apache2/sites-available/mysite which looks like this:

<VirtualHost *:80>     ServerAdmin webmaster@localhost      DocumentRoot /home/sam/public_html     <Directory />             Options FollowSymLinks             AllowOverride All     </Directory>     <Directory /home/sam/public_html>             Options Indexes FollowSymLinks MultiViews             AllowOverride All             Order allow,deny             allow from all     </Directory>      ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/     <Directory "/usr/lib/cgi-bin">             Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch             Order allow,deny             Allow from all     </Directory>      ErrorLog ${APACHE_LOG_DIR}/error.log      # Possible values include: debug, info, notice, warn, error, crit,     # alert, emerg.     LogLevel warn      CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> 

So this serves html and php files from ~/public_html all fine. But I have multiple projects there so would like to start using subdomains. What I want to do is serve files from ~/public_html/myproject/ as the root directory for myproject.localhost.

I have tried adding the following to the bottom of my apache file:

<VirtualHost myproject.localhost>     DocumentRoot ~/public_html/myproject/     ServerName myproject.localhost     ServerAdmin [email protected]     <Directory ~/public_html/myproject>             Options Indexes FollowSymLinks             AllowOverride FileInfo             Order allow,deny             Allow from all     </Directory> </VirtualHost> 

but apache complains:

Restarting web server: apache2[Tue Aug 20 11:06:19 2013] [error] (EAI 2)Name or service not known: Could not resolve host name myproject.localhost -- ignoring!  ... waiting [Tue Aug 20 11:06:20 2013] [error] (EAI 2)Name or service not known: Could not resolve host name myproject.localhost -- ignoring! 

I know I'm committing a fundamental error, but I'm not sure what it is.

Edit

This is my complete file now:

<VirtualHost *:80>     DocumentRoot /home/sam/public_html/ryua1226-magento/     ServerName mydomain.localhost     ServerAdmin [email protected]     <Directory /home/sam/public_html/ryua1226-magento>             Options Indexes FollowSymLinks             AllowOverride FileInfo             Order allow,deny             Allow from all     </Directory> </VirtualHost>   <VirtualHost *:80>     ServerAdmin webmaster@localhost      DocumentRoot /home/sam/public_html     <Directory />             Options FollowSymLinks             AllowOverride All     </Directory>     <Directory /home/sam/public_html>             Options Indexes FollowSymLinks MultiViews             AllowOverride All             Order allow,deny             allow from all     </Directory>      ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/     <Directory "/usr/lib/cgi-bin">             Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch             Order allow,deny             Allow from all     </Directory>      ErrorLog ${APACHE_LOG_DIR}/error.log      # Possible values include: debug, info, notice, warn, error, crit,     # alert, emerg.     LogLevel warn      CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>  
like image 235
Darwin Tech Avatar asked Aug 20 '13 18:08

Darwin Tech


People also ask

What is Linux subdomain?

A subdomain must be a subdomain of another domain name on your Linux Hosting account - for example, if you've got "coolexample.com", you can add "pics.coolexample.com". Subdomain website files are stored in a different directory than the main domain website files.

How do I add a subdomain to my DNS server?

Click on the DNS tab and scroll to the record card (or quickly jump to the DNS records for that domain by clicking the button in the top right). From there, click the Add record drop-down button, and select the type of record you want to create. A , ALIAS , and CNAME records will all create a new subdomain.


2 Answers

You are telling Apache what IP and port you want to answer it on inside of the <VirtualHost> tag so here * means any IP, but accept requests for this site on port 80. Next you need to tell Apache where the document root is. ~/ means your default home directory, so if your DocumentRoot just happens to be the default home variable then it would work with your existing notation (depending on which user you're running the server as). Then you would declare the server name.

Each domain name you're create a host for needs its own Virtual Host Directive unless you're using aliases.

<VirtualHost *:80>     DocumentRoot /home/sam/public_html     ServerName myproject.localhost      # Other directives here  </VirtualHost>  <VirtualHost *:80>     DocumentRoot /home/sam/public_html/myproject     ServerName myotherproject.localhost      # Other directives here  </VirtualHost> 

About Hosts In addition to this, any special name that you create for a host needs to go into a hosts file or in the DNS server as well. This way any web browser that is looking for your server can find it without having to type in the IP. Since you'll likely have multiple hosts on the same IP with your setup if you were to try and access the server with the IP only, you would only get the first host to respond on the IP (usually the top in the vhosts list).

like image 153
AbsoluteƵERØ Avatar answered Oct 10 '22 00:10

AbsoluteƵERØ


Adding to AbsoluteƵERØ's answer(if we are trying this for a local machine), we need to update hosts file which can be found here:

Ubuntu: /etc/hosts

Windows: c:\windows\system32\drivers\etc\hosts

Mac: /etc/hosts

Note: This is required to tell your local machine that the given subdomain points to your local machine itself

like image 43
Binod Kalathil Avatar answered Oct 10 '22 00:10

Binod Kalathil