docker上启动nginx,并配置修改nginx的配置文件_docker run -d __ /data/:date/ nginx_Dhjie_king的博客-CSDN博客
修改nginx.conf文件,制作新的image
docker 实际部署
https://www.cnblogs.com/shouwangrenjian/p/16499447.html
Flask + Docker 容器化部署Python应用_flask docker_酒酿小圆子~的博客-CSDN博客
一、依赖
python3
虚拟环境
uwsgi
nginx
二、安装
1、安装python3
安装gcc:因为python的底层是用c语言写的
yum install -y gcc
安装下载python时用到的依赖:用来编译、安装python时能够用到
yum install -y zlib zlib-devel
yum install -y bzip2 bzip2-devel
yum install -y ncurses ncurses-devel
yum install -y readline readline-devel
yum install -y openssl openssl-devel
yum install -y xz lzma xz-devel
yum install -y sqlite sqlite-devel
yum install -y gdbm gdbm-devel
yum install -y tk tk-devel
yum install -y mysql-devel
yum install -y python-devel
yum install -y libffi-devel
下载python3源码:
cd /data/
yum install -y wget
wget https://www.python.org/ftp/python/3.9.5/Python-3.9.5.tgz
解压 & 编译 & 安装:
tar -xvf Python-3.9.5.tgz
cd Python-3.9.5
./configure
make all
make install
配置python解释器pip第三方包时的下载源(豆瓣源):
pip3.9 config set global.index-url https://pypi.douban.com/simple/
2、安装虚拟环境
安装virtualenv
pip3.9 install virtualenv
创建虚拟环境
mkdir envs
[root@centos7computer1 envs]# virtualenv /home/susu/work/python/envs/common --python=python3.8
bash: virtualenv: 未找到命令...
[root@centos7computer1 virtualenv]# find / -name virtualenv
find: ‘/run/user/1000/gvfs’: 权限不够
/usr/local/python3/bin/virtualenv
/usr/local/python3/lib/python3.8/site-packages/virtualenv
[root@centos7computer1 virtualenv]#
[root@centos7computer1 virtualenv]#
[root@centos7computer1 virtualenv]# ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv
[root@centos7computer1 virtualenv]#
[root@centos7computer1 virtualenv]#
[root@centos7computer1 virtualenv]#
[root@centos7computer1 virtualenv]# virtualenv /home/susu/work/python/envs/common --python=python3.8
created virtual environment CPython3.8.15.final.0-64 in 619ms
...
[root@centos7computer1 bin]# source /home/susu/work/python/envs/common/bin/activate
(common) [root@centos7computer1 bin]#
3、安装uwsgi
安装uwsgi
(common) [root@centos7computer1 python]# pip install uwsgi
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.org', port=443): Read timed out. (read timeout=15)")': /simple/uwsgi/
Collecting uwsgi
Downloading uwsgi-2.0.21.tar.gz (808 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 808.5/808.5 kB 29.5 kB/s eta 0:00:00
Preparing metadata (setup.py) ... done
Building wheels for collected packages: uwsgi
Building wheel for uwsgi (setup.py) ... error
error: subprocess-exited-with-error
× python setup.py bdist_wheel did not run successfully.
│ exit code: 1
╰─> [36 lines of output]
/home/susu/work/python/envs/common/lib/python3.8/site-packages/setuptools/_distutils/dist.py:265: UserWarning: Unknown distribution option: 'descriptions'
warnings.warn(msg)
Traceback (most recent call last):
File "<string>", line 2, in <module>
File "<pip-setuptools-caller>", line 34, in <module>
File "/tmp/pip-install-ig9gm5pa/uwsgi_857d760e332c4d3b9e6723a1aca39f4f/setup.py", line 117, in <module>
setup(
File "/home/susu/work/python/envs/common/lib/python3.8/site-packages/setuptools/__init__.py", line 107, in setup
return distutils.core.setup(**attrs)
File "/home/susu/work/python/envs/common/lib/python3.8/site-packages/setuptools/_distutils/core.py", line 172, in setup
ok = dist.parse_command_line()
File "/home/susu/work/python/envs/common/lib/python3.8/site-packages/setuptools/_distutils/dist.py", line 475, in parse_command_line
args = self._parse_command_opts(parser, args)
File "/home/susu/work/python/envs/common/lib/python3.8/site-packages/setuptools/dist.py", line 1142, in _parse_command_opts
nargs = _Distribution._parse_command_opts(self, parser, args)
File "/home/susu/work/python/envs/common/lib/python3.8/site-packages/setuptools/_distutils/dist.py", line 534, in _parse_command_opts
cmd_class = self.get_command_class(command)
File "/home/susu/work/python/envs/common/lib/python3.8/site-packages/setuptools/dist.py", line 989, in get_command_class
self.cmdclass[command] = cmdclass = ep.load()
File "/home/susu/work/python/envs/common/lib/python3.8/site-packages/setuptools/_vendor/importlib_metadata/__init__.py", line 208, in load
module = import_module(match.group('module'))
File "/usr/local/python3/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 843, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/susu/work/python/envs/common/lib/python3.8/site-packages/wheel/bdist_wheel.py", line 28, in <module>
from .macosx_libfile import calculate_macosx_platform_tag
File "/home/susu/work/python/envs/common/lib/python3.8/site-packages/wheel/macosx_libfile.py", line 43, in <module>
import ctypes
File "/usr/local/python3/lib/python3.8/ctypes/__init__.py", line 7, in <module>
from _ctypes import Union, Structure, Array
ModuleNotFoundError: No module named '_ctypes'
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for uwsgi
Running setup.py clean for uwsgi
Failed to build uwsgi
ERROR: Could not build wheels for uwsgi, which is required to install pyproject.toml-based projects
解决:centos7安装uwsgi_夏夏夏夏的博客-CSDN博客
配置文件(.ini)启动
config.ini
[uwsgi]
# socket = 127.0.0.1:8001 # socket端口,接收nginx发送过来的请求
http = :5003 # 启动程序时使用的地址和端口,使用uwsgi启动flask项目时使用http端口
chdir = /program_path
wsgi-file = app.py
callable = app
processes = 1
virtualenv = /virtualenv_path
buffer-size = 65536
启动
source /虚拟环境
uwsgi --ini config.ini
启动后若报错:invalid request block size: 21573 (max 4096)...skip?
添加配置:buffer-size = 65536
uwsgi内部解析的数据包大小默认为4k,请求的大小为21573,大于默认的4096,所以被丢弃
启动后没有接收到客户端发送过来的请求?
检查.ini配置文件里面使用的是http的端口,还是socket端口
停止
ps -ef | grep config
kill -9 *****
4、安装 Nginx
安装
(common) [root@centos7computer1 python]# yum install nginx -y
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.ustc.edu.cn
* extras: mirrors.ustc.edu.cn
* updates: mirrors.nju.edu.cn
没有可用软件包 nginx。
错误:无须任何处理
(common) [root@centos7computer1 python]#
(common) [root@centos7computer1 python]# yum list | grep nginx
pcp-pmda-nginx.x86_64 4.3.2-13.el7_9 updates
(common) [root@centos7computer1 python]#
(common) [root@centos7computer1 python]#
(common) [root@centos7computer1 python]# sudo yum install epel-release
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
...
(common) [root@centos7computer1 python]# yum update
已加载插件:fastestmirror, langpacks
/var/run/yum.pid 已被锁定,PID 为 17023 的另一个程序正在运行。
...
(common) [root@centos7computer1 python]# yum install -y nginx
...
已安装:
nginx.x86_64 1:1.20.1-10.el7
作为依赖被安装:
nginx-filesystem.noarch 1:1.20.1-10.el7 openssl11-libs.x86_64 1:1.1.1k-5.el7
完毕!
配置
- 普通请求 -> 8001 端口
- /static(静态请求) -> 静态目录下
nginx 的默认配置文件路径:/etc/nginx/nginx.conf
http{
upstream flask{
server 127.0.0.1:8001;
}
server{
listen 80;
...
location /static {
alias /静态文件路径
}
location / {
uwsgi_pass flask;
include uwsgi_params;
}
}
}
启动
systemctl start nginx
systemctl enable nginx
启动时报错:
systemctl start nginx
Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xe" for details.
Nginx 启动报 [emerg] bind() to 0.0.0.0:XXXX failed (13: Permission denied)错误处理_RunSnail2018的博客-CSDN博客
输入命令systemctl status nginx.service,查看服务状态;
系统启动Nginx后,报 [emerg] bind() to 0.0.0.0:XXXX failed (13: Permission denied)错误的处理方式,分为两种
1024以下端口启动时需要root权限,sudo systemctl start nginx即可
5、重启脚本
三、项目代码
app.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/home")
def home():
return render_template("home.html")
@app.route("/index")
def index():
return "success"
if __name__ == "__main__":
app.run()