温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Ubuntu下如何部署django、python和mysql环境

发布时间:2022-10-19 14:30:02 来源:亿速云 阅读:103 作者:iii 栏目:服务器

这篇“Ubuntu下如何部署django、python和mysql环境”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Ubuntu下如何部署django、python和mysql环境”文章吧。

一、python环境搭建

操作系统ubuntu14.04,自带python2.7.6

im@58user:/$ python
python 2.7.6 (default, oct 26 2016, 20:30:19) 
[gcc 4.8.4] on linux2
type "help", "copyright", "credits" or "license" for more information.
>>>

二、django环境搭建

目前django的版本已经到1.11了。先去官网下载linux对应的文件,然后解压&安装。()

tar xzvf django-1.11.x.tar.gz
cd django-1.11.x
sudo python setup.py install

这时可能会提示importerror: no module named setuptools

执行

sudo https://bootstrap.pypa.io/ez_setup.py -o - | sudo python

然后执行

python setyp.py install```

到此django安装成功~!

三、mysql安装

执行一下命令,运行过程中可能需要输入root密码并进行确认。

sudo apt-get install mysql-server mysql-client
sudo apt-get install libmysqld-dev

然后链接mysql和python

sudo apt-get install python-dev
sudo wget https://pypi.python.org/packages/source/m/mysql-python/mysql-python-1.2.5.zip
unzip mysql-python-1.2.5.zip
cd mysql-python-1.2.5/
sudo python setup.py install

进入mysql数据库的方式:

> * sudo mysql
* mysql -u root -p 
然后输入密码

四、给mysql设置root密码

先以第一种方式进入mysql

mysql> use mysql;
reading table information for completion of table and column names
you can turn off this feature to get a quicker startup with -a

database changed
mysql> update user set password = password(‘root') where user ='root';
query ok, 3 rows affected (0.00 sec)
rows matched: 3 changed: 3 warnings: 0
mysql> exit

括号里面的'root'就是新的密码

五、新建项目

到了验证结果的时候了

将当前目录切换到python的worspace下,输入新建的项目名称:

im@58user:~/pythonprojects$django-admin.py startproject hello
im@58user:~/pythonprojects$ cd hello/
im@58user:~/pythonprojects/hello$ tree
├── hello
│ ├── init.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
  1. * __init__.py:python特性,可以是空文件,表明这个文件夹是一个可以导入的包。

  2. * settings.py:配置文件,本文主要修改数据库信息、模板目录、加载模块的信息。

  3. * url.py:url配置文件,指定函数与url的映射关系。

  4. * wsgi.py:本文中不会使用,nginx/apache+wsgi在生产环境中运行django时使用

接下来我们写一个helloworld页面。

在hello文件下的一级目录创建views.py文件

im@58user:~/pythonprojects/hello$ touch views.py
im@58user:~/pythonprojects/hello$ ls
hello manage.py views.py
im@58user:~/pythonprojects/hello$ tree
.
├── hello
│ ├── init.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
└── views.py

1 directory, 6 files

在views.py文件中写入下面代码

from django.http import httpresponse

def hello(request):
return httpresponse(“hello world~!~!”)

然后再将路径添加在urls.py文件中

from django.conf.urls import url
from django.contrib import admin
from views import hello

urlpatterns = [
url(r'^admin/‘, admin.site.urls),
url(r'^hello/‘, hello),
]

然后在hello目录下执行python manage.py runserver 0.0.0.0:8080

启动服务器

打开浏览器访问http://127.0.0.1:8000/hello/ 可以看到展示结果。

以上就是关于“Ubuntu下如何部署django、python和mysql环境”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI