Apache configuration Basics on Ubuntu

Source: Internet
Author: User
Tags mime file

Configuration File

The main configuration file of Apache officially released is httpd. conf. The server will also read a file containing the MIME file type, and its file name is determined by the typesconfig command. The default value is mime. types.

The main configuration file of Apache in Ubuntu/Debian is/etc/apache2/apache2.conf, and the file containing the MIME file type is/etc/mime. types.

In addition, to split the configuration by logic, you can use the include command and wildcard to append many other configuration files. The following configuration file is attached to Ubuntu:

# Configurations that contain dynamic modules:

Include/etc/apache2/mod-enabled/*. Load

Include/etc/apache2/mod-enabled/*. conf

# Include your own configurations:

Include/etc/apache2/httpd. conf

# Configurations that contain port listening:

Include/etc/apache2/ports. conf

# Contains a general configuration statement segment:

Include/etc/apache2/CONF. d/[^. #] *


# Configuration commands containing virtual hosts:

Include/etc/apache2/sites-enabled/[^. #] *

Configuration File Syntax

* Each row contains an instruction, and the backslash (/) can be used at the end of the row to indicate continued rows.

* Commands in the configuration file are case-insensitive, but the command parameters (argument) are usually case-sensitive.

* Rows starting with "#" are treated as annotations and ignored during reading. The annotation cannot appear behind the instruction.

* Blank lines and blank characters before the command will be ignored during reading. Therefore, indentation can be used to keep the configuration level clear.

Both the main configuration file and the configuration file contained in the include statement should comply with the Apache configuration syntax.

Apache module

Apache is a modular server with two compilation methods:

* Static Compilation: one-time compilation of core modules and required modules


Advantage: Fast Running


Disadvantage: to add or delete modules, you must recompile the entire Apache

* Dynamic compilation: only the core module and DSO (dynamic shared object) module are compiled-mod_so


Advantage: Each module can be compiled independently and loaded with the loadmodule command at any time. commands used for a specific module can be included with commands to take effect conditionally.


Disadvantage: relatively slow running speed

Apache in Ubuntu is dynamically compiled. Configure the module in Ubuntu:

1. modify the configuration file of the relevant modules in the directory of mod-available/as needed.

2. Use the a2enmod command to enable the module. Use the a2dismod command to disable the module.

For example, to enable the rewrite module, run the following command:

Sudo a2enmod rewrite

To disable the CGI Module, run the following command:

Sudo a2dismod CGI

Directive Scope

It is divided by the scope of commands. Apache has two types of commands:

* Common commands: Common commands are valid for the entire server.

* Container command: It can be placed in the container to implement additional configurations for specific access resources. It is only valid for specific access resources.

Common Containers in Apache include:

* And: Used to implement additional configuration for the specified directory

* And: Used to implement additional configuration for the specified file

* And: Used to implement additional configuration for the specified URL

* Containers are paired. All configuration commands used in containers should be placed in <> and, for example:


* All the preceding container commands with "match" have the same function as the corresponding container commands without "match", but you can use a regular expression to describe resources.

* These containers can also be nested for finer configuration.

In addition, Apache also supports multiple sites at the same time, known as a virtual host. Commands placed in the container are only valid for specific virtual hosts.

Although most commands can be included in any container, special cases are also used. To query the configuration segments that a command can be applied to, you can view the scope items of the command in the Apache manual.

Directory-based configuration file

In addition to the main configuration file, Apache can also use special files distributed in the directory tree of the entire website for decentralized configuration. Such a special configuration file is called a directory-based configuration file. These special files are usually called. htaccess, but you can also use the accessfilename command to change its name.

Obviously, the directive scope in the directory-based configuration file (. htaccess file) is the directory that stores it and all its subdirectories.

* The syntax of the. htaccess file is the same as that of the main configuration file. Commands in the container can appear in the. htaccess file. The specific commands that can appear are determined by the AllowOverride command in the main configuration file.

* Because each access request from the client to the server reads the. htaccess file, modifications to these files take effect immediately, that is, the server does not need to load or restart the configuration file.

Basic configuration instructions

Server id command

* Servername: the server is used to identify its host name and port number.

* Serveradmin: the Administrator email address contained in the error message returned from the server to the client.

* Serversignature: configure the footer of the page generated by the server (error message, FTP directory list of mod_proxy, mod_info output)

* ServerTokens: determines whether the server's "server:" response header contains information about the server's operating system type and compiled module.

* UseCanonicalName: determines how Apache constructs the SERVER_NAME and server_port commands in the URL.

File Location command

* DocumentRoot: the root directory of the main document tree visible on the network.

* Errorlog: location where error logs are stored

* Lockfile: when the value of the acceptmutex command is fcntl or flock, the location of the lock file used by Apache

* Pidfile: Set the file used by the server to record the PID of the parent process (monitoring process.

* Scoreboardfile: a file that stores Coordination Data. Apache uses scoreboard to communicate between parent and child processes)

* Serverroot: Specify the basic directory for Server Installation

Tighten Apache configuration in Ubuntu

For the actual product server, the Apache configuration should be tightened first. Therefore, you can create a new local_configs.conf file in the/etc/apache2/CONF. d/directory and add the following Configuration:

/Etc/apache2/CONF. d/local_configs.conf

# Set servername

Servername www.jamond.net

# Protect/

# Forbid default access to File System Locations

Order deny, allow

Deny from all

# Prevent use of. htaccess files in all directories

# Apart from those specifically enabled.

AllowOverride none

# Restrict display of available server information

Serversignature off

ServerTokens Production

Apache management tools

Apache kit contains many executable programs, some of which are management tools. This section only describes apache2ctl.


Apache2ctl is the frontend program of apache2 server. The administrator can help control the apache2 background daemon functions.

Apache2ctl script has two operation modes:

* As a simple front-end program of apache2, set all necessary environment variables, start apache2, and pass all command line parameters.

* As the sysv initialization script, it accepts simple parameters of a word, such as start, restart, and stop, and translates them into appropriate signals and sends them to apache2.

* In the Ubuntu apache2 STARTUP script/etc/init. d/apache2, apache2ctl is called for control.

Apache2ctl has two syntax formats:


Apache2ctl action options

Command Parameters Using apache2ctl

* Display apache version


* Display apache version and compilation Parameters


* Output a brief description of an available command line option


* Output a list of modules that are statically compiled on the server. It does not list the modules Dynamically Loaded using the loadmodule command


* Output a list of commands, including the valid parameters and use regions of each command.


* Only perform a syntax check on the configuration file .. If "-D dump_vhosts" is specified, the detailed information about the virtual host configuration is displayed.


* Display the settings read and parsed from the configuration file (currently, only the settings of the virtual host are displayed)


Action options for using apache2ctl

* Start The apache2 daemon. If it has been started, an error is generated.

Apache2ctl start

* Stop The apache2 daemon.

Apache2ctl stop

* Restart the apache2 daemon. If the daemon is not running, start it. Before restarting the daemon, this command automatically checks the configuration file using configtest to ensure that Apache will not die.

Apache2ctl restart

* A Brief status report is displayed.

Apache2ctl status

* Elegantly restart apache2
Background daemon. If the daemon has not been started, start it. It differs from standard restart in that it does not interrupt the currently opened connection or immediately close the log. This means that if
To use the log rolling script, you must add a real delay before processing logs to ensure that the old log files are closed before being processed. Before restarting the daemon, this command uses
Configtest automatically checks the configuration file to ensure that Apache will not die.

Apache2ctl graceful

* Gracefully stop The apache2 background daemon. It differs from the standard stop in that it does not interrupt the currently opened connection or immediately close the log.

Apache2ctl graceful-stop

* Execute a configuration file syntax check. It parses the configuration file and reports syntax OK or specific syntax error details.

Apache2ctl configtest

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.