之前系统是Ubuntu16.04,这次换成centos7.3试一下
- 查看系统状态
- 安装nodejs,npm,yarn,vue/cli,vue/cli-init
# 安装nodejs
yum -y install nodejs
# 解决npm设置https镜像源问题
yum -y install yum-utils
yum -y install openssl
yum-config-manager --enable cr
yum update
# 更新npm的包镜像源,方便快速下载
sudo npm config set registry https://registry.npm.taobao.org
sudo npm config list
# 全局安装n管理器(用于管理nodejs版本)
sudo npm install n -g
# 安装最新的nodejs(stable版本)
sudo n stable
sudo node -v
# git的安装
yum install git
# 安装vue-cli3
npm install -g @vue/cli
# yarn 安装
npm install -g yarn
# 习惯vue-cli2的话可以安装vue-init
npm install -g @vue/cli-init
- 安装mysql.(同时解决django sqlite3版本低的问题)
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
# 首先启动MySQL
systemctl start mysqld.service
# 查看MySQL运行状态
systemctl status mysqld.service
# 找出root密码
grep "password" /var/log/mysqld.log
[root@local]# grep "password" /var/log/mysqld.log
2019-12-17T06:49:53.882401Z 1 [Note]
A temporary password is generated for root@localhost: _T2!:h!eSohY
# 登录mysql
mysql -uroot -p
# 修改初始密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';
# 创建数据库
mysql> create database pydb;
# 添加非root账户
mysql> create user user1 identified by 'user123456';
# 赋予新用户数据库权限
mysql> grant all privileges on pydb.* to user1@'localhost' identified by 'user123456';
# 安装pymysql
pip3 install pymysql
- django环境搭建
yum install python3-pip
pip3 install django
root@localhost:~# cd /home
root@localhost:/home# django-admin startproject myproject
root@localhost:/home# cd myproject
python3 manage.py startapp django_app # 提示sqlite3错误
启动django前,修改数据库为mysql,修改/home/myproject/myproject/settings.py
将
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
修改为
DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }
'default':{
'ENGINE':'django.db.backends.mysql',
'NAME':'pydb',
'USER':'user1',
'PASSWORD':'user123456',
'HOST':'localhost',
'PORT':'3306'
}
}
在/home/myproject/myproject/init.py中加入
import pymysql
pymysql.install_as_MySQLdb()
注释/usr/local/lib64/python3.6/site-packages/django/db/backends/mysql/base.py以下2行
#if version < (1, 3, 13):
# raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
- 启动django项目
# vue项目搭建
root@localhost:/home/myproject# python3 manage.py startapp django_app
root@localhost:/home/myproject# vue-init webpack vue_app
root@localhost:/home/myproject# cd vue_app
root@localhost:/home/myproject/vue_app# yarn
root@localhost:/home/myproject/vue_app# yarn run build
/home/myproject/django_app添加urls.py
from django.conf.urls import url,include
from . import views
urlpatterns = [
url(r'get_data',views.get_data,),
]
/home/myproject/django_app修改views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render,HttpResponse,Http404
from django.http import JsonResponse
from django.views.decorators.http import require_http_methods
from django.core import serializers
import json
from . import models
# Create your views here.
def get_data(request):
content = request.GET['content']
return HttpResponse(content)
/home/myproject/myproject修改urls.py
from django.contrib import admin
from django.conf.urls import url,include
from django.views.generic.base import TemplateView
from django.views import static ##新增
from django.conf import settings ##新增
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api/',include('django_app.urls')),
url(r'^static/(?P<path>.*)$', static.serve,{'document_root': settings.STATIC_ROOT}, name='static'),
url(r'^$', TemplateView.as_view(template_name="index.html")),
]
/home/myproject/myproject修改settings.py
"""
Django settings for webone project.
Generated by 'django-admin startproject' using Django 3.0.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '=%nugts==9l5-e(7!c91^e@xr2uor7$v8m$^u9#_m&t3g=f@)a'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['你的服务器ip']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_app',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'webone.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['vue_app/dist'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'webone.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }
'default':{
'ENGINE':'django.db.backends.mysql',
'NAME':'pydb',
'USER':'user1',
'PASSWORD':'user123456',
'HOST':'localhost',
'PORT':'3306'
}
}
# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/
LANGUAGE_CODE = 'zh-hans' #修改此处
TIME_ZONE = 'Asia/Shanghai' #修改此处
#TIME_ZONE = 'UTC' #修改此处
USE_I18N = True
USE_L10N = True
USE_TZ = False #修改此处
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,"static/")
STATICFILES_DIRS = [os.path.join(BASE_DIR,"vue_app/dist/static"),]
- 运行
python3 run migrate
python3 manage.py collectstatic
python3 manage.py runserver 0.0.0.0:80
然后就可以在浏览器中访问了
8 . 使用uwsgi运行django项目
yum install python3-devel.x86_64
pip3 install uwsgi
在/home/myproject目录下添加文件uwsgi.ini
[uwsgi]
http = :80
#chdir = /home/myproject
module = myproject.wsgi
master = true
processes = 4
# clear environment on exit
vacuum = true
daemonize = /home/myproject/uwsgi.log
pidfile = /home/myproject/uwsgi.pid
运行
python3 run migrate
python3 manage.py collectstatic
uwsgi --ini wsgi.ini