Warm tip: This article is reproduced from serverfault.com, please click

Apache 2

发布于 2020-11-29 14:01:51

I have an Apache2 HTTP Server set up on a Ubuntu Linux Server according to Corey Schafer's youtube playlist about Django to serve a django application (A Hobby-Project to learn Django) via WSGI. The server is set-up to use HTTPS instead of HTTP.

I would like to also use this HTTP Server to deploy a Single-Page-Application (SPA) developed with Angular 10. I am trying to have this configuration satisfy 3 criteria:

  1. Serve my django application
  2. Serve my Angular SPA
  3. Serve the Angular SPA without me changing the index.html file of the angular SPA (e.g. by modifying the src-urls used in the js-script-tags)

Point 3 is mostly there so I can do quick and easy deployment of updates to my SPA by just pulling an updated bundle from a git-repo whenever there is one.

What I configured below so far (and successfully serves my Django App) serves my django-app correctly in the url namespace <DOMAIN>/wiki. The Angular SPA is served in the url namespace <DOMAIN>/wiki2.

#Apache2 config file aldrunewiki-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
    ...
    # Configuration for the Angular SPA
    AliasMatch ^/wiki2/(.*)$ /home/isofruit/AldruneWikiFrontend/dist/frontend/index.html
    <Directory /home/isofruit/AldruneWikiFrontend/dist/frontend>
        Require all granted
        DirectoryIndex index.html
    </Directory>

    # Configuration for the Django Application
    Alias /static /home/isofruit/AldruneWiki/static
    <Directory /home/isofruit/AldruneWiki/static>
                Require all granted
                RewriteEngine on
                RewriteCond %{HTTP_REFERER} !^https://aldrune.com/.*$ [NC]
                RewriteCond %{HTTP_REFERER} !^https://www.aldrune.com/.*$ [NC]
                RewriteRule .(mp3|jpg|png)$ - [F]
    </Directory>

    Alias /media /home/isofruit/AldruneWiki/media
    <Directory /home/isofruit/AldruneWiki/media>
                Require all granted
                RewriteEngine on
                RewriteCond %{HTTP_REFERER} !^https://aldrune.com/.*$ [NC]
                RewriteCond %{HTTP_REFERER} !^https://www.aldrune.com/.*$ [NC]
                RewriteRule .(mp3|jpg|png)$ - [F]
    </Directory>

    <Directory /home/isofruit/AldruneWiki/aldrunewiki>
    <Files wsgi.py>
                Require all granted
    </Files>
    </Directory>


    WSGIScriptAlias /wiki /home/isofruit/AldruneWiki/aldrunewiki/wsgi.py
    WSGIDaemonProcess aldrunewiki_app python-path=/home/isofruit/AldruneWiki python-home=/home/isofruit/AldruneWiki/venv
    WSGIProcessGroup aldrunewiki_app


SSLCertificateFile <REMOVED>
SSLCertificateKeyFile <REMOVED>
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

This does work so far as I do get the index.html, but the JS files it tries to load fail with 404: enter image description here

That is because the src-URLs of the <script>-tags in my index.html have the pattern <DOMAIN>/filename.js and I don't have a proper alias for that in aldrunewiki-le-ssl.conf.

#Example of script tags in index.html
...
<script src="runtime.c6a2assa9aec81ed0sc9.js" defer></script>
<script src="polyfills.5c8ss3dfb7c3b4aass01.js" defer></script>

I don't know how to solve this well.

If I want to not change my Angular-SPA index.html, I need to use an Alias in aldrunewiki-le-ssl.conf that maps <DOMAIN>/filename.js to my AldruneWikiFrontEnd directory (Where the Angular-SPA is). But doesn't that break the url namespace of my Django Application because it'd look like this: Alias / /home/isofruit/AldruneWikiFrontEnd/dist/frontend?

Would it be better I abandon Point 3 and set up a githook that changes the script tags in my Angular-SPA index.html after I make a pull?

Questioner
Philipp Doerner
Viewed
0
Philipp Doerner 2020-12-02 01:24:57

I have decided to abandon parameter 3 and instead write a pre-commit git-hook that modifies my index.html as I need it.

WARNING: This only works for me because before any changes that I commit, I have already run ng build --prod to have a dist folder of my current version and because I also commit my dist folder in my repository.

If you do not have this kind of workflow I highly recommend to not use pre-commit, but a different hook that more accurately represents whenever you have made a build. Below the code for the commit because why not.

### pre-commit file

#!/usr/bin/env python3

index_filepath: str = "/home/isofruit/Dev/AldruneWiki/frontend/dist/frontend/index.html"
prefix: str = "wiki2"

def main():
    modify_index_html()

def modify_index_html():
    index_file: str = read_file(index_filepath)

    index_file = index_file.replace('src="', f'src="{prefix}/') #Updates URL of JS script tags 
    index_file = index_file.replace('href="favicon.ico"', f'href="{prefix}/favicon.ico"') #Update URL of icon
    index_file = index_file.replace('href="styles', f'href="{prefix}/styles') #Updates URL of style-spreadsheet

    write_file(index_filepath, index_file)

def read_file(filepath):
    file_content: str
    with open(filepath) as f:
        file_content = f.read()
    return file_content

def write_file(filepath, content):
    with open(filepath, 'w') as f:
        f.write(content)

if __name__ == "__main__":
    main()