How to set up Wordpress 1-click with Django backend on same url ?

February 10, 2017 879 views
Django WordPress Apache Ubuntu 16.04

Hello everyone,

I have set up a Wordpress 1-click app on a Ubuntu droplet (16.04).
I have installed a simple Django project (works on http://localhost:8000) and configured it with VirtualHost to reach it a http://localhost/backend.
It does not work : on this url I got a 404 error page from Wordpress.

I would like to know if it possible first ? (from that post it seems possible https://www.digitalocean.com/community/questions/django-and-wordpress-on-wp-one-click-installation-ubuntu, but is it on the same url ?)

Here are the steps I have already done:

You can find here my django virtual host config file.

<VirtualHost *:80>

        WSGIDaemonProcess django_backend \
        python-path=/home/samuel/django_backend:/home/samuel/.virtualenvs/django_backend/lib/python2.7/site-packages
        WSGIProcessGroup django_backend

        Alias /static/ /home/samuel/django_backend/collected_static/

        <Directory /home/samuel/django_backend/collected_static>
        Require all granted
        </Directory>

        WSGIScriptAlias /backend /home/samuel/django_backend/django_backend/wsgi.py

        <Directory /home/samuel/django_backend/django_backend>
            Order allow,deny
            Allow from all
        </Directory>

        <Directory /home/samuel/django_backend/django_backend>
            <Files wsgi.py>
                    Require all granted
            </Files>
        </Directory>


        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        # ServerName 

        ServerAdmin webmaster@localhost
        # DocumentRoot /home/samuel/django_backend/

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/python-django_error.log
        CustomLog ${APACHE_LOG_DIR}/python-django_access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

Thank you in advance !
Samuel

1 Answer

@samuelgirardin

If Django is running on Port 8000, 8080, or another port, you won't be able to access it on Port 80 without setting up Apache as a reverse proxy.

As a reverse proxy, what happens is a request comes in on Port 80 and internally, Apache will serve http://localhost:8000. Without setting up a reverse proxy, you'd be limited to attaching the port to the end of each request.

You can check the guide below for details on how to use Apache as a Reverse Proxy, which should help you configure your VirtualHost for the Django app.

https://www.digitalocean.com/community/tutorials/how-to-use-apache-as-a-reverse-proxy-with-mod_proxy-on-ubuntu-16-04

In this tutorial, you will set up Apache as a reverse proxy using the `mod_proxy` extension to redirect incoming connections to underlying application server(s) running on the same network. There are instructions on setting up a simple web app using the Flask framework to show how Apache interacts with the real application hidden behind it, but you can also follow this tutorial using your existing application server, if you have one.
  • Thanks for your answer !

    With Django it is more common to use modwsgi with Apache as in the links in my post. With this configuration, I guess it is the same idea than modproxy.

    But here the problem I think is that two servers are on 80 port (Wordpress (apache) and Django (virtualhost/apache)...

    I will try to use mod_proxy instead, but I think there might be some configuration to do in WordPress apache conf also to be able to make both sites work on 80 ?

    • @samuelgirardin

      For Apache to respond to a request on port 80, the VirtualHost block must use:

      <VirtualHost *:80>
         ...
         ...
         ...
      </VirtualHost>
      

      Only one service can bind to a port at the same time. In your case, you want to use Apache as your web server, thus it needs to bind to port 80. This means that no other service can bind to this port while Apache is running. Only when you stop Apache can something else bind to that port (the same for port 443 when Apache is running).

      Since WordPress is a PHP application, it doesn't need to run on a port of it's own as it can be serviced using standard VirtualHost configuration with minimal setup.

      Django, on the other hand, needs to bind to it's own port (i.e. 8000, for example), so you need to tell Apache where to send requests. The way to do this is by proxying the request on port 80 to the backend using the socket.

      The same thing would apply if you were to use NGINX in place of Apache -- you'd still need to proxy requests for anything that needs to bind to a port as NGINX takes control of ports 80 and 443.

      For example, if you wanted to run WordPress on one domain and a NodeJS app on another, you'd be setting up a standard block (whether VirtualHost or Server). WordPress wouldn't need much configuration as it doesn't need to be proxied to be served, however, the NodeJS app would.

Have another answer? Share your knowledge.