0

My friends and I have made a project which is made out of two parts. One is made in Django and other one is made in php language. We have just one server so we want to use 2 subdomains. My question is how is this possible? If we install apache and make our subdomains, How we can make Django and php be ran on these subdomains?

thanks.

1

1 Answer 1

2

I assume and hope you mean "Linux server" by stating you have a server, thus, I am providing you with information about Linux.

You can host both Django and PHP on the same server using subdomains with Apache by simply using two separate virtual hosts for the applications. Here's a rough guide on how to achieve this:

Setup Subdomains: Create the two subdomains in your DNS zone or use hosts file on the server. For example:

djangoapp.yourdomain.com
phpapp.yourdomain.com

Install Required Software:

Make sure apache2 is installed. For PHP, install mod_php or php-fpm based on your PHP version. For Django, you'll probably use mod_wsgi or mod_wsgi-express. Configure Apache for PHP:

Create or edit the VirtualHost for the PHP app:

<VirtualHost *:80>
    ServerName phpapp.yourdomain.com
    DocumentRoot /path/to/your/php/app

    <Directory /path/to/your/php/app>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Make sure to change /path/to/your/php/app to the actual path of your PHP project.

Configure Apache for Django:

For Django, you'll typically use mod_wsgi. Here's a basic configuration:

<VirtualHost *:80>
    ServerName djangoapp.yourdomain.com

    WSGIDaemonProcess yourappname python-path=/path/to/your/django/app:/path/to/your/venv/lib/pythonX.X/site-packages
    WSGIProcessGroup yourappname
    WSGIScriptAlias / /path/to/your/django/app/appname/wsgi.py

    <Directory /path/to/your/django/app/appname>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
</VirtualHost>

Notes:

Make sure to replace /path/to/your/django/app with the actual path to your Django project. Change /path/to/your/venv to the path of your virtual environment if you're using one. The pythonX.X should be replaced with your python version, e.g., python3.8. Enable Configuration and Restart Apache:

Make sure your configuration files are linked to the sites-enabled directory, then restart Apache:

sudo a2ensite your_django_config.conf

sudo systemctl restart apache2

Ensure Required Permissions:

Ensure that Apache has the necessary permissions to read (and possibly write, if needed) to the directories of both your Django and PHP projects.

Django Specifics:

Ensure ALLOWED_HOSTS in settings.py includes your subdomain. Make sure Django's STATIC_URL and MEDIA_URL are properly set up if you're serving static or media files. Consider using whitenoise for static files in simpler setups or a dedicated solution like an external CDN or a service like AWS S3 for larger projects. Test: After setting up, test both subdomains in the browser to ensure they're working as expected.

With these configurations, your Django app will be accessible from djangoapp.yourdomain.com and your PHP app from phpapp.yourdomain.com.

I am of course missing some other steps, but this is just really high-level of overview on how you can do it. There are probably million other ways to achieve it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .