Ubuntu apache2 Server Configuration

Source: Internet
Author: User
Tags web hosting mediawiki



Deploy the Django-developed project to the APACHE2 server. Log my configuration process.



The Apache,django,mod_wsgi,python version is as follows. Different versions are similar.


ii  apache2                           2.2.22-1ubuntu1.6 Apache HTTP Server metapackage
ii  python-django                     1.3.1-4ubuntu1.11                   High-level Python web development framework
ii  libapache2-mod-wsgi               3.3-4ubuntu0.1                      Python WSGI adapter module for Apache
ii  python 2.7.3-0ubuntu2.2                    interactive high-level object-oriented language (default version)
First, apache2 configuration instructions


After installing Apache2 on Ubuntu with Apt-get install apache2, the configuration files are in the/et/apache2 directory.



Basic principle



The apache2 automatically reads the configuration information of the/etc/apache2/apache2.conf file at startup, and the different configuration items are distributed in different files by function, and are included in the Apache2.conf Master profile for easy management. In fact, the Apache2 master configuration file is only one, that is, apache2.conf, the others are included in the. You can put all the configuration in apache2.conf or any of the configuration files, but divided into different files will make us more convenient to manage, why not?


1. apache2.conf configuration file


This file is the master configuration file for Apache, including three levels of configuration.


    • Controls the global configuration of the Apache server execution process.
    • Defines the configuration of the parameters of the primary service or default server, which responds to requests that are not processed by virtual host. This type of configuration also provides default values for all virtual hosts configurations.
    • The virtual hosts-related configuration allows the same Apache service process to handle requests sent to different IP addresses or host names.
### Section 1: Global Environment
#ServerRoot: The root directory of the apache server. The main configuration file, the logs are all in this directory.
#Note that when the path ends, do not add a slash. The default is /etc/apache2
#ServerRoot "/etc/apache2"

LockFile ${APACHE_LOCK_DIR}/accept.lock

The process ID number is recorded in the file when the #apache service starts.
# This needs to be set in /etc/apache2/envvars
PidFile ${APACHE_PID_FILE}

#Connection timeout, in seconds
Timeout 300

#Do you allow persistent connections?
KeepAlive On

#Maximum number of requests when persistently connected
MaxKeepAliveRequests 100

KeepAliveTimeout 5


# prefork MPM
<IfModule mpm_prefork_module>
    StartServers 5
    MinSpareServers 5
    MaxSpareServers 10
    MaxClients 150
    MaxRequestsPerChild 0
</IfModule>


# worker MPM
<IfModule mpm_worker_module>
    StartServers 10
    ServerLimit 100
    MinSpareThreads 50
    MaxSpareThreads 200
    ThreadsPerChild 64
    MaxClients 6400
    MaxRequestsPerChild 0
</IfModule>

# event MPM
<IfModule mpm_event_module>
    StartServers 2
    MinSpareThreads 25
    MaxSpareThreads 75
    ThreadLimit 64
    ThreadsPerChild 25
    MaxClients 150
    MaxRequestsPerChild 0
</IfModule>

# These need to be set in /etc/apache2/envvars
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}

After the #apache service starts, look for files with .htaccess suffixes in each directory. These files are used as additional configurations.
AccessFileName .htaccess
#Next few lines to prevent .htaccess and .htpassword files from being accessed by web clients
<Files ~ "^\.ht">
    Order allow,deny
    Deny from all
    Satisfy all
</Files>

#DefaultType is the MIME type of the browser access, set to None, let the browser resolve itself
DefaultType None

# HostnameLookups: Log the names of clients or just their IP addresses
# e.g., www.apache.org (on) or 204.62.129.132 (off).
HostnameLookups Off

#Error log file directory, the default is configured here, can be overloaded in <VirtualHost>
#log file name when starting with ‘/‘ will cause a conflict, do not start with ‘/’, will default to the server root directory prefix
#如"foo.log" will be added to the ServerRoor directory "/etc/apache2" to become "/etc/apache2/foo.log"
ErrorLog ${APACHE_LOG_DIR}/error.log

