Make apache site hosted in a local computer accessible from outside

I have a working apache2 server hosting a Moodle site on LAN which can be accessed thru http://192.x.x.x/moodle. I also have a Linux server sitting between the Internet and the LAN with a live IP address 124.x.x.x. I want to make the moodle site accessible from the Internet. Is it possible?

The local apache2 server has a working configuration to serve moodle.domain.com. The live Linux server also has an apache2 server hosting domain.com. Both are running Debian Linux

I tried to redirect moodle.domain.com to 192.x.x.x from the live Linux server's /etc/hosts file but still, the domain.com is what I get when I access moodle.domain.com from the outside.

When I access from outside, I configure first the host file in a windows laptop to redirect moodle.domain.com to the live IP address of the Linux server.

What am I missing here?


Solution 1:

I got it working by implementing Apache reverse proxy in the live Linux Apache server.

For those who are looking for the same solution, here's a snippet of my /etc/apache2/sites-enabled/000-default.conf:

<VirtualHost *:80>
        ServerName moodle.domain.com
        ProxyPreserveHost On
        ProxyRequests Off
        ProxyPass / http://10.5.1.100:8080/
        ProxyPassReverse / http://10.5.1.100:8080/
</VirtualHost>

The local Apache server hosting the actual Moodle site should be listening on port 8080:

<VirtualHost *:8080>
    ServerAdmin webmaster@local
    DocumentRoot /srv/www/moodle
    ServerName moodle.domain.com
    ServerAlias moodle.domain.com
    <Directory /srv/www/moodle>
           #Allowoverride all    ###Uncomment if required
    </Directory>
</VirtualHost>

Another Moodle-specific setting I needed to tweak is the Moodle config file /srv/www/moodle/config.php:

$CFG->wwwroot = 'http://moodle.domain.com';

Moodle initially set it to $CFG->wwwroot = 'http://10.5.1.100/moodle';during installation. If you have no Moodle installation, then ignore this part

Solution 2:

Before we start I want to make sure that you understand that it's not the safest option to do this way. Specially by opening router ports, so please make sure that you understand about firewall.

Since you have a dynamic IP (I'm presuming) you cannot simply bind it to your domain. This is because your IP will end up changing every couple hours.

This answer is based in most common router options available.

  1. You must find your apache server in the router connections, and activate port forwarding (80) or, if you understand what you're doing and have a proper firewall initialized, you can simply open all router ports (I highly recommend you not doing this unless you have a firewall)
  2. Use a free DNS service like Dyn DNS (most recent routers support this out of the box, just check the settings and setup an account).
  3. Create a free DNS subdomain.
  4. Create a DNS Rule (CNAME) from your main domain. and point it to your dyndns created subdomain. For example moodle.example.com -> trex-moodle.exampledyndns.com.
  5. Configure your apache and add your dyndns subdomain.

Using a free dns in your router is the best option, since your router rotates the IP every couple hours, if you're using such a service, your dyn dns domain will be always pointing to your home IP address.

Edit

Regarding the first comment, you'll need a nginx proxy running in your main linux server. The nginx can be used to host the website domain.com and moodle.domain.com which is hosted in the different server in the same network.

Make sure that you can access the moodle website from your linux server. (Use ping IP)

Now in your nginx you'll need to have the following configuration:

server {
    listen       80;
    server_name domain.com;

        root /your/domain.com/path/goes/here ;
}

server {
    listen       80;
    server_name  moodle.trex.com;

    location / {
        proxy_pass http://192.X.X.X:80; #Your apache2 server local IP address
    }
}

For this to work, both Linux servers and your apache2 server must be under the same 192.X.X.X network.