Django Development Personal Easy Blog-nginx+uwsgin+django1.6+mysql deployment to CentOS6.5

Source: Internet
Author: User
Tags bz2 mysql version administrator password django website install django pip install django

The process of creating the project and the design of the data model are described. If you haven't seen it, you can check it here, and the project source has been zoomed into GitHub and can be downloaded here.

The code has also been deployed to the Sina Sea, with the address http://fengzheng.sinaapp.com/

Skip the section on view presentation and form processing first, and first explain how to deploy.

The deployment environment is clearly described in the title:

Server: CentOS6.5  is actually my development machine. Mysql:server version:5.1.73 Source distributionnginx version: nginx/ Version 1.6.0python: 2.7.3django version: (1, 6, 5, ' final ', 0) Uwsgi

The following describes my deployment process, just my deployment process, for different configurations may vary, for reference only.

Some software needs to be installed online, and the default source of Linux is foreign, download speed is very slow, you can set a domestic source, I set here is 163 source, download speed or fast.

1. Enter the folder where the source configuration is stored

Cd/etc/yum.repos.d

2. Back up the default source

mv./centos-base.repo./centos-base.repo.backup

3. Use wget to download 163 of the source

wget Http://mirrors.163.com/.help/CentOS-Base-163.repo

wget Http://mirrors.163.com/.help/CentOS6-Base-163.repo

4. Set the downloaded file Centos-base-163.repo as the default source

MV Centos-base-163.repo Centos-base.repo

MV Centos6-base-163.repo Centos-base.repo

1. Install MySQL:

CentOS6.5 The default MySQL version is 5.1.73, so if you do not have a special request, you can change it. If required, you can uninstall your own MySQL and reinstall the required version.

Here is an article about installing MySQL with the Yum command, which you can refer to for installation. Of course, you can download the source code, unzip, compile, install. The process does not introduce too much.

Common commands for MySQL:

Check for MySQL service status # service mysqld status start MySQL service, to start MySQL must have permissions generally before using the SU command, enter the Administrator password # service mysqld start stop MySQL service    # Service mysqld Stop Restart # service mysqld Restart    login with root # mysql-u root–p Show all databases # show databases; using MyBlog database # Use MYB log; show all tables # show tables;

2. Upgrade Python to 2.7.3:

Because the CentOS6.5 default Python version is version 2.6, upgrade is required. Here's how to install the source code:

#下载python2.7.3 Source Compression Package wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2# unzip the tar XF python-2.7.3.tar.bz2# Enter the unpacked directory CD python-2.7.3# configuration and Environment check./configure# Install make install

After installation, enter Python in the terminal window to see if the Python version is already a 2.7.3 version.

Note: This may cause the Yum command to fail after this upgrade,

Since Yum relies on the default Python version of the Contos system, and after upgrading Python, the Python version in the Yum script is modified to the latest version, which needs to be changed back to the original Python version. ContOS6.5 The default Python version is python2.6.6 and the workaround is as follows:

Enter the directory where Yum is located

Cd/usr/bin

Su

Vim Yum

The first line
#!/usr/bin/python2.7
Switch
#!/usr/bin/python2.6

Input: wq! Force Save

3. Install the MYSQLDB module:

Need to download the source Zip package here, the latest version is 1.2.3. Installation process:

cd/home/fengzheng/Soft/  #进入压缩包所在目录

tar-zxf MySQL-python-1.2.3.tar.gz  #解压

cdMySQL-python-1.2.3  #进入解压后的目录

python setup.py build #编译

python setup.py install #安装

After the installation is complete, you can enter the following command in the Terminal window to test if the installation was successful, and if no error message appears, the installation was successful.

Pythonimport MySQLdb  

4. Install Django:

This is not much to say, you can download the source on the Django website, and then use the command to install the source code:

cd/home/fengzheng/Soft/

tar-zxf Django-1.6.5.tar.gz

cdDjango-1.6.5/

python setup.py install

You can also use the online installation method provided by the official web site, which requires PIP support:pip install Django==1.6.5

5.安装uwsgi:

Export ldflags= "-xlinker--no-as-needed" $ pip Install Uwsgi

To test whether the UWSGI was installed successfully:

Create a new uwsgitest.py file with the following code:

#-*-coding:utf-8-*-def Application (env, start_response):    start_response (' OK ', [' content-type ', ' text/html ')])    return "Hello Uwsgi"

Enter the directory where the file is located and execute the command:

Uwsgi--http:1989--wsgi-file uwsgitest.py

After that, in the browser access http://127.0.0.1:1989, if the word Hello Uwsgi appears, the UWSGI installation is successful.

6. Install Nginx:

Download the required rpm file for the Nginx centos6.x. To run the command:

Su

RPM–IVH nginx-release-centos-6-0.el6.ngx.noarch.rpm

Yum Install Nginx

Nginx Common commands:

#查看nginx安装位置: Whereis nginx# view ngin status service nginx State start Nginx:/usr/sbin/nginx or directly enter Nginx or service nginx start # Stop Nginxservice nginx stop# Restart service nginx restart or nginx-s  Reload Note: The start service must have administrator privileges before you have the SU command

Sometimes an exception occurs: Nginx: [Emerg] bind () to 0.0.0.0:80 failed (98:address already on use)

This problem occurs because the 80 port is occupied by Nginx itself, and the workaround is:

Fuser-k 80/tcp

And then start the Nginx.

Last visit to http://127.0.0.1, see the following interface, the installation of Nginx is correct and successfully started:

7. Configure UWSGI with Nginx support Django:

Both Uwsgi and Nginx can work alone, and we want to link the two together to support the Django project.

First we open the directory where the project is located and create a new django_ in the root directory, which is the directory where manage.py is located. uwsgi.py file, this file is to be run by Django in Uwsgi Way, the file content is as follows: The two lines of the comment in the code are the way that manage.py runs Django, and you can see what's different.

"" "WSGI config for Fengzhengblog project. It exposes the WSGI callable as a module-level variable named ' Application '. For more information on the this file, seehttps://docs.djangoproject.com/en/dev/howto/deployment/wsgi/"" "Import Osos.environ.setdefault ("Django_settings_module", "fengzhengblog.settings") #from Django.core.wsgi import Get_wsgi_ Application#application = Get_wsgi_application () from django.core.handlers.wsgi import wsgihandlerapplication = Wsgihandler ()

In order to realize the connection between Nginx and Uwsgi, the two will use Soket to communicate, but also need to be in the project root directory, that is, and the above django_uwsgi.py the same directory to create a new file to implement, the file format can be XML, named Django_ Socket.xml, the contents are as follows:

<uwsgi>    <socket>:8077</socket>        <chdir></chdir>        <module>django_ uwsgi</module><!--The specified module is the name of the django_uwsgi.py created above--        <processes>4</processes> <!-- Number of processes-     <daemonize>uwsgi.log</daemonize></uwsgi>

or the INI format, named Django_socket.ini, which reads as follows:

[uwsgi]vhost = Falsesocket = 127.0.0.1:8077      ; communication port master = trueenable-threads = Trueworkers = 4wsgi-file = Django_uwsgi . py   ; The specified module is the django_uwsgi.py created above

Configure Nginx, with Weheris nginx command to view nginx installation directory in/etc/nginx, enter this directory, with vim open nginx.conf configuration file, modify the content:

