1. 安裝Python3.8
1.1 安裝相關依賴包
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
項目python版本為3.8.5, django版本為3.1.1
1.2 下載并解壓python3.8.5
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
1.3 編譯安裝
cd Python-3.8.5/./configure --prefix=/usr/local/python3makemake install
1.4 創建軟鏈接
ln -s /usr/local/python3/bin/python3.8 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3.8 /usr/bin/pip3
2. 安裝項目相關庫
2.1 項目中所使用的庫可以導出到文本文件中
# 從開發環境中導出所安裝的庫到requirements.txt文件中
pip freeze > requirements.txt
2.2 在服務器中安裝requirements.txt中的庫
pip install -r requirements.txt
2.3 安裝uwsgi
pip install uwsgi
3. 安裝MySQL
3.1 下載
wget http://tel.mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.34-el7-x86_64.tar.gz
3.2 拷貝安裝包到/usr/local/目錄下,并解壓
# 重命名壓縮包
mv mysql-5.7.34-el7-x86_64.tar.gz mysql.tar.gztar zxf mysql.tar.gz
# 進入mysql目錄
cd mysql
3.3 創建data目錄
cd mysqlmkdir data
3.4 創建mysql用戶并設置權限
useradd mysql -s /sbin/nologin -M
# 回到/usr/local目錄cd ..
# 設置mysql目錄權限
chown -R mysql.mysql mysql
3.5 初始化數據庫
cd mysql
# 執行后,屏幕會提示root臨時密碼,需要記錄下來
bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
- 這一步可能會發生錯誤
bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
- 解決
yum install libaio
3.6 編輯my.cnf文件
wget https://www.python.org/ftp/python/3.8.5/Python-3.8.5.tgztar zvxf Python-3.8.5.tgz
3.7 創建error.log
cd Python-3.8.5/./configure --prefix=/usr/local/python3makemake install
3.8 拷貝啟動文件到/etc/init.d目錄
ln -s /usr/local/python3/bin/python3.8 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3.8 /usr/bin/pip3
3.8.1 修改啟動文件中的路徑
# vim /etc/init.d/mysqld
# 修改basedir和datadir
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
3.9 啟動mysql
/etc/init.d/mysqld start
3.10 創建軟鏈接
ln -s /usr/local/mysql/mysql.sock /tmp/mysql.sock
3.11 使用臨時密碼登錄數據庫并重新為root設置密碼
cd /usr/local/mysql
# 回車后會提示輸入密碼,輸入臨時密碼回車即可
bin/mysql -uroot -p
# 登錄之后修改密碼
mysql> set password = password('123456');
mysql> alter user 'root'@'localhost' password expire never;
mysql> flush privileges;
4. 安裝Nginx
4.1 下載
wget http://nginx.org/download/nginx-1.17.7.tar.gz
4.2 安裝相關依賴包
yum install -y pcre pcre-devel openssl openssl-devel gcc
4.3 創建nginx用戶
useradd nginx -s /sbin/nologin -M
4.4 安裝nginx
tar zxf nginx-1.17.7.tar.gz
cd nginx-1.17.7
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
5. 安裝redis
5.1 安裝
wget http://download.redis.io/releases/redis-2.8.17.tar.gz
tar xzf redis-2.8.17.tar.gz
mv redis-2.8.17 redis
cd redis
make
5.2 啟動
cd src
./redis.server
cd src
./redis.server
6. uwsgi配置
# 在項目根目錄下創建uwsgi.ini文件
[uwsgi]
# 配置nginx連接的socket連接 /data/web/project為項目目錄
socket=/data/web/project/project.sock
# 配置uwsgi.pid所在的目錄
pidfile=/data/web/project/uwsgi.pid
# 進程數
processes=8
# 每個進程的線程數
threads=2
# 項目路徑,項目所在的目錄
chdir=/data/web/project
# wsgi接口模塊文件路徑
module=project.wsgi
# 啟動管理主進程
master=true
enable-threads=true
logdate=true
limit-as=6048
# 日志文件
daemonize=/data/web/project/run.log
6.1 啟動
# -d表示后臺啟動
uwsgi -d --ini uwsgi.ini
6.2 相關操作
# 停止
uwsgi --stop uwsgi.pid
# 重啟
uwsgi --reload uwsgi.pid
7. 配置nginx
7.1 nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type Application/octet-stream;
#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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
include vhost/*.conf;
}
7.2 創建vhost目錄
mkdir vhost
cd vhost
7.3 項目配置文件
# vim www.xxx.com.conf
server {
listen 80;
server_name www.xxx.com;
charset utf-8;
client_max_body_size 50M;
location / {
uwsgi_pass unix:/data/web/project/project.sock;
include uwsgi_params;
}
location /static {
alias /data/web/project/static;
}
location /media {
alias /data/web/project/media;
}
}
8. 關于css, js 丟失問題
代碼遷移至服務器前,使用命令 python manage.py collectstatic 收集靜態文件
python manage.py collectstatic