Linux下配置Django_Apache_Mysql环境(CentOS 7.5)
程序员文章站
2022-04-13 10:09:04
本文将介绍如何在Linux上部署Django + Mysql + Apache环境。我们知道,Django内置的http服务器只能工作在单线程下,做开发和调试时候是可以的,但是生产环境通常都会有多用户并发,而且django的simple HTTP server处理大量静态文件的性能太差,所以要用ap ......
本文将介绍如何在linux上部署django + mysql + apache环境。我们知道,django内置的http服务器只能工作在单线程下,做开发和调试时候是可以的,但是生产环境通常都会有多用户并发,而且django的simple http server处理大量静态文件的性能太差,所以要用apache做前端。django自带的sqlite数据库权限只依赖于文件系统,没有用户帐户的概念,这里我们使用典型的关系型数据库mysql。看似简单的环境搭建,在实际操作过程中还是遇到了不少的大坑,所以特地将过程记录下来,也希望对大家有小小的帮助。
centos 7.5 + python 2.7.5 + django 1.11.14 + apache 2.4.6 + mysql 5.7.23
1. 安装django
linux上我们可以直接使用pip安装django
1.1 安装python(使用centos 7.5自带的python即可)
[root@localhost ~]# python --version python 2.7.5
1.2 网上下载get-pip.py文件安装pip:
wget https://bootstrap.pypa.io/get-pip.py
1.3 pip安装django
[root@localhost ~]# pip install django [root@localhost ~]# python python 2.7.5 (default, jul 13 2018, 13:06:57) [gcc 4.8.5 20150623 (red hat 4.8.5-28)] on linux2 type "help", "copyright", "credits" or "license" for more information. >>> import django >>> django.get_version() '1.11.14'
2. 安装apache
linux上使用yum安装apache即可
[root@localhost ~]# yum install httpd [root@localhost ~]# httpd -v [thu aug 16 20:57:04.487586 2018] [so:warn] [pid 1605] ah01574: module wsgi_module is already loaded, skipping server version: apache/2.4.6 (centos) server built: jun 27 2018 13:48:59 server's module magic number: 20120211:24 server loaded: apr 1.4.8, apr-util 1.5.2 compiled using: apr 1.4.8, apr-util 1.5.2 architecture: 64-bit server mpm: prefork threaded: no forked: yes (variable process count)
注意:使用yum安装的httpd,其安装目录位于/etc/httpd/,我们只需要配置/etc/httpd/conf/httpd.conf即可
3. 安装mysql
我们使用yum安装mysql,需要先更新yum源
[root@localhost ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm [root@localhost ~]# rpm -ivh mysql57-community-release-el7-8.noarch.rpm [root@localhost ~]# yum install mysql-community-server
注意:yum安装的mysql其文件目录如下
- 配置文件:/etc/my.cnf
- 日志文件:/var/log/var/log/mysqld.log
- 服务启动脚本:/usr/lib/systemd/system/mysqld.service
- socket文件:/var/run/mysqld/mysqld.pid
4. 配置(重点)
以上三步都非常容易,但是将这三个环境配置好,还是费了我不少的时间...
4.1 配置mysql
[root@localhost ~]# systemctl start mysqld # 开启mysql服务后,会为root设置一个默认密码,我们首先重置密码 # 获得默认密码 [root@localhost ~]# cat /var/log/mysqld.log | grep -i password [root@localhost ~]# mysql -uroot -p enter password: welcome to the mysql monitor. commands end with ; or \g. your mysql connection id is 8 server version: 5.7.23 mysql community server (gpl) mysql>alter user 'root'@'localhost' identified by 'sam_tech_0912'; # 重置密码后,我们创建一个数据库,因为后续django连接mysql时需要输入数据库名称 mysql> create database platform default charset=utf8; query ok, 1 row affected (0.00 sec) mysql> quit bye
4.2 django中配置mysql
django中关于mysql的配置:
databases = { 'default': { # 'engine': 'django.db.backends.sqlite3', # 'name': os.path.join(base_dir, 'db.sqlite3'), 'engine': 'django.db.backends.mysql', 'name': 'platform', 'host': '127.0.0.1', 'port': '3306', 'user': 'root', 'password': 'sam_tech_0912', } }
django中其他的部分的配置:
debug = true allowed_hosts = ["*",] templates = [ { 'backend': 'django.template.backends.django.djangotemplates', 'dirs': [os.path.join(base_dir, "templates"),], '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', ], }, }, ] language_code = 'zh-hans' time_zone = 'asia/shanghai' use_i18n = true use_l10n = true use_tz = true static_url = '/static/' staticfiles_dirs = [ os.path.join(base_dir, "static"), ] media_url = "/media/" media_root = os.path.join(base_dir, "media")
4.3 配置apache
重点:安装mod_wsgi
[root@localhost ~]# yum install mod_wsgi [root@localhost ~]# rpm -qa | grep wsgi mod_wsgi-3.4-12.el7_0.x86_64
编辑配置文件 /etc/httpd/conf/httpd.conf
serverroot "/etc/httpd" # 设定apache监听的端口号,可以设定多个 listen 80 # 重点:这句是加载刚刚安装的wsgi模块,有了它django才能部署到apache上,切记!!! loadmodule wsgi_module modules/mod_wsgi.so include conf.modules.d/*.conf user apache group apache serveradmin root@localhost servername localhost:80 <directory /> allowoverride none require all denied </directory> documentroot "/var/www/html" <directory "/var/www"> allowoverride none require all granted </directory> <directory "/var/www/html"> options indexes followsymlinks allowoverride none require all granted </directory> <ifmodule dir_module> directoryindex index.html </ifmodule> <files ".ht*"> require all denied </files> errorlog "logs/error_log" loglevel warn <ifmodule log_config_module> logformat "%h %l %u %t \"%r\" %>s %b \"%{referer}i\" \"%{user-agent}i\"" combined logformat "%h %l %u %t \"%r\" %>s %b" common <ifmodule logio_module> logformat "%h %l %u %t \"%r\" %>s %b \"%{referer}i\" \"%{user-agent}i\" %i %o" combinedio </ifmodule> customlog "logs/access_log" combined </ifmodule> <ifmodule alias_module> scriptalias /cgi-bin/ "/var/www/cgi-bin/" </ifmodule> <directory "/var/www/cgi-bin"> allowoverride none options none require all granted </directory> <ifmodule mime_module> typesconfig /etc/mime.types addtype application/x-compress .z addtype application/x-gzip .gz .tgz addtype text/html .shtml addoutputfilter includes .shtml </ifmodule> adddefaultcharset utf-8 <ifmodule mime_magic_module> mimemagicfile conf/magic </ifmodule> enablesendfile on includeoptional conf.d/*.conf # 我们在/etc/httpd/conf/下新建httpd-vhosts.conf虚拟主机配置文件,完成对80端口的配置 # 这句是告诉apache去调用httpd-vhosts.conf # 虚拟主机中的配置参数将覆盖httpd.conf主配置文件中的设定 include conf/httpd-vhosts.conf
虚拟主机配置文件(关键一步)
<virtualhost *:80> serveradmin samliuming@aliyun.com documentroot "/home/python_projects/platform" servername samlinux01-platform.com serveralias sam-platform.com errorlog "logs/platform_error.log" customlog "logs/platform_access.log" common wsgiscriptalias / "/home/python_projects/platform/platform/wsgi.py" # 一定要定义python-path到项目目录,否则会报出相关模块无法找到的错误,切记!!! wsgidaemonprocess samlinux01-platform.com python-path=/home/python_projects/platform wsgiprocessgroup samlinux01-platform.com wsgiscriptreloading on # 设定apache访问django的项目目录 alias /static /home/python_projects/platform/static alias /media /home/python_projects/platform/media <directory /home/python_projects/platform/media> allowoverride none options indexes followsymlinks require all granted </directory> <directory /home/python_projects/platform/static> allowoverride none options indexes followsymlinks require all granted </directory> <directory /home/python_projects/platform/platform> <files wsgi.py> allowoverride none require all granted </files> </directory> </virtualhost>
注意:每次编辑完成后都需要重启httpd服务使配置生效
[root@localhost ~]# httpd -t [thu aug 16 20:35:06.439115 2018] [so:warn] [pid 1520] ah01574: module wsgi_module is already loaded, skipping syntax ok [root@localhost ~]# systemctl restart httpd.service
编辑django中的 wsgi.py文件
""" wsgi config for platform project. it exposes the wsgi callable as a module-level variable named ``application``. for more information on this file, see https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/ """ import os from django.core.wsgi import get_wsgi_application os.environ.setdefault("django_settings_module", "platform.settings") application = get_wsgi_application() # 添加项目路径到python的环境变量中 # for apache server import sys project_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, project_dir)
至此,大功告成,我们直接可以通过80端口访问我们的项目,虽然上面的步骤简单,但是网上查的资料并没有特别详细完整的,中间还是走了不少的弯路,所以特意将配置步骤记录下来,方便后续再次配置!
下一篇: ftp命令
推荐阅读
-
Linux 下搭建jsp服务器(配置jsp开发环境)
-
Linux下Java环境变量的安装与配置
-
Linux环境下双网卡主机路由配置教程
-
Linux centos7环境下MySQL安装教程
-
centos环境下lnmp安装配置
-
Linux环境(CentOS6.7 64位)下安装subversion1.9.5的方法
-
CentOS 6.4系统下编译安装LNMP和配置PHP环境具体步骤
-
linux CentOS 系统下如何将php和mysql命令加入到环境变量中
-
Linux+php+apache+oracle环境搭建之CentOS下安装Oracle数据库
-
在Linux的LEMP环境下配置OpenSSL的教程