This is my summary of learning to deploy Django these days, there are a lot of problems in the middle. Hereby recorded, used to review the consolidation, but also hope to the students want to learn some reference.
The first step: I'm python3 on Ubuntu. sudo apt-get install Python3, this method does not set environment variables. If both Python2 and Python3 are available on the machine, the default version of Python can be modified for convenience, as follows:
#先备份
sudo cp/usr/bin/python/usr/bin/python_bak,
#删除
sudo rm/usr/bin/python
#默认设置成python3.4
sudo ln-s/usr/bin/python3.4/usr/bin/python.
Enter Python in the terminal, enter the Python3
Step two: 2.1 Download django1.9 (https://www.djangoproject.com/download/)
Then unzip (the compressed file name of the tar zxvf download)
2.2 Download Setuptools (https://pypi.python.org/pypi/setuptools), select ez_setup.py, and then execute the file,
chmod +x Ez_setup.py,sudo./ez_setup.py
4. Enter the Django folder that was generated after decompression and install.
Django.sudo python setup.py Install
5. Check that the installation is complete.
Python
Import Django
Django. VERSION
The ability to see the version number is the installation success.
This method for download source installation
Install WSGI. sudo
apt-get
install
libapache2-mod-wsgi-py3
Step three: Write a Django test project, which is very simple and does not use a variety of complex things. I built a workspace folder in the home directory and then set up the project under this folder Test1
The absolute path to the project is/home/zhaoxu/workspace/test1
#新建一个test项目
Cd/home/zhaoxu/worksapce
django-admin.py Startporject Test1
#新建app, called Hello
CD Test1
Python manage.py Startapp Hello
Add the app to the settings of your project
sudo vi/home/zhaoxu/test1/test1/settings.py
INSTALLED_APPS
=
(
‘django.contrib.admin‘
,
‘django.contrib.auth‘
,
‘django.contrib.contenttypes‘
,
‘django.contrib.sessions‘
,
‘django.contrib.messages‘
,
‘django.contrib.staticfiles‘
,
‘learn‘
,
)
Define view function sudo vi/home/zhaoxu/test1/hello/views.py add from django.http import HttpResponse
def home_view (Request):
Return HttpResponse (' Hello World ')
Modify url.py
sudo vi/home/zhaoxu/test1/test1/url.py
From hello import views as Hello_views
Urlpatterns = [
URL (r ' ^admin/', admin.site.urls),
URL (r ' ^$ ', Hello_views.home_view),
]
。 The Django simple configuration is now complete. If you want to verify that you are configured correctly, you can try it first with Django's own Web server.
Cd/home/zhaoxu/workspace/test1
Python manage.py runserver #打开django自带的web服务器
Enter 127.0.0.1:8000 in the browser, if the page jumps to the Hello World, the configuration is successful. The way to close this server is CTRL + C
Fourth Step installation Apache2
If the machine is installed apache2.4, I do not use the source code installed.
sudo apt-get install apache2
After the installation is complete, the browser enters 127.0.0.1, pop-up Web page it works!, installation success, this is a key step to success. If there is a problem with apache2, you want to uninstall the reload, the method is as follows:
sudo apt-get--purge remove apache2, remember to manually delete (sudo rm-rf file path) if prompted for some files that are not deleted
Sduo Apt-get Autoremove, this step if the error, you can use the following methods:
1.$ sudo mv/var/lib/dpkg/info/var/lib/dpkg/info_old//renaming the Info folder now
2.$ sudo mkdir/var/lib/dpkg/info//re-create a new info folder
3.$ sudo apt-get update, apt-get-f Install//Don't explain it.
4.$ sudo mv/var/lib/dpkg/info/*/var/lib/dpkg/info_old//After performing the previous action, some files are generated under the new Info folder and are now moved to the Info_old folder
5.$ sudo rm-rf/var/lib/dpkg/info//delete your new info folder
6.$ sudo mv/var/lib/dpkg/info_old/var/lib/dpkg/info//Change the previous Info folder back to the name
Then, in Whereis apache2, remove the remaining apache2 files manually.
Fifth Step configuration apache2, currently only use the Apache2 virtual host function
My virtual host name is: www.yourdomain.com
Create a new virtual host profile under the/etc/apache2/site-available directory, and I'll name it sitename.conf
sudo vi/etc/apache2/site-available/sitename.conf
<VirtualHost *:80>
ServerName www.yourdomain.com #虚拟主机的域名,可以在浏览器中输入。
ServerAlias otherdomain.com
Alias
/media/
/home/zhaoxu/workspace/test1/media/#存储媒体文件的路径
Alias
/static/
/home/zhaoxu/workspace/test1
/static/#存储静态文件的路径
<Directory
/home/zhaoxu/workspace/test1
/media
>
Require all granted
<
/Directory
>
<Directory
/home/zhaoxu/workspace/test1
/static
>
Require all granted
<
/Directory
>
WSGIScriptAlias /
/home/zhaoxu/workspace/test1
/test1/wsgi
.py #非常重要的一步,将apache2与django项目相结合
<Directory
/home/zhaoxu/workspace/test1
/test1>
<Files wsgi.py>
Require all granted
<
/Files
>
<
/Directory
>
<
/VirtualHost
>
Then modify the Wsgi.pysudo vi/home/zhaoxu/workspace/test1/test1/wsgi.py
from
os.path
import
join,dirname,abspath
PROJECT_DIR
=
dirname(dirname(abspath(__file__)))
import
sys
sys.path.insert(
0
,PROJECT_DIR)
Modify Settings.pysudo vi/home/zhaoxu/workspace/test1/test1/settings.py Add debug = falseallowed_hosts = [' 127.0.0.1 ', Www.yourdomain.com '] Then set permissions on directories and files Cd/home/zhaoxu/workspace
sudo
chmod
-R 644 test1
sudo
find
Test1
-
type
d -
exec
chmod
755 {} \;
最后,重启apache2和激活网站
/etc/init.d/apache2 restart
sudo
a2ensite sitename 或
sudo
a2ensite sitename.conf
Enter www.yourdomain.com in the browser and the Hello World page will appear, which is the Web page of our Test1 project in Django. Note that the www.yourdomain.com to be parsed in the Hosts file, sudo vi/etc/hosts127.0.0.1 www.yourdomain.com to check if it is correct, the ping command can be used to test. Ping www.yourdomain.com. Can ping the instructions are configured correctly. Configuration direction is virtualhost->wsgi.py->settings.py->urls.py->views.py above is a simple configuration process, to build a good platform is a very important step in the development of the site. After the platform is set up, you can learn the Django development tutorials from the Web. In addition, the above is not particularly detailed, about static files and media files settings not written. Recommended http://www.ziqiangxuetang.com/django/django-deploy.html. Written in a very detailed, but a little bit of a flaw, you can combine my writing and tutorial in a piece to see.
apache2.4 +django1.9+python3+ubuntu15.10