In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you how to deploy nginx+uwsgi in the django project under Centos8, the content is simple and easy to understand, I hope you can learn, after learning, there will be a harvest, the following let the editor to take a look at it.
1. Virtual environment virtualenv installation
1. Install virtualenv
Pip3 install virtualenv
two。 Create a directory and send the project files over
Mkdir My
Cd My
3. Create a stand-alone running environment-naming
Virtualenv-- no-site-packages-- python=python3 venv1 # creates a separate environment and specifies that the interpreter is python3
4. Enter the virtual environment
Source venv1/bin/activate # enter the virtual environment (venv1) at this time
5. Install third-party libraries in the virtual environment and import the required environment (export command: pip3 freeze > packages.txt)
Pip3 install django==2.11 # at this point, all pip3 packages will be installed in the venv1 environment. Venv1 is created for Myproject.
Pip3 install-r packages.txt
6. Exit the venv1 environment
Deactivate
7. How does virtualenv create an "independent" Python runtime environment? The principle is very simple, that is, copy the system Python to the virtualenv environment.
When you enter a virtualenv environment with the command source venv/bin/activate, virtualenv modifies the relevant environment variables so that both the commands python and pip point to the current virtualenv environment.
2.django configuration
1.settings.py
DEBUG = False # debug change to falseALLOWED_HOSTS = ['*'] # access address to "*" means that all directories accessed by STATIC_ROOT = os.path.join (os.path.dirname (BASE_DIR), "static") # nginx are placed in the directory above the previous static. You can customize the absolute path STATIC_URL ='/ static/' STATICFILES_DIRS= [os.path.join (BASE_DIR, "static"). ] MEDIA_URL ='/ archive/'MEDIA_ROOT = os.path.join (os.path.dirname (BASE_DIR), 'archive') # static files uploaded by users, such as avatars
After configuration, run python manage.py collectstatic to load static files to the STATIC_ROOT directory
2.urls.py
From django.urls import path,re_pathfrom django.conf import settingsfrom django.views.static import serve urlpatterns = [re_path (r'^ archive/ (? P.*) $', serve, {'document_root': settings.MEDIA_ROOT}, name='archive'), # path of files uploaded by users (' favicon.ico', serve, {'path':' img/favicon.ico','document_root':settings.STATIC_ROOT}), # global favicon.ico icon]
3. Install and configure uwsgi
1. Enter the virtual environment venv1 and install uwsgi (preferably outside the virtual environment)
(venv1) [root@localhost ~] # pip3 install uwsgi
two。 Configure the startup file (put it in any directory, I put it under venv1)
Uwsgi supports ini, xml and other configuration methods. Take ini as an example, create a new uwsgi.ini under the / etc/ directory and add the following configuration:
# add configuration Select [uwsgi] # configure socket connection and nginx connection socket=127.0.0.1:8000 # http=0.0.0.0:8000 # http connection # configure project path The directory where the project is located chdir = / opt/My/Myproject # configure the wsgi interface module file path, that is, the directory name of the wsgi.py file wsgi-file = Myproject/wsgi.py # configure the number of processes started processes=4 # configure the number of threads per process threads=2 # configure the startup management main process master=True # virtual environment directory home=/opt/My/venv1 # configure the process number file that holds the main process (I commented It is said to conflict with supervisor logs) # pidfile=uwsgi.pid # configure dump logging (ibid.) # daemonize=uwsgi.log
3. Specify configuration file startup
Uwsgi-ini / opt/My/venv1/uwsgi.ini
4. Install and configure nginx
1.centos8 installation nginx (direct yum installation)
Yum install-y nginx
two。 Configure nginx.conf
User nginx; worker_processes 2; # number of processes error_log / var/log/nginx/error.log; pid / run/nginx.pid; # Load dynamic modules. See / usr/share/doc/nginx/README.dynamic. Include / usr/share/nginx/modules/*.conf; events {worker_connections 1024;} http {log_format main'$remote_addr-$remote_user [$time_local] "$request"'$status $body_bytes_sent "$http_referer"'"$http_user_agent"$http_x_forwarded_for"; access_log / var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048 Include / etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the / etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. # include / etc/nginx/conf.d/*.conf; server {listen 80 port # listening port # listen [:]: 80 default_server; server_name 192.168.3.119 Bing # domain name or IP # root / usr/share/nginx/html; # Load configuration files for the default server block. # include / etc/nginx/default.d/*.conf; charset utf-8; location / static {alias / opt/My/static; # static file address (STATIC_ROOT)} location / {include uwsgi_params; uwsgi_pass 0.0.0.0 opt/My/static; 8000; # project port number uwsgi_param UWSGI_SCRIPT Myproject.wsgi; # project wsgi.py directory uwsgi_param UWSGI_CHDIR / opt/My/Myproject; # project directory}
3. Start nginx
/ usr/sbin/nginx
5. Install and configure supervisor
1. Install supervisor
Pip3 install supervisor # used to require a python2 environment to install it. Now you can install it directly with pip3.
two。 Generate configuration files to the etc directory by command (customizable)
Echo_supervisord_conf > / etc/supervisord.conf
3. Add the following code at the end of the configuration file
[program:myname] # Task name command=/opt/my/venv1/bin/uwsgi-- the command executed by ini / opt/my/venv1/uwsgi.ini # runs uwsgi. Uwsgi is a [program:nginx] command=/usr/sbin/nginx # running nginx in a virtual environment
4. Start supervisor
Supervisord-c / etc/supervisord.conf # launch supervisor supervisorctl-c / etc/supervisord.conf # to enter the supervisor interface
5.supervisor command
Start myname # start\ stop myname # stop > > you can write the task name or all to indicate all restart myname # restart / above is about how to deploy nginx+uwsgi in the django project under Centos8. If you have learned knowledge or skills, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.