django部署

部署

項目不要放在/root目錄下
全新的centos7,阿里雲
html

yum -y update

yum install gcc

yum -y install zlib*

yum install openssl-devel -y

這裏先配置一下安全組
python

ssh安裝

不安裝也能夠,須要配置密匙對
登陸時須要使用pem文件+密碼的方式進行登錄mysql

yum install openssh-server -y

service sshd restart

#服務端開啓密碼驗證的方法:

vim /etc/ssh/sshd_config
(最後一行)
把PasswordAuthentication項爲yes

重啓服務

service sshd restart

mysql安裝(5.7.23)

安裝
下載rpm安裝源
官方地址:https://dev.mysql.com/downloads/repo/yum/
rpm文件地址:https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
[root@GYJ ~]# mkdir download
[root@GYJ ~]# cd download/
[root@GYJ download]# wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
--2018-09-30 17:35:27--  https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
Resolving dev.mysql.com (dev.mysql.com)... 137.254.60.11
Connecting to dev.mysql.com (dev.mysql.com)|137.254.60.11|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm [following]
--2018-09-30 17:35:29--  https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
Resolving repo.mysql.com (repo.mysql.com)... 23.44.160.128
Connecting to repo.mysql.com (repo.mysql.com)|23.44.160.128|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 25680 (25K) [application/x-redhat-package-manager]
Saving to: ‘mysql57-community-release-el7-11.noarch.rpm’

100%[====================================================================================>] 25,680      --.-K/s   in 0.1s    

2018-09-30 17:35:30 (194 KB/s) - ‘mysql57-community-release-el7-11.noarch.rpm’ saved [25680/25680]

安裝Mysql源文件nginx

yum localinstall -y mysql57-community-release-el7-11.noarch.rpm

查看MySQL源是否安裝成功git

[root@GYJ download]# yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64 MySQL Connectors Community                  65
mysql-tools-community/x86_64      MySQL Tools Community                       69
mysql57-community/x86_64          MySQL 5.7 Community Server                 287
[root@GYJ download]#

安裝MySQL服務github

yum install -y mysql-community-server
啓動, 重啓
systemctl start mysqld
systemctl restart mysqld
修改root登陸密碼
獲取root默認密碼(因爲Mysql安全策略升級,安裝完成後系統自動設置了一個隨機密碼)
[root@GYJ download]# grep 'temporary password' /var/log/mysqld.log
2018-09-30T09:39:46.046400Z 1 [Note] A temporary password is generated for root@localhost: CRdwG_iRw58q
使用生成的隨機密碼登錄MySQL
[root@GYJ download]# mysql -uroot -pCRdwG_iRw58q
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.23

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
修改密碼
因爲Mysql默認要求設置密碼複雜度高(必須包含 大小寫字母、數字、符號)
mysql> alter user 'root'@'localhost' identified by '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql>

這樣設置是合法的:web

mysql> alter user 'root'@'localhost' identified by 'Mysql666!';
Query OK, 0 rows affected (0.00 sec)

mysql>

關閉Mysql密碼校驗規則,容許設置簡單密碼
在Mysql配置文件最後加入:validate_password = offsql

[root@localhost ~]# vi /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

validate_password = off

重啓Mysql服務生效shell

systemctl restart mysqld
配置遠程用戶登陸

指定Ipdjango

mysql> grant all privileges on *.* to 'root'@'192.168.1.1' identified by '123456' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql>

容許全部

mysql> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql>
FLUSH PRIVILEGES;
設置開機啓動
systemctl enable mysqld

 systemctl daemon-reload
安裝mysqlclient出問題
centos 7:
        yum install python-devel mariadb-devel -y

    ubuntu:
        sudo apt-get install libmysqlclient-dev

    而後:
        pip install mysqlclient

設置mysql密碼(能夠跳過了,使用123456密碼能夠登錄了)

進入mysql:
    set password =password('123456');     #密碼123456
    FLUSH PRIVILEGES;

安裝python3.6

1. 獲取

wget https://www.python.org/ftp/python/3.6.5/Python-3.6.2.tgz
tar -xzvf Python-3.6.2.tgz -C  /tmp
cd  /tmp/Python-3.6.2/

2. 把Python3.6安裝到 /usr/local 目錄

./configure --prefix=/usr/local
make
make altinstall

3. 更改/usr/bin/python連接

ln -s /usr/local/bin/python3.6 /usr/bin/python3

安裝virtualenvwrapper

yum install python-setuptools python-devel
pip install virtualenvwrapper

#編輯.bashrc文件
vim ~/.bashrc

#添加進去
export WORKON_HOME=$HOME/.virtualenvs
source /usr/bin/virtualenvwrapper.sh

