0

I have a Ubuntu from Linode.com. I'm trying to host multi website ( domains, each will listen to different ports ). Here is what I did: In file mysite.com.conf:

Listen *:81
<VirtualHost *:81>
  ServerName mysite.com
  ServerAlias www.mysite.com
</VirtualHost>

In file ports.conf

Listen *81

Then run command in terminal:

sudo a2ensite mysite.com.conf
sudo service apache2 restart

Then I setup DNS Manager for domain mysite.com But when I visit mysite.com from browser, it always listen to port 80. So when I setup multiply Virtual Host file as above, for different domains, All of them just route to port 80. Do you know which step I missed and how to tell Apache to redistribute to different port according to incoming domains name ? I followed tutorial here.

V.Tran
  • 457
  • 1
  • 5
  • 13
  • This is not how port selection works. I doubt you _really_ want to use different ports. You'd have to use URLs like https : //www.mysite.com:81/ to reach such http server. That hardly is desirable. Instead you want to use "name based virtual hosts", where all hosts listen _on the same port number_. Take a look at the documentation of the apache http server. That is the common technology these days to host multiple hostnames / domains. – arkascha Jun 10 '17 at 18:02
  • I read tutorial here on Apache Site: https://httpd.apache.org/docs/2.4/vhosts/examples.html If I have to put the port after domains name then I can achieve that without doing anything with Apache. – V.Tran Jun 10 '17 at 18:14
  • What do you mean by "then I can achieve that without doing anything with Apache"? You certainly _do_ use the apache http server and you certainly _do_ have to configure it - regardless of whether you use name or port based virtual hosts. – arkascha Jun 10 '17 at 18:20
  • You don't have to install Apache and don't have to configure anything If You put the port after domain name like: mysite.com:8080 or mysite.com:9090. You don't need Apache for this to work. If you open the port 8080 by ufw on Ubuntu then mysite.com:8080 works without needing Apache. – V.Tran Jun 10 '17 at 18:26
  • Um, sorry, no. You definitely do need an http server. How else do you want to serve http?You certainly can use another http server software, it does not have to be the apache implementation. But you need _some_. – arkascha Jun 10 '17 at 18:26
  • If you build a java web app, pack in .jar file. Then upload on your Ubuntu server. In terminal, run: java -jar app.jar Then you can put port after the domain name you set for you app.jar, let say 8080. After that, visit the site by: mysite.com:8080. No Apache or any other Ngnix or any server needed. – V.Tran Jun 10 '17 at 18:33
  • Of course, then you implemented your own http server, that's all. Which you obviously also can do when using port 80, so without the need to specify a port number in the URL. I don't see what that has to do with virtual hosting and why you tagged this question with `apache` then... – arkascha Jun 10 '17 at 18:35
  • Back to your original question and my first comment: I explained that _if_ you use _port based virtual hosts_ (as you do in your example), then you _do_ have to specify a port number in the URL. How else should the browser know which port to connect to? If you use _name based virtual hosts_, then multiple domains / hostnames can be served using a single port number (so probably the standard http port 80), so that you do _not_ have to specify that port in the URL. – arkascha Jun 10 '17 at 18:56
  • As questioner, I'm expecting guidance, suggestion. You can realize the purpose of this question is: How to distribute request based on incoming domains so that one Ubuntu server can support multiple websites. Putting port after domain name is something for dev, not for users. If you know any software solving this problem ( for sure there is ) then please suggest. I'm trying Docker now. Hope it works – V.Tran Jun 10 '17 at 18:58
  • I actually explained _twice_ to you how to achieve what you ask. – arkascha Jun 10 '17 at 18:59
  • Again I suggest that you take a look into the documentation of the apache http server. It is of great quality and comes with good examples. Here is the section about "name based virtual hosting" which is what you want to use: https://httpd.apache.org/docs/2.4/vhosts/name-based.html – arkascha Jun 10 '17 at 19:02

1 Answers1

0

Tran

I have this in my configuration and hope it will help you.

But first of all you will need to disable the 000-default.conf by doing this

sudo a2dissite 000-default.conf

Then after add the ports you want apache to listen to for example my apache listens to the following ports 80,8080,1000 and port 8000 in my configuration file ports.conf

# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf

Listen 80

<IfModule ssl_module>
        Listen 443
</IfModule>

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

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Then your mysite.com.conf should look something like this

<VirtualHost *:80>

        ServerName mysite.com
        ServerAlias www.mysite.com

        ServerAdmin admin@mysite.com
        DocumentRoot /path/to/mysite.com

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Aftre doing all that work, write the following commands

sudo a2ensite mysite.com.conf
sudo service apache2 restart

Then try accessing your site like this

mysite.com

Hope this helps. Thanks