(阿里雲)centos7下Django2.0服務器環境零基礎完全部署(簡化版)

服務器環境:
系統:Centos7.4 64bit
其他:Python3.6+virtualenv+MySQL5.7+Django2+uwsgi+Nginx+Supervisor+Memcached+redis

更改yum源鏡像(非必需,阿里雲服務器已更改好)
cd /etc/yum.repos.d
mv CentOS-Base.repo CentOS-Base.repo.bak
wget http://mirrors.aliyun.com/repo/Centos-7.repo
mv Centos-7.repo CentOS-Base.repo
yum clean all && yum makecache && yum update

pip3的源鏡像修改(阿里雲已生成好)
mkdir ~/.pip
vim ~/.pip/pip.conf

[global]
index-url = https://pypi.doubanio.com/simple/
[install]
trusted-host = pypi.doubanio.com
timeout = 6000

簡化安裝版本(阿里雲)
安裝python3
yum install epel-release -y
yum install https://centos7.iuscommunity.org/ius-release.rpm -y
yum install python36u -y
yum install python36u-devel -y
pip3 install --upgrade pip (建議關閉窗口或重啓一下)
pip3 --version

安裝`virtualenv
pip3 install virtualenv
pip3 install virtualenvwrapper
whereis virtualenvwrapper.sh
vim ~/.bashrc (尾部添加)
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export WORKON_HOME=$HOME/.virtualenvs 
source /usr/local/bin/virtualenvwrapper.sh
source ~/.bashrc

安裝Git
yum install -y git

安裝mysql
卸載原有數據庫rpm -qa | grep mariadb
yum remove mariadb-libs-5.5.56-2.el7.x86_64
cd /usr/local/src
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql-community-server

systemctl start mysqld 
systemctl status mysqld
grep "password" /var/log/mysqld.log
mysql -u root -p

重置密碼:
set global validate_password_policy=0;
set global validate_password_length=1;

alter user 'root'@'localhost' identified by 'django12345'; 
flush privileges;
CREATE USER 'root'@'%' IDENTIFIED BY "django12345";
grant all privileges on *.* to root @"%" identified by "django12345" WITH GRANT OPTION; 
flush privileges;

systemctl restart mysqld
systemctl status mysqld

vim /etc/my.cnf
# 修改字符編碼爲utf8
character_set_server = utf8
init_connect = 'SET NAMES utf8'

# 如果不需要密碼策略,禁用密碼策略
validate_password = off

解決安裝mysqlclient報錯:
yum install gcc mariadb-devel

安裝memcached 和redis
yum install libevent libevent-devel
yum install memcached
systemctl start memcached
systemctl enable memcached
systemctl status memcached

yum install redis
systemctl start redis
systemctl enable redis
ps -ef | grep redis

本地生成需使用的pip環境文件:(cmd 在項目目錄下)
pip3 freeze > requirements.txt
修改setting文件
訪問域名或IP 以及數據庫的信息

(本地Git命令)
全局設置:
git config --global user.name "xxxx"
git config --global user.email "[email protected]"

創建git倉庫
git init
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin https://gitee.com/xxx/xxxx.git
git push -u origin master


進入虛擬環境:創建虛擬環境和文件夾:mkvirtualenv djangocs     mkdir djangocs1
git init
git remote add origin https://gitee.com/xxx/xxx.git
git pull origin master
輸入Gitee用戶和密碼:(不能輸錯,檢查輸入法)

pip3 install -r requirements.txt

python3 manage.py migrate

# 服務器manage.py文件的第一行代碼應修改爲:
#!/root/.virtualenvs/djangocs/bin/python3.6
python3 manage.py runserver 0.0.0.0:80 

收集靜態文件:
python3 manage.py collectstatic

退出虛擬環境   需要全局安裝:
pip3 install uwsgi
cd ~
cd .virtualenvs/djangocs/
pwd
/root/.virtualenvs/djangocs

cd /srv/djangocs1/
uwsgi --http :80 --module djangocs1.wsgi --virtualenv=/root/.virtualenvs/djangocs

在項目目錄下創建djangocs1_uwsgi.ini
vim djangocs1_uwsgi.ini  配置文件
[uwsgi]
#使用nginx連接時使用sock方式同機速度更快
socket=/srv/djangocs1/djangocs1.sock
#socket=172.16.0.4:9000
#不用nginx直接當做web服務器使用
#http=0.0.0.0:80
# Django相關1的配置
# 必須全部爲絕對路徑
# 項目的路徑
chdir=/srv/djangocs1
#wsgi文件路徑,在項目底下
wsgi-file=/srv/djangocs1/wsgi.py
# Django的wsgi文件
module= djangocs1.wsgi:application
# Python虛擬環境的路徑
home=/root/.virtualenvs/djangocs

# 進程相關的設置
# 主進程
master=true
# 最大數量的工作進程
processes=4
threads=2
pidfile=/srv/djangocs1/uwsgi.pid
stats=/srv/djangocs1/uwsgi.status
# socket文件路徑,絕對路徑
# socket=/srv/djangocs1/djangocs1.sock
# 設置socket的權限
chmod-socket=666
# 退出的時候是否清理環境
vacuum=true

*************************************************
cd /
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum search nginx
yum install -y nginx
systemctl stop firewalld
cd /etc/nginx/conf.d
新建 djangocs1.conf (vim touch)

upstream djangocs1 {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
#server 172.16.0.4:9000; # uwsgi的端口
server unix:///srv/djangocs1/djangocs1.sock;
}
# configuration of the server

error_log  /home/wwwroot/nginxerror.log;#錯誤日誌
server {
# the port your site will be served on
listen      80;
# 端口
server_name xx.xx.xx.xx; # 服務器ip或者域名
charset     utf-8;

# max upload size
client_max_body_size 75M;   # adjust to taste


# Django media
location /media  {
    alias  /srv/djangocs1/media;  # 指向django的media目錄
}

# Django static
location /static  {
    alias  /srv/djangocs1/static_dist;  # 指向django的static目錄
}

# Finally, send all non-media requests to the Django server.
location / {
    uwsgi_pass  djangocs1;
    include     /etc/nginx/uwsgi_params; # uwsgi服務
}
}
*******************************************************************
錯誤日誌的路徑需要先建立好 /home/wwwroot/nginxerror.log
mkdir /home/wwwroot
cd /lib/systemd/system/
vim nginx.service (阿里雲已經配置好了 )
systemctl enable nginx.service
systemctl start nginx
systemctl status nginx (查看狀態)
systemctl restart nginx
在項目路徑下  重啓 uwsgi
uwsgi --ini djangocs1_uwsgi.ini

pip3 install supervisor
在項目路徑下創建
vim djangocs1_supervisor.conf
**********************************************

# upervisor的程序名字
[program:djangocs1]
# supervisor執行的命令
command = uwsgi --ini djangocs1_uwsgi.ini
# 項目的目錄
directory = /srv/djangocs1
# 開始的時候等待多少秒
startsecs=0
# 停止的時候等待多少秒
stopwaitsecs=0
# 自動開始
autostart=true
# 程序掛了後自動重啓
autorestart=true
# 輸出的log文件
stdout_logfile=/srv/djangocs1/log/supervisord.log
# 輸出的錯誤文件
stderr_logfile=/srv/djangocs1/log/supervisord.err

[supervisord]
# log的級別
loglevel=info

# 使用supervisorctl的配置
[supervisorctl]
# # 使用supervisorctl登錄的地址和端口號
serverurl = unix:///srv/djangocs1/djangocs1_supervisorctl.sock
# # serverurl = http://127.0.0.1:9001

[unix_http_server]
file=/srv/djangocs1/djangocs1_supervisorctl.sock

[inet_http_server]
# supervisor的服務器
port = 127.0.0.1:9001
# 用戶名和密碼
username = djangoroot
password = django12345

# 登錄supervisorctl的用戶名和密碼
username = djangoroot
password = django12345

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

**********************************************************************************************
在djangocs1下建立log文件夾

運行supervisord -c djangocs1_supervisor.conf
進入管理平臺
supervisorctl -c djangocs1_supervisor.conf

平臺管理指令:
   * status                # 查看狀態
    * start program_name    #啓動程序
    * restart program_name  #重新啓動程序
    * stop program_name     # 關閉程序
    * reload                # 重新加載配置文件
    * quit                  # 退出控制檯

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章