#sudo find / -name virtualenvwrapper.sh      查看你的virtualenvwrapper.sh在什麼地方

#從新加載.bashrc文件
source ~/.bashrc

#虛擬環境保存的路徑
cd ~/.virtualenvs/      (建立的虛擬環境都會保存在這個目錄,前面設置的)

#建立指定python版本的虛擬環境方法
mkvirtualenv -p /usr/local/bin/python3.6 MxOnline

workon MxShop

#進虛擬環境安裝依賴包

首先 pip freeze > requirements.txt 將本地的虛擬環境安裝包導出來,上傳到服務器

pip install -r requirements.txt

虛擬環境目錄: /root/.virtualenvs/
能夠經過pycharm遠程調試

git安裝(也能夠直接從本地經過PyCharm的部署功能直接上傳到服務器)

yum install git

git config --global user.name "Your Name"
git config --global user.email "youremail@domain.com"

cd ~/ && ssh-keygen -t rsa -C "你的郵箱"

#提示的信息,直接按enter就行

cd .ssh

把公鑰文件(id_rsa.pub)中的碼複製到github

# 就能夠開始clone代碼了
git@github.com:coder-gao/MxOnline.git

拉取全部須要的static file 到同一個目錄

在django的setting文件中,添加下面一行內容:

STATIC_ROOT = os.path.join(BASE_DIR, "static")

運行命令

python manage.py collectstatic

uwsgi的安裝和配置

進入虛擬環境,安裝uwsgi

workon MxOnline
    pip install uwsgi

在項目根目錄下新建uwsgi.ini 配置文件, 內容以下:

# mysite_uwsgi.ini file
    [uwsgi]

    # Django-related settings
    # the base directory (full path)
    chdir           = /PyProject/MxOnline
    # Django's wsgi file
    module          = MxOnline.wsgi
    # the virtualenv (full path)
    virtualenv = /root/.virtualenvs/MxOnline

    # process-related settings
    # master
    master          = true
    # maximum number of worker processes
    processes       = 5
    # the socket (use the full path to be safe
    socket          = /PyProject/MxOnline/mxonline.sock
    # ... with appropriate permissions - may be needed
    chmod-socket    = 777
    # try to remove all of the generated file/sockets
    vacuum          = true

    logto = /tmp/MxOnline.log

注:
chdir: 表示須要操做的目錄,也就是項目的目錄
module: wsgi文件的路徑
processes: 進程數
virtualenv:虛擬環境的目錄
socket: 能夠是127.0.0.1:8000,也能夠是sock文件;若是是文件,和nginx通訊時會自動建立
chmod-socket:
權限,若是權限不夠會顯示502
若是使用的不是sock文件通訊的話,這裏能夠不設置

nginx的安裝和配置

安裝

yum install nginx

/etc/nginx/conf.d下新建MxOnline.conf

# the upstream component nginx needs to connect to
upstream MxOnline {
server unix:///PyProject/MxOnline/mxonline.sock; # for a file socket
# server 127.0.0.1:8000; # for a web port socket (we'll use this first)
}
# configuration of the server

server {
# the port your site will be served on
listen      80;
# the domain name it will serve for
# server_name mxonline.cht3.top; 有域名的話能夠直接填域名,沒有的話直接寫ip
server_name xxx.xx.xx.xx ; # substitute your machine's IP address or FQDN
charset     utf-8;

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

# Django media
location /media  {
    alias /PyProject/MxOnline/media;  # 指向django的media目錄
}

location /static {
    alias /PyProject/MxOnline/static; # 指向django的static目錄
}

# Finally, send all non-media requests to the Django server.
location / {
    uwsgi_pass  MxOnline;
    include     uwsgi_params; # the uwsgi_params file you installed
}
}

重啓nginx,
systemctl restart nginx
進入虛擬環境,
workon MxOnline
,進入項目根路徑,執行
uwsgi --ini uwsgi.ini
打開瀏覽器輸入服務器的IP地址,就能夠看到網站的頁面了
這裏推薦一些域名交易網站(域名已備案的那種)
好名網
聚名網
配置好解析後在配置nginx,
重啓nginx,瀏覽器輸入域名就可訪問了

經過systemctl status nginx.service能夠查看啓動失敗的緣由

阿里雲要求服務器信息和域名信息一致才行
這裏不使用80,在安全組中添加8081端口
nginx中也再也不監聽80端口,而是監聽8081端口

listen      8081;
# the domain name it will serve for
server_name mxonline.cht3.top:8081 mxonline.iceflower.top:8081;

在線教育

supervisor管理uwsgi

在系統環境,不是虛擬環境,安裝supervisor
supervisor不支持3.x,系統的環境是2.7

pip install supervisor
### 生成配置文件,且放在/etc目錄下
echo_supervisord_conf > /etc/supervisord.conf

