50

Spent hours going in circles following every guide I can find on the net.

I want to have two sites running on a single apache instance, something like this - 192.168.2.8/site1 and 192.168.2.8/site2

I’ve been going round in circles, but at the moment I have two conf files in ‘sites-available (symlinked to sites-enabled)’ that look like this-

<VirtualHost *:2000>

ServerAdmin webmaster@site1.com
ServerName site1
ServerAlias site1

# Indexes + Directory Root.
DirectoryIndex index.html
DocumentRoot /home/user/site1/

# CGI Directory
ScriptAlias /cgi-bin/ /home/user/site1/cgi-bin/

Options +ExecCGI

# Logfiles
ErrorLog /home/user/site1/logs/error.log
CustomLog /home/user/site1/logs/access.log combined

</VirtualHost>

and

<VirtualHost *:3000>

ServerAdmin webmaster@site2.com
ServerName site2
ServerAlias site2

# Indexes + Directory Root.
DirectoryIndex index.html
DocumentRoot /home/user/site2/

# CGI Directory
ScriptAlias /cgi-bin/ /home/user/site2/cgi-bin/

Options +ExecCGI

# Logfiles
ErrorLog /home/user/site2/logs/error.log
CustomLog /home/user/site2/logs/access.log combined

</VirtualHost>

http.conf looks like this-

NameVirtualHost *:2000
NameVirtualHost *:3000

At the moment I’m getting this error-

[error] VirtualHost *:80 — mixing * ports and non-* ports with a NameVirtualHostaddress is not supported, proceeding with undefined results

Ports.conf looks like this – (although no guides have mentioned any need to edit this)

NameVirtualHost *:80

Listen 80
<IfModule mod_ssl.c>
# If you add NameVirtualHost *:443 here, you will also have to change
# the VirtualHost statement in /etc/apache2/sites-available/default-ssl
# to <VirtualHost *:443>
# Server Name Indication for SSL named virtual hosts is currently not
# supported by MSIE on Windows XP.
Listen 443
</IfModule>

<IfModule mod_gnutls.c>
Listen 443
</IfModule>

Can anyone give some simple instructions to get this running? Every guide I’ve found says to do it a different way, and each one leads to different errors. I'm obviously doing something wrong but have found no clear explanation of what that might be.

Just want one site accessible on port 2000 and the other accessible on port 3000 (or whatever, just picked those ports to test with).

I’m running Ubuntu server 12.04…

=============

EDIT

Followed another 'guide'...

I've now set this up in sites-available:

<VirtualHost *:80>
    DocumentRoot "/home/user/site1/"
    ServerName 192.168.2.10/site1
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/home/user/site2/"
    ServerName 192.168.2.10/site2
</VirtualHost>

Have set this in apache2.conf:

ServerName site1
ServerName site2

Have added this to ports.conf:

Listen 192.168.2.10:80

==============

EDIT

It now works, I put this in a conf file in site-enabled:

<VirtualHost *:81>
    DocumentRoot "/home/user/site1/"
    ServerName site1
</VirtualHost>

<VirtualHost *:82>
    DocumentRoot "/home/user/site2/"
    ServerName site2
</VirtualHost>

I have this in ports.conf:

Listen *:80
Listen *:81
Listen *:82

I have this in apache2.conf:

ServerName site1
ServerName site2

I didn't find this in any guides I just got it working through an entire day of trial and error so I don't know if this is a good solution. But it's at least working how I want it to now.

Exbi
  • 950
  • 4
  • 12
  • 19

2 Answers2

73

Your question is mixing a few different concepts. You started out saying you wanted to run sites on the same server using the same domain, but in different folders. That doesn't require any special setup. Once you get the single domain running, you just create folders under that docroot.

Based on the rest of your question, what you really want to do is run various sites on the same server with their own domain names.

The best documentation you'll find on the topic is the virtual host documentation in the apache manual.

There are two types of virtual hosts: name-based and IP-based. Name-based allows you to use a single IP address, while IP-based requires a different IP for each site. Based on your description above, you want to use name-based virtual hosts.

The initial error you were getting was due to the fact that you were using different ports than the NameVirtualHost line. If you really want to have sites served from ports other than 80, you'll need to have a NameVirtualHost entry for each port.

Assuming you're starting from scratch, this is much simpler than it may seem.

If you are using 2.3 or earlier, the first thing you need to do is tell Apache that you're going to use name-based virtual hosts.

NameVirtualHost *:80

If you are using 2.4 or later do not add a NameVirtualHost line. Version 2.4 of Apache deprecated the NameVirtualHost directive, and it will be removed in a future version.

