Constant dropping wears the stone
Drip through stone
This article is used to record some notes on Django deployment. The system environment described in this article is Ubuntu, the server used is nginx and uwsgi is used to connect to Django. This is also the mainstream deployment package of Django.
Run and view status
Install python-dev first, otherwise uwsgi installation may report an error
Add environment variables after installation:
Open the file: sudo vim .bashrc and add the following:
Then run source .bashrc to make it work, you can run uwsgi directly from the command line.
Open http://localhost:80, you can see that nginx indicates that nginx is successfully installed.
Create a test.py file in the root directory of the project and write:
Run under the project root directory:
Visit http://localhost:8001, if you can see hello world, the uwsgi installation is successful.
- http : protocol type and port number
- processes : number of processes opened
- workers : The number of processes opened, which is equivalent to processes (the official website is spawn the specified number of workers / processes)
- chdir : specify the run directory (chdir to specified directory before apps loading)
- stats : enable the stats server on the specified address
- threads : Run the thread. Because of the existence of GIL, I feel that this is really useless. (run each worker in prethreaded mode with the specified number of threads)
- master : allows the main process to exist (enable master process)
- daemonize : Causes the process to run in the background and logs the log to the specified log file or udp server (daemonize uWSGI). In fact, the most common one is to output the log to a local file.
- pidfile : Specify the location of the pid file and record the pid number of the main process.
- vacuum : Automatically clean up the environment when the server exits, delete the unix socket file and the pid file (try to remove all of the generated file/sockets)
Create the project root directory: myweb_uwsgi.ini file, write:
Run django with uwsgi: (same as the previous command line, so that it is easy to write a file)
Configuration file parameters:
- socket: refers to the port that uwsgi runs.
- Chdir: the directory to run
- Module: running file
Open /etc/nginx/nginx.conf and add the following to http:
Description: The 8000 port here is the uwsgi port, and the nginx operation will open the 8890 port, that is, the 8890 port of nginx and the 8000 port of uwsgi communicate with each other.
Finally, visit http://localhost:8890 and you can see that the django project has been run on nginx.
Note: After updating the Django code, it is best to restart the uwsgi process to avoid unpredictable bugs!
- If the server is mapped, the server_name in the nginx configuration file should write the IP or domain name of the external network.
- Do not use uwsgi in the python virtual environment, there will be some problems, of course, can also be resolved, reference: https://stackoverflow.com/questions/14194859/importerror-no-module-named-django-core-wsgi-for- Uwsgi
- If uwsgi is running normally, nginx is also running normally, but it is not connected. You can check if selinux is enabled in the system and you need to close it.
The following error was reported when starting uwsgi:
When uwsgi is started in the python virtual environment, a virtual path is added to the configuration file.
Open django.init (create it yourself) write:
This is also because of the virtual environment, it is recommended to exit the python virtual environment, and then pip install uwsgi.