Skip to main content

Installing apache and php on debian



Certain content management systems do not work with the nginx and php-fpm combination , and require an apache web server with the php module. The most common reason for this is the need to use the rewrite module for the site to work.



Install the base components with one command. Dependencies will be installed automatically.

  # aptitude install apache2-mpm-itk libapache2-mod-php5 php5-curl php5-mysqlnd php5-gd 

The content of the article:

Configure apache

First turn on mod_rewrite. He will definitely need it if your site will use CNC.

  # a2enmod rewrite 

It will not be necessary to include mod_php, because during installation it will be enabled automatically.



I also recommend disabling the following modules if you don’t need them:

  # a2dismod -f autoindex deflate setenvif status 

This will allow you to reduce memory consumption.

Open the /etc/apache2/conf-available/charset.conf file and uncomment the following line:


  AddDefaultCharset UTF-8 

This directive indicates that the default encoding will be UTF-8. If your sites use a different encoding, for example, CP-1251, then you need to specify it either right there or in the parameters of the virtual host.

Save the changes.

Open the /etc/apache2/conf-available/security.conf file and convert the ServerTokens OS line to ServerTokens Prod . This will hide the apache version for clients.

ServerSignature On string ServerSignature On to ServerSignature Off .

Save the changes.

Open the /etc/apache2/apache2.conf file for editing and find the KeepAlive On parameter. If, after configuring apache, you install nginx and you will proxy requests from it, then this parameter needs to be converted to KeepAlive Off , since this function (maintaining connection with the client) will be performed by nginx. If you plan to use apache as your primary web server, this feature should be configured.

MaxKeepAliveRequests - the maximum number of requests to maintain a permanent connection. For best performance, it is recommended to set a high value. For a start, the default value is 100.

KeepAliveTimeout is the time in seconds to wait for the next request from the same client.

Save the changes and restart apache.

Open the /etc/apache2/mods-available/mpm_prefork.conf file.

The parameters in this file should be specified depending on site traffic and server resources. To get started, try to start the site with a minimal configuration and watch it work. If it turns out that resources are insufficient, increase them.

Give default values ​​to this view:

  StartServers 2
 MinSpareServers 2
 MaxSpareServers 6
 MaxRequestWorkers 32
 MaxConnectionsPerChild 0

StartServers - the number of child server processes created at startup.

MinSpareServers - the minimum number of idle processes.

MaxSpareServers - the maximum number of idle processes.

MaxRequestWorkers - the maximum number of connections that will be processed simultaneously.

MaxConnectionsPerChild - the maximum number of connections, after processing which, the child process will be terminated. May be useful in dealing with memory leaks.

Also save the changes and restart apache: service apache2 restart .

Creating a virtual host

A virtual host is a group of configuration parameters for a specific site. It includes an accepted domain, email administrator, paths to log files, connection of php handlers, etc.

I like to host virtual host configurations in one file - apache2.conf. If you need to massively replace a value for all sites, you will not need to open all the configuration files of virtual hosts in turn.

First, disable the default site.

  # a2dissite 000-default 

At the end of the /etc/apache2/apache2.conf file, write the following directive:

  <VirtualHost *: 80>

 </ Virtualhost> 

This directive groups all site parameters. That she is a virtual host. The following parameters are written inside this directive.

ServerName - your domain without www. (For example, example.com.)

ServerAlias - domain with www (www.example.com)

DocumentRoot - the directory where the files of your site are located. For example, / var / www /.

ServerAdmin - site administrator email.

ErrorLog - location of the error log file. As a rule, they are located in the / var / log / apache2 / directory. The alias $ {APACHE_LOG_DIR} is used for it. Example:

  ErrorLog $ {APACHE_LOG_DIR} / example.com .error.log 

LogLevel warn . We change nothing, we register it like this. Sets the level of error logging.

CustomLog - Location and type of access-log site. The default type is combined. Although there are other types. Example:

  CustomLog $ {APACHE_LOG_DIR} /example.com.access.log combined 

In both examples, example.com is replaced with your domain without www.

We add settings for mpm-itk, since we installed this particular module. What does he give? Allows you to handle php scripts on behalf of the user who owns the site.

  <IfModule mpm_itk_module>
 AssignUserId user group
 </ IfModule> 

Here you need to replace user with the user name and group with the name of the group in whose home folder the sites are located.

We define settings for directories.

  <Directory />
 Options + FollowSymLinks
 AllowOverride None
 Require all granted
 </ Directory>
 <Directory / var / www>
 Options -Indexes + FollowSymLinks 
 AllowOverride All
 Order allow, deny
 allow from all
 </ Directory> 

Replace the directory / var / www with the one where your sites are located.

We connect the handler php-scripts.

  <IfModule mod_php5.c>
 php_admin_flag engine on
 php_admin_value open_basedir "/ var / www: / tmp / sessions: / tmp / upload_tmp"
 php_admin_value upload_tmp_dir / tmp / sessions
 php_admin_value session.safe_path / tmp / upload_tmp
 php_admin_value sendmail_path "/ usr / sbin / sendmail -t -i -fmail@example.com"
 </ IfModule> 

open_basedir as a value, we specify the colon directories where php will have access: a directory with a site, a directory for storing session files, a directory for downloading temporary files.

upload_tmp_dir - directory for storing temporary files. When you upload a picture (or something else) to the site, the downloaded file is first placed in this directory, and then in the final location.

session.safe_path - directory for storing sessions.

For security reasons, these two directories should not be kept in a public place. Create them in the user's home directory where your sites are located. Allow access to them only to this user (rights 0750).

sendmail_path - sendmail_path path and parameters. As a rule, sendmail is located in the / usr / sbin directory. After the -f parameter, you should specify an email, without separating with a space.

Save the apache2.conf configuration file, restart apache:

  service apache2 restart 

Testing Customized Bundles

In the site directory, create a file index.php. Write the code in it:

  <? php phpinfo ();  ?> 

Then open your website in a browser and see if php information is displayed. If yes, then the server is successfully configured and you can place the site in the intended for him directory.

Otherwise, you should analyze the error logs, check the settings in the configuration files.



How do you rate the article?
Звёзд: 1Звёзд: 2Звёзд: 3Звёзд: 4Звёзд: 5 ( 2 ratings, average: 5.00 out of 5)
Loading...

” 2 Comments “ Installing apache and php on Debian

  1. Thank. My name is Sergey. 65 years old. uch. doctor. SPb. Got VDS hosting. On it the site was established: egmontgroup.ru
    According to your article: php7. Also postgreSQL 9.4 and Apache 2.4
    Basically normal. On the local computer Debian 8.6 jessey (on VDS is the same). I installed all the same. But already days and nights I can not start Apache. In the logs it is not clear to me, nothing happened on the VDS.
    And what about VDS hosting (there is only ssh (I use putty).) Copy some configs, and indeed. ?
    Sincerely. Thank. My email is sergeygsd@yandex.ru
    I also liked switching work between php versions.

Add a comment

Your email will not be published.