本文介绍Django + Bootstrap基础环境开发环境的搭建
1、使用工具:Pycharm与Linux Centos 7操作系统
2、Django + uSWGI + Nginx环境的搭建以及Bootstrap的使用
Linux安装Django
pip3 install django
Pycharm创建New Project的 Django项目(等待时间较长)
关于venv:先选择本地venv,可以选择重新创建
创建完成:
Linux创建Django项目文件目录
[root@localhost Python-3.6.4]# mkdir -p /var/www/leondjango
[root@localhost Python-3.6.4]# cd /var/www/leondjango
[root@localhost leondjango]# ls
[root@localhost leondjango]#
Pycharm添加远程linux运行环境
Linux查看python3目录
[root@localhost leondjango]# which python3
/usr/bin/python3
编辑project配置
设置setting.py中的ALLOWD_HOSTS
Linux运行命令:
[root@localhost leondjango]# python3 manage.py migrate
错误排查
django.core.exceptions.ImproperlyConfigured: SQLite 3.8.3 or later is required (found 3.7.17).
可以通过如下方法解决:注释掉抛出异常的语句。
查找django安装目录:
cd /find -name django
然后发现相关目录:
./usr/local/lib/python3.6/site-packages/django./usr/local/lib/python3.6/site-packages/django/forms/jinja2/django./usr/local/lib/python3.6/site-packages/django/forms/templates/django
修改sqlite的base.py:
vi /usr/local/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py
注释掉抛出异常的语句:
Centos 7和 Centos 6开放查看端口防火墙关闭打开:
https://www.zccie.com/python/992/
[root@localhost leondjango]# firewall-cmd --list-ports
[root@localhost leondjango]# firewall-cmd --zone=public --add-port=8000/tcp --permanent
success
[root@localhost leondjango]# firewall-cmd --reload
success
[root@localhost leondjango]# firewall-cmd --list-ports
8000/tcp
Django运行成功:
Bootstrap与模板
templates/index.html
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>leonzhang - index</title>
</head>
<body>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="/urltest">URLTEST</a>
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
<div class="container-fluid">
<h2>index Test</h2>
</div>
</body>
</html>
templates/urltest.html
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>lenonzhang - urltest</title>
</head>
<body>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="/urltest">URLTEST</a>
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
<div class="container-fluid">
<h2>url Test</h2>
</div>
</body>
</html>
views/index.py
from django.shortcuts import render_to_response
def index(request):
return render_to_response('index.html')
views/urltest.py
from django.shortcuts import render_to_response
def urltest(request):
return render_to_response('urltest.html')
leondjango/urls.py
from django.contrib import admin
from django.urls import path
from views import index,urltest
urlpatterns = [
path('admin/', admin.site.urls),
path('', index.index),
path('urltest/', urltest.urltest)
]
安装UWSGI
[root@localhost leondjango]# pip3 install uwsgi
[root@localhost leondjango]# find / -name uwsgi
/usr/local/python3/bin/uwsgi
[root@localhost leondjango]# ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi
测试:
[root@localhost leondjango]# uwsgi --http :8080 --chdir /var/www/leondjango --module leondjango.wsgi
*** Starting uWSGI 2.0.18 (64bit) on [Thu Jun 6 18:48:24 2019] ***
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-36) on 06 June 2019 08:45:26
os: Linux-3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018
nodename: localhost.localdomain
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 2
current working directory: /var/www/leondjango
detected binary path: /usr/local/python3/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
chdir() to /var/www/leondjango
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 7183
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on :8080 fd 4
spawned uWSGI http 1 (pid: 81286)
uwsgi socket 0 bound to TCP address 127.0.0.1:34382 (port auto-assigned) fd 3
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
Python version: 3.6.4 (default, Jun 6 2019, 13:38:15) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x1524a20
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72920 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x1524a20 pid: 81285 (default app)
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 81285, cores: 1)
[pid: 81285|app: 0|req: 1/1] 192.168.80.1 () {38 vars in 692 bytes} [Thu Jun 6 10:48:31 2019] GET / => generated 3124 bytes in 30 msecs (HTTP/1.1 200) 3 headers in 110 bytes (1 switches on core 0)
[pid: 81285|app: 0|req: 2/2] 192.168.80.1 () {38 vars in 726 bytes} [Thu Jun 6 10:48:34 2019] GET /urltest/ => generated 3125 bytes in 2 msecs (HTTP/1.1 200) 3 headers in 110 bytes (1 switches on core 0)
编写uWSGI的配置文件
[root@localhost ~]# cd /var/www/leondjango/
[root@localhost leondjango]# ls
db.sqlite3 leondjango manage.py __pycache__ templates venv views
[root@localhost leondjango]# vi uwsgi.ini
# uwsgi.ini file
[uwsgi]
# Django-related settings
socket = 127.0.0.1:8001
# the base directory (full path)
chdir = /var/www/leondjango
# Django's wsgi file
module = leondjango.wsgi
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 2
# ... with appropriate permissions - may be needed
#chmod-socket = 666
#chown-socket = nginx:nginx
# clear environment on exit
vacuum = true
enable-threads = true
安装NGINX
[root@localhost leondjango]# yum install epel-release
[root@localhost leondjango]# yum -y install nginx
[root@localhost leondjango]# vi /etc/nginx/nginx.conf
upstream django {
server 127.0.0.1:8001;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name 192.168.80.13;
root /var/www/leondjango; #项目路径
#load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
include uwsgi_params;
uwsgi_pass django;
}
location /static/ { #加入静态文件路径.包括CSS文件,image文件和JS文件
autoindex on;
alias /var/www/static/;
}
}
uWSGI服务
[root@localhost leondjango]# cd /etc/systemd/system
[root@localhost system]# vi leon.service
[Unit]
Description = uWSGI instance to serve leon
After=network.target
[Service]
User=nginx
Group=nginx
ExecStart=/usr/bin/bash -c 'cd /var/www/leondjango; uwsgi --ini uwsgi.ini'
[Install]
WantedBy=multi-user.target
[root@localhost system]# systemctl start leon.service
[root@localhost system]# systemctl status leon.service
● leon.service
Loaded: loaded (/etc/systemd/system/leon.service; bad; vendor preset: disabled)
Active: active (running) since Wed 2019-07-03 14:02:31 CST; 7s ago
Main PID: 7663 (bash)
CGroup: /system.slice/leon.service
├─7663 /usr/bin/bash -c cd /var/www/leondjango; uwsgi --ini uwsgi.ini
├─7664 uwsgi --ini uwsgi.ini
├─7666 uwsgi --ini uwsgi.ini
└─7667 uwsgi --ini uwsgi.ini
Jul 03 14:02:31 localhost.localdomain bash[7663]: python threads support enabled
Jul 03 14:02:31 localhost.localdomain bash[7663]: your server socket listen backlog is limited to 100 co...ions
Jul 03 14:02:31 localhost.localdomain bash[7663]: your mercy for graceful operations on workers is 60 seconds
Jul 03 14:02:31 localhost.localdomain bash[7663]: mapped 218760 bytes (213 KB) for 2 cores
Jul 03 14:02:31 localhost.localdomain bash[7663]: *** Operational MODE: preforking ***
Jul 03 14:02:32 localhost.localdomain bash[7663]: WSGI app 0 (mountpoint='') ready in 1 seconds on inter...app)
Jul 03 14:02:32 localhost.localdomain bash[7663]: *** uWSGI is running in multiple interpreter mode ***
Jul 03 14:02:32 localhost.localdomain bash[7663]: spawned uWSGI master process (pid: 7664)
Jul 03 14:02:32 localhost.localdomain bash[7663]: spawned uWSGI worker 1 (pid: 7666, cores: 1)
Jul 03 14:02:32 localhost.localdomain bash[7663]: spawned uWSGI worker 2 (pid: 7667, cores: 1)
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost system]# systemctl enable leon.service
Created symlink from /etc/systemd/system/multi-user.target.wants/leon.service to /etc/systemd/system/leon.service.
[root@localhost system]# systemctl start nginx
[root@localhost system]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@localhost system]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2019-07-03 14:04:38 CST; 35s ago
Main PID: 7693 (nginx)
CGroup: /system.slice/nginx.service
├─7693 nginx: master process /usr/sbin/nginx
├─7694 nginx: worker process
└─7695 nginx: worker process
Jul 03 14:04:38 localhost.localdomain systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jul 03 14:04:38 localhost.localdomain nginx[7688]: nginx: the configuration file /etc/nginx/nginx.conf sy... ok
Jul 03 14:04:38 localhost.localdomain nginx[7688]: nginx: configuration file /etc/nginx/nginx.conf test i...ful
Jul 03 14:04:38 localhost.localdomain systemd[1]: Started The nginx HTTP and reverse proxy server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost system]# firewall-cmd --list-ports
8000/tcp 8080/tcp
[root@localhost system]# firewall-cmd --zone=public --add-port=80/tcp --permanent
success
[root@localhost system]# firewall-cmd --reload
success
关闭SELinux
1、关闭SeLinux:
查看SELinux状态:
1、# /usr/sbin/sestatus -v ##如果SELinux status参数为enabled即为开启状态
SELinux status: enabled
2、getenforce ##也可以用这个命令检查
关闭SELinux:
1、临时关闭(不用重启机器):
# setenforce 0 ##设置SELinux 成为permissive模式
# setenforce 1 设置SELinux 成为enforcing模式
2、修改配置文件需要重启机器:
修改# /etc/selinux/config 文件
将SELINUX=enforcing改为SELINUX=disabled
重启机器即可
普及一下SELinux:
什么是 SELinux(Security-Enhanced Linux) 是美国国家安全局(NAS)对于强制访问控 制的实现,在这种访问控制体系的限制下,进程只能访问那些在他的任务中所需要文件。大部分使用 SELinux 的人使用的都是SELinux就绪的发行版,例如 Fedora、Red Hat Enterprise Linux (RHEL)、Debian 或 Gentoo。它们都是在内核中启用SELinux 的,并且提供一个可定制的安全策略,还提供很多用户层的库和工具,它们都可以使用 SELinux 的功能。
修改favorite和Logo图片
[root@localhost ~]# mkdir /var/www/static/
<link rel="shortcut icon" href="/static/favicon.ico">
<a class="navbar-brand" href="#"><img src="/static/logo.png" height="40" width="auto"></a>