Now your vhost definitions:

<VirtualHost *:80>
    DocumentRoot "/home/user/site1/"
    ServerName site1
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/home/user/site2/"
    ServerName site2
</VirtualHost>

You can run as many sites as you want on the same port. The ServerName being different is enough to tell Apache which vhost to use. Also, the ServerName directive is always the domain/hostname and should never include a path.

If you decide to run sites on a port other than 80, you'll always have to include the port number in the URL when accessing the site. So instead of going to http://example.com you would have to go to http://example.com:81

bradym
  • 4,880
  • 1
  • 31
  • 36
  • can you please elaborate the case for the port other than 80. I'm really getting confused in implementing this. :) – user79307 Nov 18 '13 at 09:35
  • 1
    @accssharma By default apache (and all other web server software) listens for connections on port 80. Because of this, browsers are coded to connect to port 80 when requesting websites. If you run a website on a port other than 80, your users will always have to include the port number when requesting the site. I strongly recommend against this. – bradym Nov 25 '13 at 23:41
  • @accssharma If you do choose to run a site on a port other than 80, you'll need a NameVirtualHost line specifying the port, then VirtualHost containers specifying that same port. Basically, copy what I've got above and replace 80 with the port where you intend to run the site. – bradym Nov 25 '13 at 23:42
  • @bradym Hi, Just curious to add a configuration here is possible. If I make nginx as my proxy server and then route it to apache2 for displaying different domains requested, in that case, can I run two python(wsgi) applications from same virtual host or I need to run different apache on different ports ? – 89n3ur0n Sep 20 '15 at 08:12
  • If you have this setup, how do you tell apache which site site that should be reached from 127.0.0.1 so you can reach that site from local network? – embe Jan 20 '17 at 07:26
  • @89n3ur0n Sorry, just saw your note. If you haven't solved that yet I suggest asking a separate question with more details as I can't tell from this brief question how to help. – bradym Feb 23 '17 at 05:36
  • @embe - Add an entry to your hosts file where you point a domain at 127.0.0.1 Then you can view it locally. – bradym Feb 23 '17 at 05:37
  • This very clear explanation is probably outdated, I have a NameVirtualHost in my config, and apache tells me upon restarting: AH00548: NameVirtualHost has no effect and will be removed in the next release. So, is this option now unnecessary? Apache/2.4.29 "ubuntu" version – piotao Apr 02 '19 at 21:20
  • 1
    @piotao Sorry for the late response. According to https://httpd.apache.org/docs/current/new_features_2_4.html NameVirtualHost is no longer required. – bradym Nov 19 '19 at 04:39
6

Yes with Virtual Host you can have as many parallel programs as you want:

Open

/etc/httpd/conf/httpd.conf

Listen 81
Listen 82
Listen 83

<VirtualHost *:81>
    ServerAdmin webmaster@site1.com
    DocumentRoot /var/www/site1/html
    ServerName site1.com
    ErrorLog logs/site1-error_log
    CustomLog logs/site1-access_log common
    ScriptAlias /cgi-bin/ "/var/www/site1/cgi-bin/"
</VirtualHost>

<VirtualHost *:82>
    ServerAdmin webmaster@site2.com
    DocumentRoot /var/www/site2/html
    ServerName site2.com
    ErrorLog logs/site2-error_log
    CustomLog logs/site2-access_log common
    ScriptAlias /cgi-bin/ "/var/www/site2/cgi-bin/"
</VirtualHost>

<VirtualHost *:83>
    ServerAdmin webmaster@site3.com
    DocumentRoot /var/www/site3/html
    ServerName site3.com
    ErrorLog logs/site3-error_log
    CustomLog logs/site3-access_log common
    ScriptAlias /cgi-bin/ "/var/www/site3/cgi-bin/"
</VirtualHost>

Restart apache

service httpd restart

You can now refer Site1 :

http://<ip-address>:81/ 
http://<ip-address>:81/cgi-bin/

Site2 :

http://<ip-address>:82/
http://<ip-address>:82/cgi-bin/

Site3 :

http://<ip-address>:83/ 
http://<ip-address>:83/cgi-bin/

If path is not hardcoded in any script then your websites should work seamlessly.

Srihari Karanth
  • 2,067
  • 2
  • 24
  • 34
  • This should still work if you use the same port (80 for example) vor every virtualhost entry right? Apache can tell which application to direct to based on the servername; or am I wrong? – DFSFOT Mar 09 '22 at 20:47