内容纲要
最近用腾讯云购买了一个轻量服务器,然后折腾了一下,成功在上面部署了一个Python Django服务程序,记录一下
一、Linux版本的选择
运行Python310的Django 4.1.2
- 腾讯云CentOS8系统和Ubuntu 20可用
- 腾讯云的CentOS 7系统,Django会报Sqlite错
SQLITE 3.8.3 OR LATER IS REQUIRED
在使用Python310版本时,上述错误暂时没有解决办法。
最终,先用Ubuntu 20版本的系统。
二、在Ubuntu 20上安装Python310
参考资料:https://www.cnblogs.com/jsdy/p/12694908.html
安装此资料参考下来,没有遇到问题
1. 先update一下
sudo apt update
2. 安装依赖库
sudo apt-get install zlib1g-dev libbz2-dev libssl-dev libncurses5-dev libsqlite3-dev libreadline-dev tk-dev libgdbm-dev libdb-dev libpcap-dev xz-utils libexpat1-dev liblzma-dev libffi-dev libc6-dev
3. 下载Python3安装包,并解压
tar zxvf Python-3.10.0.tgz
进入Python-3.10目录:
cd Python-3.10.0
4. 设置编译选项:
./configure --enable-optimizations
会有以下类似的输出:
....
checking whether gcc -pthread supports computed gotos... yes
checking for build directories... done
checking for -O2... yes
checking for glibc _FORTIFY_SOURCE/memmove bug... no
checking for gcc ipa-pure-const bug... no
checking for stdatomic.h... no
checking for builtin __atomic_load_n and __atomic_store_n functions... yes
checking for ensurepip... upgrade
checking if the dirent structure of a d_type field... yes
checking for the Linux getrandom() syscall... yes
checking for the getrandom() function... no
...
5. 编译和安装Python310:
sudo make altinstall
此步耗费较长时间,会有类似以下的输出:
....
running install_scripts
copying build/scripts-3.10/pydoc3.10 -> /usr/local/bin
copying build/scripts-3.10/idle3.10 -> /usr/local/bin
copying build/scripts-3.10/2to3-3.10 -> /usr/local/bin
changing mode of /usr/local/bin/pydoc3.10 to 755
changing mode of /usr/local/bin/idle3.10 to 755
changing mode of /usr/local/bin/2to3-3.10 to 755
rm /usr/local/lib/python3.10/lib-dynload/_sysconfigdata__linux_x86_64-linux-gnu.py
rm -r /usr/local/lib/python3.10/lib-dynload/__pycache__
/bin/install -c -m 644 ./Misc/python.man \
/usr/local/share/man/man1/python3.10.1
if test "xupgrade" != "xno" ; then \
case upgrade in \
upgrade) ensurepip="--altinstall --upgrade" ;; \
install|*) ensurepip="--altinstall" ;; \
esac; \
./python -E -m ensurepip \
$ensurepip --root=/ ; \
fi
...
6. 检查版本Python 3.10
$ python3.10 --version Python 3.10.0
Pip3.10 must also have been installed. Run the command below to check:
$ pip3.10 --version pip 21.2.3 from /usr/local/lib/python3.10/site-packages/pip (python 3.10)
至此,应该安装完成
三、安装django
1. 使用pip3.10安装django(这边会自动安装4.1.2)
pip3.10 install django
2. 创建django工程
参考资料:https://docs.djangoproject.com/en/4.1/intro/tutorial01/
打开终端,创建名为mysite的django工程输入:
$ django-admin startproject mysite
进入mysite目录,运行:
$ python3.10 manage.py runserver
看到类似下面输出即表示运行成功:
Performing system checks...
System check identified no issues (0 silenced).
You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
October 21, 2022 - 15:50:53
Django version 4.1, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
3. 访问django
浏览器输入上述的url:http://127.0.0.1:8000/
如果部署后,发现页面的样式丢失的解决办法:
参考资料:https://blog.csdn.net/m0_37780940/article/details/119965539
a. setting.py 添加 STATIC_ROOT
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
b. 执行,重新收集样式:
$ python3.10 manage.py collectstatic