赞
踩
Ubuntu 18.04
Python 3.9
MySQL 5.6
Python
版本:python -V
python3 -V
Python 3.9
,bash
中输入python
并按两下Tab
键可以看到所有Python
版本。sudo apt update
sudo apt install software-properties-common -y
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.9 -y
Python 3.9
设为默认版本sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
sudo update-alternatives --config python3
运行上面的指令结果如下:
There are 2 choices for the alternative python3 (providing /usr/bin/python3).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/python3.9 2 auto mode
1 /usr/bin/python3.6 1 manual mode
2 /usr/bin/python3.9 2 manual mode
pip
:sudo apt update
sudo apt install python3-pip -y
sudo apt install python3.9-venv -y
使用pip3 -V
查看是否安装成功。
vitualenv
:sudo apt-get install python-virtualenv -y
mkdir env
cd env
virtualenv -p /usr/bin/python3.9 py39env
sudo vim ~/.bashrc
# 在~/.bashrc末尾添加
source /root/env/py39env/bin/activate
source ~/.bashrc
虚拟环境激活后bash
前面有(py39env
)标示,后面内容都在该环境下进行。
sudo apt-get install build-essential python-dev -y
sudo apt-get install python3.9-dev -y
cd ~
mkdir uwsgi
cd uwsgi
wget http://projects.unbit.it/downloads/uwsgi-latest.tar.gz
tar -zxvf uwsgi-latest.tar.gz
cd uwsgi-2.0.20/
sudo python3 uwsgiconfig.py --build
make
sudo ln -s /root/uwsgi/uwsgi-2.0.20/uwsgi /usr/bin/uwsgi
测试:
cd ~
uwsgi
如果安装成功则结果如下:
*** Starting uWSGI 2.0.20 (64bit) on [Thu Jun 16 10:28:37 2022] *** compiled with version: 7.5.0 on 16 June 2022 07:26:14 os: Linux-4.4.0 #1 SMP Tue Aug 25 11:59:26 MSK 2020 nodename: 158t.c.hostens.cloud machine: x86_64 clock source: unix detected number of CPU cores: 1 current working directory: /root detected binary path: /root/uwsgi/uwsgi-2.0.20/uwsgi !!! no internal routing support, rebuild with pcre support !!! uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** *** WARNING: you are running uWSGI without its master process manager *** your processes number limit is 62987 your memory page size is 4096 bytes detected max file descriptor number: 1024 lock engine: pthread robust mutexes thunder lock: disabled (you can enable it with --thunder-lock) uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** The -s/--socket option is missing and stdin is not a socket.
sudo apt-get install git -y
pip3
安装项目所需第三方依赖)git clone <你的项目地址>
进入项目根目录,创建uwsgi.ini
文件:
vim uwsgi.ini
文件中输入以下内容后保存(注意删除其中的中文注释):
[uwsgi] #ip和端口要与Nginx配置文件一致 socket=127.0.0.1:5000 #项目的根目录 chdir=<你的项目根目录> #启动文件,注意这里是相对项目根目录的地址。 wsgi-file=<你的启动文件> #启动文件实例的名称 callable=app processes=4 threads=10 master=true virtualenv=/root/env/py39env #启用多线程 enable-threads=true preload=true lazy-apps=true
sudo apt-get install nginx -y
# 进入到nginx配置文件的目录
cd /etc/nginx
# 修改配置文件,注意先对nginx.conf进行备份
cp nginx.conf nginx.conf.bak
vim nginx.conf
需要找到或在http{}
中添加server{}
部分并修改,为了能修改项目根目录,还需要把开始的user
值www-data
改为root
。配置文件可以通过nginx -t
找到。
server {
listen 80;
server_name xxx.xxx.xxx.xxx; #服务器外网的ip地址, 也可以是你的域名,或者两者都有,用空格隔开
root <与uwsgi中的项目根目录一致>;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:5000; #这里与uwsgi配置文件里的socket保持一致
}
}
使用nginx -t
检查是否有问题,尝试启动nginx
。(启动报错请先执行关闭再启动。)
nginx #启动nginx
pkill -9 nginx #关闭nginx服务
sudo apt-get install ufw
ufw enable
ufw allow 80
ufw allow 22 #记得开放SSH端口
ufw reload
ufw status
sudo apt-get update
sudo apt-get install mysql-server -y
sudo apt-get install mysql-client -y
sudo mysql_secure_installation
配置时推荐选项(参考https://blog.csdn.net/weixx3/article/details/80782479):
#1 VALIDATE PASSWORD PLUGIN can be used to test passwords... Press y|Y for Yes, any other key for No: N (我的选项) #2 Please set the password for root here... New password: (输入密码) Re-enter new password: (重复输入) #3 By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them... Remove anonymous users? (Press y|Y for Yes, any other key for No) : N (我的选项) #4 Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network... Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y (我的选项) #5 By default, MySQL comes with a database named 'test' that anyone can access... Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N (我的选项) #6 Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y (我的选项)
检查MySQL服务运行状态用systemctl status mysql.service
指令。
vim /etc/mysql/mysql.conf.d/mysqld.cnf
# 将bind-address改为0.0.0.0
bind-address = 0.0.0.0
输入mysql -u root -p
后进入mysql,再依次输入:
GRANT ALL PRIVILEGES ON *.* TO root@localhost IDENTIFIED BY "<你的密码>";
use mysql;
update user set host = '%' where user = 'root';
如果还存在问题可以参考https://blog.csdn.net/weixx3/article/details/80782479和https://blog.csdn.net/weixin_46765649/article/details/123754890
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
在[mysqld]
下增加:
character-set-server=utf8
然后:
sudo vi /etc/mysql/conf.d/mysql.cnf
在[mysql]
下增加:
default-character-set=utf8
最后重启mysql
:
/etc/init.d/mysql restart
Flask
的依赖可通过pip3
进行安装,需要使用export
设置环境变量FLASK_APP
,即export FLASK_APP=xxx.py
。在启动之前需要创建数据库并初始化各表。
为了能在后台运行,需要安装screen
:
sudo apt-get install screen -y
使用指令创建后台bash如下:
screen -S <后台名称>
启动nginx后启动uwsgi:
pkill -9 nginx
pkill -9 uwsgi
nginx
uwsgi /root/<你的项目地址>/uwsgi.ini
参考链接:
https://blog.csdn.net/xujianjun229/article/details/118500154
https://blog.csdn.net/G_D_L_Dupai/article/details/122498187
http://t.zoukankan.com/GavinSimons-p-10775293.html
https://blog.csdn.net/trium_KW/article/details/124849163
https://zhuanlan.zhihu.com/p/435731668
https://www.runoob.com/python3/python-uwsgi.html
https://blog.csdn.net/weixx3/article/details/80782479
https://blog.csdn.net/tanhuanzheng/article/details/108960426
https://blog.csdn.net/weixin_46765649/article/details/123754890
https://blog.csdn.net/qq_29761395/article/details/104076771
https://blog.csdn.net/weixin_39808877/article/details/111010702
https://blog.csdn.net/qq_38613380/article/details/111315277
https://www.likecs.com/show-203364581.html
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。