Install Apache in Linux and deploy the django site in mod_wsgi Mode

Source: Internet
Author: User
Tags django website install django pip install django

Install Apache through source code compilation

First download Apache source code compressed package, address for http://mirror.bit.edu.cn/apache/httpd/

Continue downloading the apr and apr-util packages at http://mirror.bit.edu.cn/apache/apr/

Download the pcre package at http://ftp.exim.llorien.org/pcre/

L install arp

Tar-zxf apr-1.5.0.tar.gz # decompress the compressed package name according to the version number of the cd apr-1.5.0 # enter the decompressed directory. /configure -- prefix =/usr/local/apr # install and configure make # compile sudo make install # install

L install apr-util

Tar-zxvf apr-util-1.5.3.tar.gz # decompress the compressed package name according to the version number of the cd apr-util-1.5.3 # enter the decompressed directory. /configure -- prefix =/usr/local/apr-util -- with-apr =/usr/local/apr/bin/apr-1-configmake sudo make install

L install pcre

Unzip-o pcre-8.34.zip # decompress the file name depending on the version number of the cd pcre-8.34./configure -- prefix =/usr/local/pcremakesudo make install

L install Apache

Tar-zxvf httpd-2.4.7.tar.gz # unzip the cd httpd-2.4.7 I installed for 2.4.7. /configure -- prefix =/usr/local/apache2 -- with-apr =/usr/local/apr -- with-apr-util =/usr/local/apr-util/makesudo make install

Then start Apache,

Cd/usr/local/apache2/binsudo apachectl-k start or sudo service apache2 start

If the following page is displayed, the installation is successful.

How to Control Apache services

Start:

Sudo service apache2 start or sudo apachectl start

Stop:

Sudo service apache2 stop or sudo apachectl stop

Restart:

Sudo service apache2 restart or sudo apachectl restart

Reload the site:

sudo service apache2 reload
How to make Apache support django

Mod_wsgi is used.

First download mod_wsgi-2.4.tar.gz.

You can download it from the official website.

You can also download it directly here.

Next, run the following command to decompress the compressed package;

tar –zxvf mod_wsgi-3.4.tar.gz

Then cd to the decompressed directory and execute the following command to generate the configuration compilation configuration file;

./configure --with-apxs=/usr/local/apache2/bin/apxs --with-python=/usr/bin/python

/Usr/local/apache2 is the installation directory of Apache. Make sure that apxs exists in the bin directory, and/usr/bin/python is the default installation location of python in Ubuntu.

Next, run the make command to compile;

make

Finally, execute the following command for installation;

sudo make install

If it succeeds, the system prompts "chmod 755/usr/local/apache2/modules/mod_wsgi.so" to authorize mod_wsgi.so.

Follow the prompts to enter the command:

chmod 755 /usr/local/apache2/modules/mod_wsgi.so

Next, install Django:

