1.准备工作
1)安装操作系统:Centos7.3
2)关闭防火墙
[root@ITCMDB ~]# systemctl stop firewalld
3)在开发环境上收集项目所有包
(mycmdbtest) C:\Users\surface>pip freeze > plist.txt
diff-match-patch==20121119
Django==2.0.1
django-crispy-forms==1.7.2
django-formtools==2.1
django-import-export==1.0.1
et-xmlfile==1.0.1
future==0.16.0
httplib2==0.11.3
jdcal==1.4
mysqlclient==1.3.13
odfpy==1.3.6
openpyxl==2.5.4
pytz==2018.5
PyYAML==3.13
six==1.11.0
tablib==0.12.1
unicodecsv==0.14.1
xlrd==1.1.0
xlwt==1.3.0
2.安装nginx
1)编辑repo文件
[root@ITCMDB ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
2)查看
[root@ITCMDB ~]# yum list | grep nginx
3)安装nginx并检查版本
[root@ITCMDB ~]# yum install nginx
[root@ITCMDB ~]# nginx -v
nginx version: nginx/1.14.0
[root@ITCMDB ~]#
3.安装python3.6.5
1)准备工作
安装python3.6可能使用相关软件
[root@ITCMDB /]# yum groupinstall 'Development Tools'
[root@ITCMDB /]# yum install -y ncurses-libs zlib-devel mysql-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
下载python3.6.5,并存放在/usr/local目录下
[root@ITCMDB /]# cd /usr/local
[root@ITCMDB local]# wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz
解压下载的压缩包
[root@ITCMDB local]# tar -xzvf Python-3.6.5.tgz -C /temp
[root@ITCMDB local]# cd /temp/Python-3.6.5/
2)安装
--prefix选项是配置安装的路径,执行后所有资源文件放在/usr/local目录
[root@ITCMDB Python-3.6.5]# ./configure --prefix=/usr/local
[root@ITCMDB Python-3.6.5]# make
[root@ITCMDB Python-3.6.5]# make altinstall
3)修改软链接
[root@ITCMDB bin]# mv python python_old
修改之前一定要对以存在的/usr/bin/python更名,否则出现错误
[root@ITCMDB bin]# ln -s /usr/local/bin/python3.6 /usr/bin/python
[root@ITCMDB bin]# ln -s /usr/local/bin/pip3.6 /usr/bin/pip
[root@ITCMDB python3]#
4.安装mysql数据库
1)检查系统是否存在
[root@ITCMDB ~]# rpm -qa | grep mysql
2)安装MySQL源
[root@ITCMDB ~]# yum localinstall http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
3)安装MySQL
yum install mysql-community-server
已安装:
mysql-community-libs.x86_64 0:5.7.22-1.el7 mysql-community-libs-compat.x86_64 0:5.7.22-1.el7
mysql-community-server.x86_64 0:5.7.22-1.el7
作为依赖被安装:
mysql-community-client.x86_64 0:5.7.22-1.el7 mysql-community-common.x86_64 0:5.7.22-1.el7
替代:
mariadb-libs.x86_64 1:5.5.52-1.el7
完毕!
[root@ITCMDB ~]#
4)启动MySQL
[root@ITCMDB ~]# systemctl start mysqld
5)获取密码
[root@ITCMDB ~]# grep 'temporary password' /var/log/mysqld.log
2018-07-26T03:27:04.577803Z 1 [Note] A temporary password is generated for root@localhost: rGauXA8te:>p
[root@ITCMDB ~]#
这里密码为:rGauXA8te:>p
6)进入MySQL
root@ITCMDB ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.22
7)修改密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'KZbd123469#';
Query OK, 0 rows affected (0.00 sec)
8)退出
mysql> exit
Bye
[root@ITCMDB ~]#
9)重启mysql
[root@ITCMDB ~]# systemctl restart mysqld
10)开放远程连接
mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "KZbd123460#";
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>
11)开放3306端口或关闭防火墙
[root@ITCMDB ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
[root@ITCMDB ~]# firewall-cmd --reload
success
[root@ITCMDB ~]#
12)远程连接测试
用navicat进行连接
5.安装django2.0.1
[root@ITCMDB local]# pip install django==2.0.1
6.安装uwsgi
[root@ITCMDB local]# pip install uwsgi
测试uwsgi
uwsgi --http :9000 --module kzbd_cmdb.wsgi
7.安装virtualenvwrapper虚拟环境
[root@ITCMDB /]# yum install python-setuptools python-devel
[root@ITCMDB /]# pip install virtualenvwrapper
接下来编辑.bashrc文件,这是重点
[root@ITCMDB ~]# vi ~/.bashrc
添加下面两行
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
保存后,重载./bashrc文件
[root@ITCMDB bin]# source ~/.bashrc
virtualenvwrapper.user_scripts creating /root/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postmkproject
virtualenvwrapper.user_scripts creating /root/.virtualenvs/initialize
virtualenvwrapper.user_scripts creating /root/.virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/get_env_details
[root@ITCMDB bin]#
8.创建虚拟环境
[root@ITCMDB bin]# mkvirtualenv kzitcmdb
Using base prefix '/usr/local'
New python executable in /root/.virtualenvs/kzitcmdb/bin/python3.6
Also creating executable in /root/.virtualenvs/kzitcmdb/bin/python
Installing setuptools, pip, wheel...done.
virtualenvwrapper.user_scripts creating /root/.virtualenvs/kzitcmdb/bin/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/kzitcmdb/bin/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/kzitcmdb/bin/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/kzitcmdb/bin/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/kzitcmdb/bin/get_env_details
(kzitcmdb) [root@ITCMDB bin]#
创建完成后直接进入虚拟环境,也可以通过workon进入。
9.安装pip包
将准备的plist.txt上传到服务器
然后执行下面命令
[root@ITCMDB /]# workon kzitcmdb
(kzitcmdb) [root@ITCMDB /]# pip install -r plist.txt
10.将项目文件上传到服务器规划的目录
/home/wwwroot
同时,将数据库上传到服务器
本例中,用navicat进行数据库的复制。
此时,可以通过python内置的服务器进行测试
(kzitcmdb) [root@ITCMDB wwwroot]# python manage.py runserver 0.0.0.0:9999
启动成功后,在浏览器进行测试。
11.配置nginx作为反向代理服务器
在这个目录下新建一个或复制default.conf为itcmdb.conf
[root@ITCMDB conf.d]# pwd
/etc/nginx/conf.d
编辑itcmdb.conf
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8000; # uwsgi的端口
}
# configuration of the server
error_log /home/wwwroot/nginxerror.log;#错误日志
server {
# the port your site will be served on
listen 80;
# 端口
server_name 192.168.150.20 ; # 服务器ip或者域名
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /home/wwwroot/media; # 指向django的media目录
}
# Django static
location /static {
alias /home/wwwroot/extra_apps/xadmin/static; # 指向django的static目录
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include uwsgi_params; # uwsgi服务
}
}
12.拉取所有需要的static file到同一目录
在django的setting文件中,添加下面一行内容:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
并运行命令:
(kzitcmdb) [root@ITCMDB wwwroot]# python manage.py collectstatic
完成后,重启nginx
13.编辑uwsgi文件
这里uwsgi文件放在项目目录下
/home/wwwroot/conf/uwsgi
新建一个uwsgi.ini文件
ite_uwsgi.ini file`
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /home/wwwroot
# Django's wsgi file
module = kzbd_cmdb.wsgi
# the virtualenv (full path)
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 4
threads = 2
# the socket (use the full path to be safe
socket = 127.0.0.1:8000
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
virtualenv = /root/.virtualenvs/kzitcmdb
logto = /home/wwwroot/kzbd_cmdb/uwsgilog.log
14.启动uwsgi
进入虚拟环境
(kzitcmdb) [root@ITCMDB uwsgi]# pwd
/home/wwwroot/conf/uwsgi
(kzitcmdb) [root@ITCMDB uwsgi]# uwsgi -i /home/wwwroot/conf/uwsgi/uwsgi.ini &
15.项目上线测试
在浏览器输入
http://192.168.150.20
进行测试,效果如下:
16.遇到的问题
1)安装virtualwrapper后新建虚拟环境出错的问题;
2)Centos7.3自带的python2.7.5升级到python3.6.5时软连建立的问题;
3)xadmin后台管理系统样式丢失的问题;
这些坑在后续总结。