How to create a Linux subdomain and use a public_html subdirectory

I have managed

to create umpteen subdomains on a Ubuntu 21.04 VPS and they all work as expected. Also each have separate SSL Certificates courtesy of LetsEncrypt.org

The Problem:

I use CodeIgniter4 PHP Framework and unfortunately it requires the subdomain to call the following file:

https://mysubdomain.example.com/publi_html/index.php

Unfortunately the subdomain has the following set:

incorrect:

$_SERVER["DOCUMENT_ROOT"] = "/var/www/mydomain.com/mySubdomain/";

required:

$_SERVER["DOCUMENT_ROOT"] = "/var/www/mydomain.com/mySubdomain/public_html/";

File: /etc/apache2/sites_available/mySubdomain.conf

<VirtualHost *:80>
    ServerName  mysubDomain.example.com
    DocumentRoot /var/www/example.com/mysubdomainl
    # VirtualDocumentRoot /var/www/example.com/mysubdoman
    <Directory /var/www/example.com/mysubdomain>
      Options +Indexes +FollowSymlinks +MultiViews
      AllowOverride all
      Require all granted
    </Directory>
    ErrorLog  ${APACHE_LOG_DIR}/error-example-mysubdoain.log
    # CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Requirements:

I would like to browse to https://mysubdomain.example.com and to call the follwoing file:

/var/www/example.com/mysubdomain/public_html/index.php

This follwoing file is beings called:

/var/www/example.com/mysubdomain/index.php

Unsuccesful Trials and Errors

  1. Adding /public_html to /etc/apache2/sites_available/mysubdomain.conf path
  2. Adding Virtual DocumentRoot
  3. using ***var/www/example.com/mysubdomain/index.php to chmod to the public_html directory
  4. using ***var/www/example.com/mysubdomain/index.php to "Heading(‘Location: public_html/index.php’);
  5. Read but did not understand many posts and the documentation.

Any ideas?

Are you trying to make a codebase that will automatically create and set up subdomains, or are you manually setting them up?

Looking at your virtual host file, you are setting up a wildcard, or was that an attempt to get all subdomains pointing to the main domain codebase?

Edit:
Could you also let us know the file structure of your main domain?
And how you would like the subdomains set up according to that.
(i.e. why do you need public_html in the path).

Assuming you want them to run the same codebase, one way to do it is inside the index.php file for the subdomain, is to set an indicator (if needed) on who is running, and then include the main code base index file.

For this to work, the subdomain needs to have access to the main code base. Depending on the server setup, this might be restricted by the user running apache, or PHP settings. If, you would either need to give the users access to the different codebase, or soften the settings.

Another method, would be as you suggested to change the subdomain to point to the main codebase. This should be doable from the virtual host files if you run apache, just make sure you update the correct ones. Though if you are running WHM or any similar management software packages, those might override changes you do. (I have only done similar updates to subdomains on nginx in the past.)

Regret the delay in replying but I had a bug.

Simple Subdomains Isolated:

Today I’ve investigated further and decided to isolate the simple subdomains and they are all working as expected.

The modern trend with most PHP Frameworks, WordPress, etc is to have a publicly available index.php which includes other files Above-the-root in an endeavour to have the included files unreachable by Joe Public.

Latest Trials:

I now have another three domains using CodeIgniter4 and they each require a public_html sub-directory.

File: /etc/apache2/sitesavailable/example-withsub-domans.conf

<VirtualHost *:80>
    ServerAdmin admin@EXAMPLE.com
    ServerName  EXAMPLE.com
    ServerAlias www.EXAMPLE.com
    DocumentRoot "/var/www/EXAMPLE.com/public_html"
    <Directory /var/www/EXAMPLE.com/public_html>
      Options +Indexes +FollowSymlinks +MultiViews
      AllowOverride all
      Require all granted
    </Directory>
    LogLevel warn
    ErrorLog  ${APACHE_LOG_DIR}/error-EXAMPLE.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:80>
    ServerName    ci4.EXAMPLE.com
    DocumentRoot /var/www/EXAMPLE.com/ci4
    VirtualDocumentRoot /var/www/EXAMPLE.com/ci4/public_html
    <Directory /var/www/EXAMPLE.com/ci4>
      Options +Indexes  +MultiViews
      # +FollowSymlinks 
      AllowOverride all
      Require all granted
    </Directory>
    ErrorLog  ${APACHE_LOG_DIR}/error-example-ci4.log
    # CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:80>
    ServerName  ci4-strict.EXAMPLE.com
    DocumentRoot "/var/www/EXAMPLE.com/ci4-strict"
    VirtualDocumentRoot /var/www/EXAMPLE.com/ci4-strict/public_html
    <Directory /var/www/EXAMPLE.com/ci4-strict>
      Options +Indexes  +MultiViews
      # +FollowSymlinks
      AllowOverride all
      Require all granted
  </Directory>
    ErrorLog  ${APACHE_LOG_DIR}/error-example-ci4-strict.log
    # CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Installation of above conf file:

The following Apache2 Command was used to register the Domains:

a2ensite example-withsub-domans.conf

and LetsEncrypt.org created the three domains without errors.

Results:

https://example.com and https://www.example.com both render as expected

https://ci4.example.com and ci4-strict.example.com both fail to call public_html/index.php

I would be grateful ti know how I can call the subdomain *public_html/index.php file.

As a last resort I am tempted to ignore the public_html/index.php file and to move the Above-the-root included files to the immediate level below the subdomains :slight_smile:

Please ignore the previous post because both subdomains have started working :slight_smile:

It is a good approach to store all library files below the public path (i.e. all files that are not image, CSS, javascript, etc and entry files).

Though if you run subdomains on WHM for example, where Apache like to place the subdomains like this /home/website/public_html/subdomain, the best approach is to go below the current public_html folder, making the root path for the subdomain files /home/website/subdomain/

I have never run Apache as standalone, so not sure if it does the same when run as that.

However from the example conf files you provided, I would verify that you cannot access the main subdomain folder from the main website.

I.e. www.example.com/ci4/Inc/Config.env etc.

Not sure if this is still possible, but it used to be possible the last time I ran WHM/Apache years ago.

I used to use WHM a long time ago when I was given access to the company’s VPS but now only use an Ubuntu 21.04 VPS at $6.99 per month for 50 GB…

Now with Fast NVMe
4 vCPU Cores
8 GB RAM
50 GB NVMeor 200 GB SSD
1 Snapshot
32 TB Traffic

WHM is an additional cost and I think offers nothing more than can be programmed using Apache2.

The directory structure:

/var/www/EXAMPLE.com/
  ci4/
  ci4-strict/
  jack/
  fred/
  public_html/
  system/

[quote]@TheRedDevil,
However from the example conf files you provided, I would verify that you cannot access the main subdomain folder from the main website.

I.e. www.example.com/ci4/Inc/Config.env etc.
[/quote]

None of the subdomain are accessible from /var/www/EXAMPLE.com/public_html/ unless there is a link from index.php to the specific subdomain.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.