#LogLogging information, the value can be: debug, info, notice, warn, error, crit, alert, emerg.
LogLevel warn

#import module configuration file
Include mods-enabled/*.load
Include mods-enabled/*.conf
#Import all user profiles
Include httpd.conf
#import port listening configuration file
Include ports.conf


# The following directives define some format nicknames for use with
# a CustomLog directive (see below).
# If you are behind a reverse proxy, you might want to change %h into %{X-Forwarded-For}i
#
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent


#import general configuration directory
Include conf.d/

#importing the configuration of the virtual host
Include sites-enabled/ 
View Code2. Apache2 other configuration files and subdirectories
#2.2.22 version of apache2 configuration directory
[email protected]: /etc/apache2# tree -L 1
.
├── apache2.conf global configuration
├── apache.pem
├── conf.d general configuration file storage location
├── envvars environment variable
├── httpd.conf
├── magic
├── mods-available installed modules
├── mods-enabled enabled modules
├── ports.conf httpd service port information
├── sites-available available site information
├── sites-enabled Site information that has been enabled, and the files in it are soft links to the /etc/apache2/sites-available/ file.


The 2.4.7 version of the apache2 configuration file directory is as follows






Character



Configure Apache website character encoding,/etc/apache2/conf.d/charset file uncomment #adddefaultcharset UTF-8



Ports.conf



Modify the port,/etc/apache2/ports.conf file changes namevirtualhost *:80 change to Namevirtualhost x.x.x.x:80



Sites-available and sites-enabled Directories



The sites-available directory is the content that is available, but it does not work, and it only works if you connect to the sites-enabled directory with Ln. The sites-enabled directory holds the configuration file that really works, storing some symbolic links to the sites-available directory. Therefore, Apache has more than one virtual host configured, each virtual host configuration is placed under the sites-available, then the virtual host is disabled and enabled is very convenient. It is enabled when a connection to a virtual host configuration file is established under Sites-enabled. If you want to close a virtual host, you only need to delete the corresponding symbolic link, not to change the configuration file.



Mods-available and mods-enabled Directories



Similar to the above-mentioned sites-available, sites-enabled, these two directories are the configuration files and links that hold the Apache function module. For example, after installing the PHP module with apt-get install PHP5, there are php5.load, php5.conf, and links to the two files in both directories. The same is true for installing MOD-WSGI. This directory structure is very handy for enabling and deactivating an Apache module.



After configuring the Apache server, the point is to specify the location of the project root directory, ubuntu default is/var/www. You can see it in the default in the/etc/apache2/sites-available directory


[email protected]:/etc/apache2/sites-available# cat default <VirtualHost *:80> ServerAdmin [email protected]

        DocumentRoot /var/www <Directory /> Options FollowSymLinks
                AllowOverride None </Directory>
        <Directory /var/www/> Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all </Directory> ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin"> AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg.  LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/" <Directory "/usr/share/doc/"> Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>
View Code


This default configuration is not required at the time of official release, so do not add to the Sites-enable directory. To access your own projects, you need to configure a virtual host.


Second, configure the virtual host


The virtual host configured here is the same server that handles more than one domain name at a time. Different domains access the same or different directories on the same server (that is, the same IP).



Valid site configurations are in the/etc/apache2/sites-available directory.


1. <VirtualHost> Grammar


<VirtualHost> and </VirtualHost> are used to split a set of configurations that work only on specific virtual hosts. when the server receives a document request for a specific virtual host, it uses the configuration that is encapsulated in the <VirtualHost> configuration segment.



Grammar:



<virtualhost addr[: Port] [addr[: Port]]...>



</VirtualHost>



The addr can be:


    • IP address of the virtual host
    • The full domain name of the virtual host IP address
    • Character "*"
    • The string "_default_", associated with an IP-based virtual host to capture all IP addresses that do not match
2. Configure the Djagno project's virtual host through WSGI


The first step is to create the xxx.org virtual host in/etc/apache2/sites-available


[email protected]: /etc/apache2/sites-available# cat xxx.org
<VirtualHost *:80>
# Here to note, the directory is the project directory, modified according to the actual situation, the following django.wsgi file needs to be manually created
WSGIScriptAlias / /var/www/stack/wsgi/django.wsgi
Alias /site_media /var/www/stack/static_resource
ServerName www.xxx.org
#DirectoryRestrictions on the root directory
<Directory /var/www/stack/static_resource>
     Options None FollowSymLinks #followsymlinks indicates whether symbolic links are allowed, the default is disabled
     AllowOverride None # tag prohibits users from overloading the directory configuration file (.htaccess), which is not recommended for normal sites.
     Order allow, deny # is treated with allow priority, not explicitly stated to allow rejection
     Allow from all #clearly indicates that all access is allowed
</Directory>

<Directory /var/www/stack/wsgi>
     #AuthType Basic
     #AuthName "xxx"
     #AuthUserFile /var/www/access
     #Require valid-user
     Order allow,deny
     Allow from all
</Directory>
ErrorLog /etc/apache2/xxx.org.error.log
LogLevel warn
</VirtualHost>


The second step is to create a new Django.wsgi in the/var/www/stack/wsgi directory.


[email protected]:/var/www/stack/wsgi# cat django.wsgi  import os import sys import django.core.handlers.wsgi from django.conf import settings # Add this file path to sys.path in order to import settings sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), ‘../..‘))
os.environ[‘DJANGO_SETTINGS_MODULE‘] = ‘stack.settings‘ sys.stdout = sys.stderr

STACK_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))

sys.path.append(STACK_PATH)

DEBUG = True

application = django.core.handlers.wsgi.WSGIHandler()


The Sys.path.insert () parameter is the parent directory of the project catalog, which is modified according to the project catalog.



os.environ[' django_settings_module ']= ' stack.settings ', this stack.settings is the setting file under the project directory, which needs to be modified according to the project directory name.



The Django.wsgi file name can be arbitrarily taken, but be sure to match the name of the wsgiscriptalias configuration in the virtual host.



Resource Link: Mod_wsgi Implement a detailed explanation of some configuration commands



The third step, the last step of the configuration of the content is only "effective" virtual host, the actual effect can be put under the/etc/apache2/sites-enabled folder. So use the ln command to create a pair of associated files:


Ln-s/etc/apache2/sites-available/xxx.org/etc/apache2/sites-enabled/xxx.org


Fourth step, check the syntax, restart the Web service



As a precaution, check the syntax before restarting the service:


sudo apache2ctl configtest


If there are no errors, restart Apache2


#/etc/init.d/apache2 Restart


Or use the service apache2 Restart command. You can now access the Django project through www.xxx.org.


3. Configure the wiki virtual server


"This wiki uses MediaWiki, written in PHP, so don't Mod-wsgi"



The first step is to create a MediaWiki virtual host in/etc/apache2/sites-available


 [Email protected]:/etc/apache2/sites-available[email protected]: /etc/apache2/sites-available# cat wiki.xxx.org
<VirtualHost *:80>
Alias /wiki /var/lib/mediawiki
Alias /index.php /var/lib/mediawiki/index.php
#域配置
ServerName wiki.xxx.org

<Directory /var/lib/mediawiki/>
     Options FollowSymLinks
     AllowOverride None
     Order allow,deny
     Allow from all
</Directory>
#log文件Configuration
ErrorLog /etc/apache2/wiki.xxx.org.error.log
LogLevel warn
</VirtualHost> 


The second step, the last step of the configuration of the content is only "effective" virtual host, the actual effect can be put under the/etc/apache2/sites-enabled folder. So use the ln command to create a pair of associated files:


Ln-s/etc/apache2/sites-available/wiki.xxx.org  /etc/apache2/sites-enabled/wiki.xxx.org


Step three, check the syntax, restart the Web service



As a precaution, check the syntax before restarting the service:


sudo apache2ctl configtest


If there are no errors, restart Apache2


# /etc/init.d/apache2 Restart


Or use the service apache2 Restart command. You can now access the wiki via wiki.xxx.org/index.php.



Resources Link:



Apache Web Hosting Documentation



Apache Virtual Host Configuration



Ubuntu apache2 Server Configuration


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.