### 爲了避免將全部新增配置信息全寫在一個配置文件裏,這裏新建一個文件夾,每一個程序設置一個配置文件,相互隔離
mkdir /etc/supervisord.d/  

### 修改配置文件
vim /etc/supervisord.conf

### 加入如下配置信息
[include]
files = /etc/supervisord.d/*.conf

### 在supervisord.conf中設置經過web能夠查看管理的進程,加入如下代碼(默認即有,取消註釋便可)    
[inet_http_server] 
port=9001
username=user      
password=123


### 啓動supervisord

supervisord -c /etc/supervisord.conf

如今經過 http://ip:9001/ 就能夠查看supervisor的web界面了(默認用戶名及密碼是user和123),固然目前尚未加入任何監控的程序。

/etc/supervisord.d/目錄下新建一個MxOnline.conf的文件,內容以下

# 程序的名字,在supervisor中能夠用這個名字來管理該程序,隨便起
[program:MxOnline]
# 指定運行用戶
user = root
# 啓動程序的命令
command = /root/.virtualenvs/MxOnline/bin/uwsgi --ini uwsgi.ini
# 項目的目錄
directory = /PyProject/MxOnline
# 開始的時候等待多少秒
startsecs = 0
# 中止的時候等待多少秒
stopwaitsecs = 0
# 設置改程序是否雖supervisor的啓動而啓動
autorstart = true
# 程序掛了是否須要從新將其啓動
autorestart = true
# 是否將程序錯誤信息重定向到文件
redirect_stderr=true
# 輸出的log文件(log目錄須要手動建立)
stdout_logfile = /PyProject/MxOnline/log/supervisord.log
# 輸出的錯誤文件(log目錄須要手動建立)
stderr_logfile = /PyProject/MxOnline/log/supervisord.err

[supervisord]
# log的級別
loglevel = info

忽視這段代碼

# 使用supervisorctl的配置
[supervisorctl]
# 使用supervisorctl登錄的地址和端口
serverurl = http://127.0.0.1:9001
# 登錄supervisorctl的用戶名和密碼
username = admin
password = admin123

# supervisor的服務器,能夠經過瀏覽器進入
[inet_http_server]
port=0.0.0.0:9001
username=admin
password=admin123

從新啓動supervisord,或者從新加載配置文件:

supervisorctl reload
### 或者
supervisorctl -c /etc/supervisord.conf

打開瀏覽器,輸入IP:9001
輸入帳號和密碼
便可看到supervisor的web管理界面

阿里雲的服務器25端口被封了,郵件沒法發送,只能使用465端口了
協議也要使用SSL了

# 發送郵件的setting設置
EMAIL_HOST = "smtp.aliyun.com"
EMAIL_PORT = 465
EMAIL_HOST_USER = "xxxxxx@aliyun.com"
EMAIL_HOST_PASSWORD = "xxxxxx"
EMAIL_USE_SSL= True
EMAIL_FROM = "xxxx@aliyun.com"

服務器重啓後查看MySQL和nginx是否從新啓動
沒有譜的話須要重啓

修改系統python版本

安裝的python3存放位置

[root@gyj bin]# cd /usr/local/bin/
[root@gyj bin]# ls
2to3-3.6  easy_install-3.6  idle3.6  pip3.6  pydoc3.6  python3.6  python3.6m  python3.6m-config  pyvenv-3.6

建立python3的連接

[root@gyj bin]# ln -s /usr/local/bin/python3.6 /usr/bin/python
ln: failed to create symbolic link ‘/usr/bin/python’: File exists
[root@gyj bin]# pwd
/usr/local/bin
[root@gyj bin]# cd /usr/bin/
[root@gyj bin]# mv python python.back
[root@gyj bin]# ln -s /usr/local/bin/python3.6 /usr/bin/python
[root@gyj bin]# ln -s /usr/local/bin/python3.6 /usr/bin/python3
ln: failed to create symbolic link ‘/usr/bin/python3’: File exists

修改使用系統python的文件,將裏面的python改成python2

[root@gyj bin]# cd /usr/bin/
[root@gyj bin]# ls yum*
yum  yum-builddep  yum-config-manager  yum-debug-dump  yum-debug-restore  yumdownloader  yum-groups-manager
[root@gyj bin]# vim yum
[root@gyj bin]# vim yum-builddep 
[root@gyj bin]# vim yum-config-manager 
[root@gyj bin]# vim yum-debug-dump 
[root@gyj bin]# vim yum-debug-restore 
[root@gyj bin]# vim yumdownloader 
[root@gyj bin]# vim yum-groups-manager 
[root@gyj bin]# vim /usr/libexec/urlgrabber-ext-down
相關文章
相關標籤/搜索