Django official website (https://www.djangoproject.com/download/) provides two installation methods.

L pip Mode

pip install Django==1.6.2

L direct download compressed package installation, https://www.djangoproject.com/download/1.6.2/tarball/

Tar xzvf Django-1.6.2.tar.gz # unzip the cd Django-1.6.2 # enter the unzip directory sudo python setup. py install

Select either of the above two methods.

After installation, you can check whether the installation is successful in the terminal window.

I installed django 1.7.

How to Create a django website

The default www Service address of Apache is/var/www. After the service is installed, there is only one index.html webpage file.

Next, let's talk about Apache. After Apache is installed in Linux, the installation file exists in two parts, one in the directory specified during installation, the other part is in the/etc/apache2 directory.

Since the directory specified during Apache installation is/usr/local/apache2, apache is distributed in the following two directories on my machine:

And

Go to the/etc/apache2 directory and check the directory structure. The blue directory and the white directory are files.

Httpd. confThe file is usually an empty file. You can add the following commands when setting it later;

Next, let's take a look at several commands in httpd. conf. "#" is followed by comments;

 

MagicThe file contains data about the mod_mime_magic module, which generally does not need to be modified;

Ports. confThe configuration file set for the server listening IP address and port;

Mod-availableThe directory contains some. conf and. load files, which load the configuration files of various modules that can be used in the system.Mod-enabledThe directory is a symbolic connection to these configuration files. From the configuration file apache (the most popular WEB server platform on the Unix platform) 2. conf, we can see that the system usesMod-enabledDirectory to load the module, that is, the system only creates a symbolic connectionMod-availableDirectory to load the module. The system also provides two Commands: a2enmod and a2dismod to maintain these symbolic connections. These two commands are provided by apache (the most popular WEB server platform on the Unix platform) 2-common package. Various commands are also very simple: a2enmod [module] Or a2dismod [module];

Sites-availableThe configuration file of the configured site in the directory,Sites-enabledThe directory is a symbolic connection to these configuration files. The system uses these symbolic connections to start the site.Sites-enabledThe symbolic connection in the directory is attached with a numeric prefix, such as 000-default. This number is used to determine the startup sequence. The smaller the number, the higher the startup priority. the system provides two commands, a2ensite and a2dissite, to maintain these symbolic connections. These two commands are provided by the apache (the most popular WEB server platform on the Unix platform) 2-common package;

 

Apache configuration is introduced here. Next, configure the Django website.

 

The default server address of Apache is the var/www directory. You do not need to modify this location. Run cd to the/var/www directory to create a new Django project. Run the following command:

sudo django-admin.py startproject mysite

The directory structure of the created project is as follows:

Mysite/

Manage. py

Mysite/_ init _. py

Settings. py

Urls. py

Wsgi. py

You can use

python manage.py runserver 8000

Test whether the django project is successfully created. If an error is prompted, it may be because django uses the sqllite database by default. You only need to add db. sqllite3 In the mange. py directory at the same level.

Then, I plan to run mysite on port 8888.

Modify/etc/apache2/ports. conf.

NameVirtualHost *:80Listen 80

Add

NameVirtualHost *:8888Listen 8888

Indicates that the virtual host of VirtualHost *: 8888 listens to port 8888, and the default VirtualHost *: 80 virtual host listens to port 80.

Next, let apache load our own project website, go to the apache configuration directory/etc/apache/sites-available, and create a configuration file for our website.

sudo vim mysite

Add the following Configuration:

<VirtualHost *:8888>    DocumentRoot /var/www/mysite/mysite    <Directory /var/www/mysite/mysite>        Order allow,deny        Allow from all    </Directory>    WSGIScriptAlias / /var/www/mysite/mysite/wsgi.py</VirtualHost>

Add the following configuration in/etc/apache2/httpd. conf:

<VirtualHost *:8888>    DocumentRoot /var/www/mysite/mysite    <Directory /var/www/mysite/mysite>        Order allow,deny        Allow from all    </Directory>    WSGIScriptAlias / /var/www/mysite/mysite/wsgi.py</VirtualHost>

Add the following code to/var/www/mysite/wsgi. py:

import syssys.path.append("/var/www/mysite/")

Add the website directory to the environment variable. If the website directory is no longer in the environment variable, an error occurs.

Activate my new mysite website and execute the following command:

Sudo a2ensite mysite # Here, mysite is the name of the configuration file created in sites-available.

Reload the website:

sudo service apache2 reload

Then run

sudo service apache2 restart

Restart the apache service.

Open 127.0.0.1: 8888 in the browser and you will see the following page:

Note: If you do not want to create a new website configuration file, add the default configuration in etc/apache2/httpd. conf:

WSGIScriptAlias / "/ var/www/mysite/mysite/wsgi.py"<Directory " /var/www/mysite/mysite">Order Deny,AllowAllow from all</Directory>



For the first configuration, if you have any errors or omissions, please send a reminder or give us some advice. If you have any questions, please leave a message.

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.