部署项目,假设项目名称为My_Blog
1 安装python
-
先安装gcc,用于将python源码编译
yum install gcc -y
-
安装python依赖
yum install zlib zlib-devel -y yum install bzip2 bzip2-devel -y yum install ncurses ncurses-devel -y yum install readline readline-devel -y yum install openssl openssl-devel -y yum install xz lzma xz-devel -y yum install sqlite sqlite-devel -y yum install gdbm gdbm-devel -y yum install tk tk-devel -y yum install mysql-devel -y yum install python-devel -y yum install libffi-devel -y
-
使用wget下载python3.9 wget https://www.python.org/ftp/python/3.9.5/Python-3.9.5rc2.tgz
或者下载到本地后再上传到服务器
-
解压 tar -xvf Python-3.9.5.tgz,注意要进入对应目录
-
进入cd Python-3.9.5目录,分别执行 ./configure make all make install三条命令
-
配置清华源
pip3.9 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
-
安装virtualenv
pip3.9 install virtualenv
-
创建envs目录,并在envs目录下创建虚拟环境virtualenv /envs/django --python=python3.9
-
激活虚拟环境source /envs/django/bin/activate
-
安装django依赖包
pip3.9 install django pip3.9 install django-haystack pip3.9 install django-ckeditor==6.6.1 pip3.9 install whoosh pip3.9 install jieba pip3.9 install mysqlclient # 注意如果使用的是mariadb数据库,mysqlclient可能无法安装,故本教程使用mysql数据库 # 解决方案见下面的链接 pip3.9 install pymysql pip3.9 install six pip3.9 install django_redis pip3.9 install markdown
-
参考链接
2 数据库选择和安装
mysql和mariadb(我们接下来使用mysql,mariadb会存在很多问题)
-
下载官方mysql包 wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
-
安装mysql包 yum -y install mysql57-community-release-el7-10.noarch.rpm
-
安装 MySQL yum -y install mysql-community-server --nogpgcheck
yum -y install mysql-community-server 直接运行这条命令可能会有如下报错,是由于由于GPG密钥验证问题引起的,所以直接使用上一条命令。
The GPG keys listed for the "MySQL 8.0 Community Server" repository are already installed but they are not correct for this package. Check that the correct key URLs are configured for this repository. Failing package is: mysql-community-server-8.0.34-1.el7.x86_64 GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
-
启动mysql服务 systemctl start mysqld.service 如果没有报错,进入下一步
如果执行报错,多半是没有权限,执行下面语句赋予权限,然后重试
chown mysql:mysql -R /var/lib/mysql
-
查看初始密码 grep ‘password’ /var/log/mysqld.log
-
进入mysql mysql -u root -p 这里输入刚才的密码,如果没有密码,直接回车即可。
-
如果显示数据库无法访问 修改MySql配置文件my.cnf,新增skip-grant-tables
-
修改密码 set password = password(‘新密码’)
-
退出后重新启动mysql服务,service mysqld start
-
参考链接
3 安装uwsgi
-
uwsgi(用于接收用户请求,提高性能)
-
激活刚才创建的虚拟环境django,然后使用pip install uwsgi安装uwsgi
-
基于uwsgi运行项目
-
基于命令行的方式 uwsgi --http :8080 --wsgi-file app.py --callable app
-
基于配置文件的方式(推荐)创建一个 myblog_uwsgi.ini的文件,文件名字是随便写的。将文件放在与manage.py同一个目录下,文件内容如下:
[uwsgi] #使用 Nginx 连接时使用,Django程序所在服务器地址 socket=127.0.0.1:8001 #项目目录 chdir=/data/My_Blog #项目中wsgi.py文件的目录,相对于项目目录 wsgi-file=My_Blog/wsgi.py # 进程数 processes=2 # 线程数 threads=1 # uwsgi服务器的角色 master=True # 存放进程编号的文件 pidfile=uwsgi.pid # 日志文件,因为uwsgi可以脱离终端在后台运行,日志看不见。我们以前的runserver是依赖终端的 daemonize=uwsgi.log # 指定依赖的虚拟环境, 路径到虚拟环境的名字即可(可以命令查看 which python) virtualenv=/envs/django
-
-
之后启动uswgi uwsgi --ini myblog_uwsgi.ini
-
停止uwsgi: uwsgi --stop uwsgi.pid
安装nginx
-
yum install nginx -y
-
配置nginx,(nginx默认的配置文件目录在 /etc/nginx 里面,找到nginx.conf文件,并将该文件覆盖)
user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; use epoll; multi_accept on; } 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 4096; include /etc/nginx/mime.types; default_type application/octet-stream; gzip on; gzip_min_length 1k; gzip_types text/plain text/css application/json application/javascript application/xml; upstream django{ server 127.0.0.1:8001; } server { listen 80; listen [::]:80; location /static { alias /data/My_Blog/static; } location /{ uwsgi_pass django; include uwsgi_params; } } }
-
启动nginx
-
临时启动 systemctl start nginx 临时停止 systemctl stop nginx 临时重启 systemctl restart nginx
-
配置开机自启动:systemctl enable nginx
-
4 启动项目
-
首先进入项目文件夹
-
项目相关的命令
- 生成数据库迁移文件:python manage.py makemigrations, python manage.py migrate
- 全站搜索功能执行以下命令进行分词 python manage.py rebuild_index
- 创建django超级用户 python manage.py createsuperuser
-
启动项目 python manage.py runserver
5 总结以后可能的常用命令
-
首先激活虚拟环境source /envs/django/bin/activate
-
进入项目文件夹 cd /data/My_Blog
-
启动nginx 如果没有设置开机自启动,systemctl enable nginx
-
启动uwsgi uwsgi --ini myblog_uwsgi.ini
-
总结一些功能
nginx用于挡在最外面,它监听http协议的80端口,如果是静态文件则自己处理,如果是python代码则转发给uwsgi。