Centos7 apache httpd install and configure the django project, centos7django

Source: Internet
Author: User

Centos7 apache httpd install and configure the django project, centos7django
1. Install the httpd service

Apache is an Apache HTTP server in centos7. The following explanation of httpd is Apache HTTP Server. To install apache, you must install httpd.

httpd.x86_64 : Apache HTTP Server

Installation:

# yum install httpd

Set httpd service startup

[root@yl-web httpd]# /sbin/chkconfig httpd onNote: Forwarding request to 'systemctl enable httpd.service'.ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'

Start the httpd service

[root@yl-web httpd]# /sbin/service httpd startRedirecting to /bin/systemctl start  httpd.service

Access ip address verification, successful!

Ii. Configuration

The default configuration file directory of httpd is

[root@yl-web httpd]# cd /etc/httpd/[root@yl-web httpd]# lsconf  conf.d  conf.modules.d  logs  modules  run

The main configuration file is/etc/httpd/conf/httpd. conf.

Configure the directory stored in/etc/httpd/conf. d.

1. Main configuration file

Take a look at the useful configuration items in the main configuration file httpd. conf.

# Server root directory
ServerRoot "/etc/httpd" # Port
# Listen 12.34.56.78: 80 Listen 80 # The domain name + port is used to identify the server. You can also use an ip address without a domain name.
# ServerName www.example.com: 80 # do not access the root directory
<Directory/> AllowOverride none Require all denied </Directory> # document Directory
DocumentRoot "/var/www/html" # Restrict/var/www directory access
<Directory "/var/www"> AllowOverride None # Allow open access: require all granted </Directory> # Restrict/var/www/html Directory Access <Directory "/var/www/html"> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> # default encoding
Adddefacharcharset UTF-8 # EnableMMAP offEnableSendfile on
# Include other configuration files

IncludeOptional conf. d/*. conf
2. Download and configure mod_wsgi

Run apache apxs extension before installing mod_wsgi.

Http-devel is for apxs. After yum, you can search for it with whereis apxs and compile and use it later.

# yum install -y httpd-devel

Download

[root@yl-web collectedstatic]# yum install mod_wsgi

Add the following configuration in httpd. conf:

LoadModule  wsgi_module modules/mod_wsgi.so

This configuration is used to connect django. wsgi and load the project by apache.

Configure django wsgi

Create wsgi in the project directory, and create django. wsgi as follows:

import osimport sysimport django.core.handlers.wsgifrom django.conf import settings# Add this file path to sys.path in order to import settingssys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), '..'))os.environ['DJANGO_SETTINGS_MODULE'] = 'lxyproject.settings'sys.stdout = sys.stderrDEBUG = Trueapplication = django.core.handlers.wsgi.WSGIHandler()

When configuring wsgi,

  • You must configure the project path to the system path, because you need to find the settings. py configuration file through the project path. That is, sys. path. insert (0, OS. path. join (OS. path. dirname (OS. path. realpath (_ file __)),'.. ')).
  • DJANGO_SETTINGS_MODULE must point to the settings. py file of the project.

After you modify the wsgi configuration, you must restart the httpd service.

3. Configure the django project Virtual Host

Add the configuration file lxyproject. conf in/etc/httpd/conf. d. The content is as follows:

<VirtualHost *:80>WSGIScriptAlias / /srv/lxyproject/wsgi/django.wsgiAlias /static/ /srv/lxyproject/collectedstatic/ServerName 10.1.101.31#ServerName example.com#ServerAlias www.example.com<Directory /srv/lxyproject/collectedstatic>    Options Indexes  FollowSymLinks    AllowOverride None    Require all granted</Directory><Directory /srv/lxyproject/wsgi/>    Require all granted</Directory>ErrorLog   /etc/httpd/logs/lxyproject.error.logLogLevel warn</VirtualHost>

Where

  • WSGIScriptAlias directly tells apache that in this virtual host, the request/is sent to WSGI for processing, that is, the django. wsgi configured in the project will be specified.
  • Alias Indicates access/static/directly obtained from DocumentRoot without WSGI processing.

Now you can access the django project through the IP address configured on the apache server.

Iii. resource links

How to use django with mod_wsgi

 

Starof, the author of this article, is constantly learning and growing because of the changing knowledge. The content of this article is also updated from time to time. To avoid misleading readers and facilitate tracing, Please repost the Source: Ghost.

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.