Repository: ckan/ckan

Issue:
Update the WSGI entrypoint to not use paste.deploy
TODO

paste.deploy loadapp

#### Approach


#### Story points


Discussion:
We want to get rid of PasteDeploy's `loadapp` and instead use Werkzeug.

It looks like @JGulic has done this for the Profile command - https://github.com/ckan/ckan/pull/5244.

The heart of this ticket is to change the .wsgi file we use to deploy CKAN into production: https://github.com/ckan/ckan/blob/master/doc/maintaining/installing/deployment.rst#4-create-the-wsgi-script-file 

A basic Werkzeug .wsgi is documented here: https://werkzeug.palletsprojects.com/en/1.0.x/deployment/mod_wsgi/#creating-a-wsgi-file

Our [make_app()](https://github.com/ckan/ckan/blob/master/ckan/config/middleware/__init__.py#L50) also needs a config parameter. So I guess we need to do: CKANConfigLoader(filename).get_config()
(whereas loadapp we just passed in the filename)

The existing apache.wsgi also configures logging:
```
from paste.script.util.logging_config import fileConfig
    fileConfig(config_filepath)
```
We want to get rid of PasteScript in py3 requirements. Logging is not configured from the ini when running tests or the click cli, so this is a new thing to ckan. It looks the built-in python3 logging module has what we want: [logging.config.fileConfig](https://docs.python.org/3.5/library/logging.config.html#logging.config.fileConfig)
☝️ I wrote this with a bit of research - I'd appreciate @amercader or @smotornyuk checking this is right. And if it is then it should be easier for whoever gets there first to contribute a PR and test it out with Apache.
I will also be able to test the new (Werkzeug)  .wsgi file with the new uWSGI and gunicorn "servers/entrypoints" as I have now finished these new configurations. They both work with the PasteDeploy loadapp file.  I'm guessing Apache will still be around for CKAN 2.9 however I could add these 2 new configs to the CKAN 2.9 documentation. I guess we will be phasing out Apache completely by CKAN 3.0...
@kowh-ai I'm very happy to see https://github.com/ckan/ckan/issues/4991 progressing 👍 

When we have a PR for this issue, it doesn't really matter which server it is tested against - I just said Apache as it was the current default - so I'm also very happy to see your offer of testing it 🙏 

Resolving patch:
diff --git a/doc/conf.py b/doc/conf.py
--- a/doc/conf.py
+++ b/doc/conf.py
@@ -50,6 +50,7 @@
 .. |test_datastore| replace:: datastore_test
 .. |apache_config_file| replace:: /etc/apache2/sites-available/ckan_default.conf
 .. |apache.wsgi| replace:: |config_dir|/apache.wsgi
+.. |wsgi.py| replace:: |config_dir|/wsgi.py
 .. |data_dir| replace:: |config_dir|/data
 .. |sstore| replace:: |config_dir|/sstore
 .. |storage_parent_dir| replace:: /var/lib/ckan
@@ -67,7 +68,8 @@
 .. |javascript| replace:: JavaScript
 .. |apache| replace:: Apache
 .. |nginx_config_file| replace:: /etc/nginx/sites-available/ckan
-.. |restart_nginx| replace:: sudo systemctl restart nginx
+.. |reload_nginx| replace:: sudo service nginx reload
+.. |restart_nginx| replace:: sudo service nginx restart
 .. |jquery| replace:: jQuery
 .. |nodejs| replace:: Node.js
 
diff --git a/wsgi.py b/wsgi.py
new file mode 100644
--- /dev/null
+++ b/wsgi.py
@@ -0,0 +1,12 @@
+# -*- coding: utf-8 -*-
+
+import os
+from ckan.config.middleware import make_app
+from ckan.cli import CKANConfigLoader
+from logging.config import fileConfig as loggingFileConfig
+config_filepath = os.path.join(
+    os.path.dirname(os.path.abspath(__file__)), u'ckan.ini')
+abspath = os.path.join(os.path.dirname(os.path.abspath(__file__)))
+loggingFileConfig(config_filepath)
+config = CKANConfigLoader(config_filepath).get_config()
+application = make_app(config)


Test patch: