如果有钱,这件事就没有必要。
腾讯云ubuntu,采用22.04LTS版本,自带Python 3.10.12
Welcome to Ubuntu 22.04 LTS (GNU/Linux 5.15.0-86-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
System information as of Fri Oct 27 09:17:43 PM CST 2023
System load: 0.0068359375 Processes: 123
Usage of /: 8.7% of 49.10GB Users logged in: 1
Memory usage: 26% IPv4 address for eth0: 10.0.8.13
Swap usage: 0%
没有提供root,考虑到ubuntu的不熟练,按常见问题的解决,添加root按密码登陆,避免了权限修改问题。
1.安装Nginx
root@VM-8-13-ubuntu:/home/ubuntu/hnuliulinhai/blogproject# nginx -v
nginx version: nginx/1.18.0 (Ubuntu)
Nginx 的配置文件分两块,主体位于 /etc/nginx/nginx.conf 文件中,该设置文件开头采用user root,该设置默认为www-data,需要修改,否则导致静态文件不能加载,响应403拒绝访问,此事浪费较长时间调试。
主体文件中通过include /etc/nginx/conf.d/*.conf;语句将我们的另一位于/etc/nginx/conf.d目录下的server配置文件mysite_nginx.conf加载。
nginx重启不在赘述
2.使用pipenv
pip3 install pipenv
cd /home/ubuntu/hnuliulinhai/blogproject
pipenv install django==4.0
pipenv install django-contrib-comments
pipenv install django-qiniu-strorage
pipenv install django-ckeditor
pipenv install gunicorn
pipenv install django-utils-six
pipenv install Pillow
在项目文件夹下,使用pipenv install安装,可以自动生成环境。所有包装好后,pipenv graph后的结果:
root@VM-8-13-ubuntu:/home/ubuntu/hnuliulinhai/blogproject# pipenv graph
django-ckeditor==6.7.0
├── Django [required: >=3.2, installed: 4.0]
│ ├── asgiref [required: >=3.4.1,<4, installed: 3.7.2]
│ │ └── typing-extensions [required: >=4, installed: 4.8.0]
│ └── sqlparse [required: >=0.2.2, installed: 0.4.4]
└── django-js-asset [required: >=2.0, installed: 2.1.0]
└── django [required: >=3.2, installed: 4.0]
├── asgiref [required: >=3.4.1,<4, installed: 3.7.2]
│ └── typing-extensions [required: >=4, installed: 4.8.0]
└── sqlparse [required: >=0.2.2, installed: 0.4.4]
django-contrib-comments==2.2.0
└── Django [required: >=2.2, installed: 4.0]
├── asgiref [required: >=3.4.1,<4, installed: 3.7.2]
│ └── typing-extensions [required: >=4, installed: 4.8.0]
└── sqlparse [required: >=0.2.2, installed: 0.4.4]
django-qiniu-storage==2.3.1
├── qiniu [required: >=7.1.0, installed: 7.12.0]
│ └── requests [required: Any, installed: 2.31.0]
│ ├── certifi [required: >=2017.4.17, installed: 2023.7.22]
│ ├── charset-normalizer [required: >=2,<4, installed: 3.3.1]
│ ├── idna [required: >=2.5,<4, installed: 3.4]
│ └── urllib3 [required: >=1.21.1,<3, installed: 2.0.7]
├── requests [required: Any, installed: 2.31.0]
│ ├── certifi [required: >=2017.4.17, installed: 2023.7.22]
│ ├── charset-normalizer [required: >=2,<4, installed: 3.3.1]
│ ├── idna [required: >=2.5,<4, installed: 3.4]
│ └── urllib3 [required: >=1.21.1,<3, installed: 2.0.7]
└── six [required: Any, installed: 1.16.0]
django-utils-six==2.0
gunicorn==21.2.0
└── packaging [required: Any, installed: 23.2]
Pillow==10.1.0
pipenv shell可以进入环境,可以省略输入pipenv run。
cd /home/ubuntu/hnuliulinhai/blogproject
pipenv shell
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
3.使用gunicorn
在Nginx的server配置文件中,除了一些静态文件由Nginx直接处理外,其他传给代理gunicorn处理,即:
location / {
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8000;
#uwsgi_pass django;
#include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
}
在项目文件下,运行gunicorn的方式:
cd /home/ubuntu/hnuliulinhai/blogproject
pipenv run gunicorn blogproject.wsgi -w 2 -k gthread -b 127.0.0.1:8000
从上可知,127.0.0.1:8000与Nginx里的配置有对应关系。
4.使用supervisor
采用如下目录结构管理相关文件,同时对配置文件的目录进行相应修改。
其中 supervisord.conf 是 Supervior 的配置文件,它会通过include包含 conf.d 下的配置。var 目录下用于存放一些经常变动的文件,例如 socket 文件,pid 文件,log 下则存放日志文件。
~/etc
├── supervisor
│ ├── conf.d
│ └── var
│ ├── log
└── supervisord.conf
首先建立目录结构:
mkdir -p ~/etc/supervisor/conf.d
mkdir -p ~/etc/supervisor/var/log
相应的文件,如果网站目录结构没有发生变化,可以直接使用,不再赘述,摘录见后。
其常见用法为查看状态和重启,最好先到配置文件目录,cd /root/etc
root@VM-8-13-ubuntu:~# sudo supervisorctl status
blogproject RUNNING pid 740426, uptime 2 days, 17:46:44
root@VM-8-13-ubuntu:~# sudo supervisorctl restart blogproject
blogproject: stopped
blogproject: started
root@VM-8-13-ubuntu:~# sudo supervisorctl status
blogproject RUNNING pid 1697793, uptime 0:00:09
root@VM-8-13-ubuntu:~#
supervisord.conf文件:
; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; Notes:
; - Shell expansion ("~" or "$HOME") is not supported. Environment
; variables can be expanded using this syntax: "%(ENV_HOME)s".
; - Quotes around values are not supported, except in the case of
; the environment= options as shown below.
; - Comments must have a leading space: "a=b ;comment" not "a=b;comment".
; - Command will be truncated if it looks like a config file comment, e.g.
; "command=bash -c 'foo ; bar'" will truncate to "command=bash -c 'foo ".
;
; Warning:
; Paths throughout this example file use /tmp because it is available on most
; systems. You will likely need to change these to locations more appropriate
; for your system. Some systems periodically delete older files in /tmp.
; Notably, if the socket file defined in the [unix_http_server] section below
; is deleted, supervisorctl will be unable to connect to supervisord.
[unix_http_server]
file=/root/etc/supervisor/var/supervisor.sock ; the path to the socket file
;chmod=0700 ; socket file mode (default 0700)
;chown=nobody:nogroup ; socket file uid:gid owner
;username=user ; default is no username (open server)
;password=123 ; default is no password (open server)
; Security Warning:
; The inet HTTP server is not enabled by default. The inet HTTP server is
; enabled by uncommenting the [inet_http_server] section below. The inet
; HTTP server is intended for use within a trusted environment only. It
; should only be bound to localhost or only accessible from within an
; isolated, trusted network. The inet HTTP server does not support any
; form of encryption. The inet HTTP server does not use authentication
; by default (see the username= and password= options to add authentication).
; Never expose the inet HTTP server to the public internet.
;[inet_http_server] ; inet (TCP) server disabled by default
;port=127.0.0.1:9001 ; ip_address:port specifier, *:port for all iface
;username=user ; default is no username (open server)
;password=123 ; default is no password (open server)
[supervisord]
logfile=/root/etc/supervisor/var/log/supervisord.log ; main log file; default $CWD/supervisord.log
logfile_maxbytes=50MB ; max main logfile bytes b4 rotation; default 50MB
logfile_backups=10 ; # of main logfile backups; 0 means none, default 10
loglevel=info ; log level; default info; others: debug,warn,trace
pidfile=/root/etc/supervisor/var/supervisord.pid ; supervisord pidfile; default supervisord.pid
nodaemon=false ; start in foreground if true; default false
silent=false ; no logs to stdout if true; default false
minfds=1024 ; min. avail startup file descriptors; default 1024
minprocs=200 ; min. avail process descriptors;default 200
;umask=022 ; process file creation umask; default 022
user=root ; setuid to this UNIX account at startup; recommended if root
;identifier=supervisor ; supervisord identifier, default is 'supervisor'
;directory=/tmp ; default is not to cd during start
;nocleanup=true ; don't clean up tempfiles at start; default false
;childlogdir=/tmp ; 'AUTO' child log dir, default $TEMP
;environment=KEY="value" ; key value pairs to add to environment
;strip_ansi=false ; strip ansi escape codes in logs; def. false
; The rpcinterface:supervisor section must remain in the config file for
; RPC (supervisorctl/web interface) to work. Additional interfaces may be
; added by defining them in separate [rpcinterface:x] sections.
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
; The supervisorctl section configures how supervisorctl will connect to
; supervisord. configure it match the settings in either the unix_http_server
; or inet_http_server section.
[supervisorctl]
serverurl=unix:///root/etc/supervisor/var/supervisor.sock ; use a unix:// URL for a unix socket
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris ; should be same as in [*_http_server] if set
;password=123 ; should be same as in [*_http_server] if set
;prompt=mysupervisor ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history ; use readline history if available
; The sample program section below shows all possible program subsection values.
; Create one or more 'real' program: sections to be able to control them under
; supervisor.
;[program:theprogramname]
;command=/bin/cat ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1 ; number of processes copies to start (def 1)
;directory=/tmp ; directory to cwd to before exec (def no cwd)
;umask=022 ; umask for process (default None)
;priority=999 ; the relative start priority (default 999)
;autostart=true ; start at supervisord start (default: true)
;startsecs=1 ; # of secs prog must stay up to be running (def. 1)
;startretries=3 ; max # of serial start failures when starting (default 3)
;autorestart=unexpected ; when to restart if exited after running (def: unexpected)
;exitcodes=0 ; 'expected' exit codes used with autorestart (default 0)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false ; send stop signal to the UNIX process group (default false)
;killasgroup=false ; SIGKILL the UNIX process group (def false)
;user=chrism ; setuid to this UNIX account to run the program
;redirect_stderr=true ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10 ; # of stdout logfile backups (0 means none, default 10)
;stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
;stdout_events_enabled=false ; emit events on stdout writes (default false)
;stdout_syslog=false ; send stdout to syslog with process name (default false)
;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10 ; # of stderr logfile backups (0 means none, default 10)
;stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;stderr_syslog=false ; send stderr to syslog with process name (default false)
;environment=A="1",B="2" ; process environment additions (def no adds)
;serverurl=AUTO ; override serverurl computation (childutils)
; The sample eventlistener section below shows all possible eventlistener
; subsection values. Create one or more 'real' eventlistener: sections to be
; able to handle event notifications sent by supervisord.
;[eventlistener:theeventlistenername]
;command=/bin/eventlistener ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1 ; number of processes copies to start (def 1)
;events=EVENT ; event notif. types to subscribe to (req'd)
;buffer_size=10 ; event buffer queue size (default 10)
;directory=/tmp ; directory to cwd to before exec (def no cwd)
;umask=022 ; umask for process (default None)
;priority=-1 ; the relative start priority (default -1)
;autostart=true ; start at supervisord start (default: true)
;startsecs=1 ; # of secs prog must stay up to be running (def. 1)
;startretries=3 ; max # of serial start failures when starting (default 3)
;autorestart=unexpected ; autorestart if exited after running (def: unexpected)
;exitcodes=0 ; 'expected' exit codes used with autorestart (default 0)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false ; send stop signal to the UNIX process group (default false)
;killasgroup=false ; SIGKILL the UNIX process group (def false)
;user=chrism ; setuid to this UNIX account to run the program
;redirect_stderr=false ; redirect_stderr=true is not allowed for eventlisteners
;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10 ; # of stdout logfile backups (0 means none, default 10)
;stdout_events_enabled=false ; emit events on stdout writes (default false)
;stdout_syslog=false ; send stdout to syslog with process name (default false)
;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10 ; # of stderr logfile backups (0 means none, default 10)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;stderr_syslog=false ; send stderr to syslog with process name (default false)
;environment=A="1",B="2" ; process environment additions
;serverurl=AUTO ; override serverurl computation (childutils)
; The sample group section below shows all possible group values. Create one
; or more 'real' group: sections to create "heterogeneous" process groups.
;[group:thegroupname]
;programs=progname1,progname2 ; each refers to 'x' in [program:x] definitions
;priority=999 ; the relative start priority (default 999)
; The [include] section can just contain the "files" setting. This
; setting can list multiple files (separated by whitespace or
; newlines). It can also contain wildcards. The filenames are
; interpreted as relative to this file. Included files *cannot*
; include files themselves.
[include]
files = /root/etc/supervisor/conf.d/*.ini
conf.d/blogproject.ini文件
[program:blogproject]
command=pipenv run gunicorn blogproject.wsgi -w 2 -k gthread -b 127.0.0.1:8000
directory=/home/ubuntu/hnuliulinhai/blogproject
autostart=true
autorestart=unexpected
user=root
stdout_logfile=/root/etc/supervisor/var/log/blogproject.log
stderr_logfile=/root/etc/supervisor/var/log/blogproject.log
这里面autostart是使用supervisor的主要原因。
5.升级Django4.0
由于使用了python3.10版本,将Django从2.2升级到了4.0,主要修改有
settings.py添加如下配置:
DEFAULT_AUTO_FIELD='django.db.models.AutoField'
SECURE_CROSS_ORIGIN_OPENER_POLICY = 'None'
#2023.10.27
urls.py修改引用包django.urls,方法url改用re_path:
#from django.conf.urls import url,include
from django.urls import re_path,include
from django.contrib import admin
#from DjangoUeditor import urls as DjangoUeditor_urls
from blog.feeds import AllPostsRssFeed
urlpatterns = [
re_path(r'^admin/', admin.site.urls),
#url(r'',include('blog.urls')),
re_path(r'',include('blog.urls')),
#url(r'^ueditor/',include('DjangoUeditor.urls' )),
re_path(r'^comments/', include('django_comments.urls')),
re_path(r'^all/rss/$', AllPostsRssFeed(), name='rss'),
re_path(r'^ckeditor/', include('ckeditor_uploader.urls')),
]
blog里的urls.py亦相同调整。
模板文件detail2.html使用模板标签if替换ifequal。
6. 其他
将域名解析调整,已经无缝衔接到原有文章的访问,并此文为计。
Last Modified·2023年10月30日 15:26
您尚未登录,请先登录才能评论。