User nginx;worker_processes 1;error_log/var/log/nginx/error.log warn;pid/var/run/nginx.pid;events {worker_ Connections 1024;} HTTP {include/etc/nginx/mime.types;default_type Application/octet-stream;log_format main ' $remote _addr-$remote _user [$time _local] "$request" "$status $body _bytes_sent" $http _referer "" "$http _user_agent" "$http _x_forwarded_for "'; Access_log/var/log/nginx/access.log main;sendfile on; #tcp_nopush on;keepalive_timeout; #gzip on;inclu  De/etc/nginx/conf.d/*.conf;server {Listen 80;  #80端口server_name 127.0.0.1;  #最后访问的地址access_log/home/fengzheng/mypython/access.log; #日志error_log/home/fengzheng/mypython/error.log; #charset koi8-r; #access_log logs/host.access.log main;location/{  Include Uwsgi_params;uwsgi_pass 127.0.0.1:8077; #前面django_socket the port that is configured in the. xml or Django_socket.ini file} #error_page 404/404.html;# redirect server error pages to th E static Page/50x.html#error_page 502 503 504/50x.html;#The following configuration of static files location/css/{alias/home/fengzheng/blog/fengzhengblog/fengzhengblog/css/;}       location/js/{alias/home/fengzheng/blog/fengzhengblog/fengzhengblog/js/;        }location/images/{alias/home/fengzheng/blog/fengzhengblog/fengzhengblog/images/;        }location/ueeditor/{alias/home/fengzheng/blog/fengzhengblog/fengzhengblog/ueeditor/; }} #以下是另一个项目配置server {listen 81;server_name 127.0.0.1;access_log/home/fengzheng/mypython/accessue.log;error_log/ Home/fengzheng/mypython/errorue.log; #charset koi8-r; #access_log logs/host.access.log main;location/{include UWS Gi_params;uwsgi_pass 127.0.0.1:8088;} #error_page 404/404.html;# Redirect Server error pages to the static Page/50x.html#error_page 500 502 50 3 504/50x.html;location =/50x.html {root html;} location/upload/{alias/home/fengzheng/ueeditor_django/ueeditor_django/upload/;}        location/ue/{alias/home/fengzheng/ueeditor_django/ueeditor_django/ue/; }}}

There are two servers configured, which can support two Django sites. If there is only one, you can remove the following server node. Note the configuration of the location node, such as:

location/css/{
alias/home/fengzheng/blog/fengzhengblog/fengzhengblog/css/; }

A directory followed by a static file in the project is preceded by a "/", followed by the directory where the static file resides. Corresponds to the routing configuration in the urls.py:

(R ' ^css/(? p<path>.*) $ ', ' django.views.static.serve ',            {' document_root ': root+ '/css '}    ),    (R ' ^js/(? p<path>.*) $ ', ' django.views.static.serve ',            {' document_root ': root+ '/js '}    ),    (R ' ^images/(? p<path>.*) $ ', ' django.views.static.serve ',            {' document_root ': root+ '/images '}    ),    (R ' ^ueeditor/( ? p<path>.*) $ ', ' django.views.static.serve ',            {' document_root ': root+ '/ueeditor '}    ),

After the above settings, you can let Nginx handle the static files. Non-static file request Nginx will be sent to socket 8077, and then let uwsgi for processing.

8. Start the website:

After the configuration is complete, restart Nginx:nginx-s reload

To start the UWSGI service:

Enter the project root directory, which is the directory where the django_uwsgi.py was created earlier.

Run the following command, using the Django_socket.xml configuration:

Uwsgi-x Django_socket.xml

If the system does not support the-X command, you can run the following command to start the Django_socket.ini configuration:

Uwsgi--ini Django_socket.ini

After successful startup, you will get the following information:

[Uwsgi] Getting INI configuration from django_socket.ini*** starting Uwsgi 2.0.5.1 (64bit) on [Thu Jul 17 01:02:06 2014] * **compiled with version:4.4.7 20120313 (Red Hat 4.4.7-4) on June 20:36:27os:linux-2.6.32-431.el6.x86_64 #1 SMP F RI 03:15:09 UTC 2013nodename:localhostmachine:x86_64clock source:unixdetected number of CPU cores:1current work ing directory:/home/fengzheng/blog/fengzhengblogdetected binary path:/USR/LOCAL/BIN/UWSGI!!! No internal routing support, rebuild and PCRE support!!! Your processes number limit is 1024your memory page size is 4096 bytesdetected Max file descriptor Number:1024lock engine : Pthread Robust Mutexesthunder lock:disabled (can enable it with--thunder-lock) UWSGI sockets 0 bound to TCP address 127.0.0.1:8077 FD 3Python version:2.7.3 (default, June, 02:55:10) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)]python ma In interpreter initialized @ 0x17db280python threads support Enabledyour Server sockets Listen backlog is limited to Connectionsyour Mercy for graceful operations on workers are secondsmapped 363840 bytes (355 KB) for 4 cores*** Op Erational mode:preforking ***wsgi app 0 (mountpoint= ") ready for seconds on interpreter 0x17db280 pid:7871 (default a PP) * * * UWSGI is running in multiple interpreter mode ***spawned UWSGI Master process (pid:7871) spawned UWSGI worker 1 (pi d:7873, cores:1) spawned UWSGI worker 2 (pid:7874, cores:1) spawned UWSGI worker 3 (pid:7875, cores:1) spawned Uwsgi wo Rker 4 (pid:7876, cores:1)


Looking at the above information, we found that the boot was successful and 4 threads were turned on.

More detailed installation configuration to view http://django-china.cn/topic/101/#top和http://django-china.cn/topic/124/is very detailed.

Then visit the site: http://127.0.0.1 to see the effect

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.