This article describes how to deploy a Django site on a shared server of a host provider. Django is the most popular Python framework, for more information, see service providers of shared hosts. They are not allowed to run your own service processes or modify httpd. conf file. However, it is still possible to run Django through sub-processes generated by the Web server.
Record
If you want to use the sub-process of the server, you do not have to start the FastCGI server on your own. Apache automatically generates some sub-processes. The number of sub-processes generated varies according to requirements and configurations.
Add the following content to the. htaccess file under your Web root directory:
AddHandler fastcgi-script .fcgiRewriteEngine OnRewriteCond %{REQUEST_FILENAME} !-fRewriteRule ^(.*)$ mysite.fcgi/$1 [QSA,L]
Next, create a script to tell Apache how to run your FastCGI program. Create a mysite. fcgi file and put it in your Web directory to open executable permissions.
#!/usr/bin/pythonimport sys, os# Add a custom Python path.sys.path.insert(0, "/home/user/python")# Switch to the directory of your project. (Optional.)# os.chdir("/home/user/myproject")# Set the DJANGO_SETTINGS_MODULE environment variable.os.environ['DJANGO_SETTINGS_MODULE'] = "myproject.settings"from django.core.servers.fastcgi import runfastcgirunfastcgi(method="threaded", daemonize="false")
Restart the new process server
If you change any python code on the site, you need to inform FastCGI. However, you do not need to restart Apache. Instead, you only need to re-upload mysite. fcgi or edit and modify the file so that the modification time changes. It will automatically restart the Django application. You can re-upload mysite. fcgi or edit the file to change the timestamp of the file. When the Apache server finds that the document has been updated, it will restart your Django application for you.
If you have the executable permission for the Unix system command line, you only need to simply use the touch command:
touch mysite.fcgi