Apache with mod php

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Set up clean Django server for PHP projects. Apache with mod_php, mod_rewrite, MariaDB, PHP 7.0+

alexey-goloburdin/debian-set-up-for-php

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Debian Server Set Up for LAMP Server (Apache, PHP, MariaDB)

In this guide we will set up clean Debian server for PHP projects. We will configure secure SSH connection, install from Debian repositories and from sources all needed packages and ware it together for working PHP projects.

sudo apt-get update ; \ sudo apt-get install -y vim mosh tmux htop git curl wget unzip zip gcc build-essential make 
sudo vim /etc/ssh/sshd_config AllowUsers www PermitRootLogin no PasswordAuthentication no 

Restart SSH server, change www user password:

sudo service ssh restart sudo passwd www 
sudo apt-get install -y zsh sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)" 
sudo apt install -y mariadb-server mariadb-client sudo mysql_secure_installation 

Login with root user is available just with sudo :

So, create database and user and give him privileges to new database:

sudo mariadb -u root CREATE DATABASE new_db_name COLLATE 'utf8_general_ci'; CREATE USER new_db_user IDENTIFIED BY 'some_password'; GRANT ALL privileges ON new_db_name .* TO new_db_user; 

Install Apache, create code directory in user home directory:

sudo apt install -y apache2 apache2-utils cd mkdir code cd code mkdir newproject sudo chown www-data:www-data /home/www/code/ -R 
sudo apt install -y php7.0 libapache2-mod-php7.0 php7.0-mysql php-common php7.0-cli php7.0-common php7.0-json php7.0-opcache php7.0-readline php7.0-mbstring php7.0-xml php7.0-gd 
sudo a2enmod php7.0 sudo systemctl restart apache2 
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/newproject.conf sudo vim /etc/apache2/sites-available/newproject.conf 
 DocumentRoot /home/www/code/newproject ServerName newproject.your-site.ru 

Enable site config in Apache, enable mod_rewrite :

sudo a2ensite newproject.conf sudo a2enmod rewrite 

Configure Apache for .htaccess files:

sudo vim /etc/apache2/apache2.conf 
 Options FollowSymLinks AllowOverride All Order allow,deny Allow from all 
sudo service apache2 restart 

About

Set up clean Django server for PHP projects. Apache with mod_php, mod_rewrite, MariaDB, PHP 7.0+

Читайте также:  What is shallow copy in java

Источник

PHP Configuration Tips

RHEL 7 provides the Apache HTTP Server version 2.4 and PHP version 5.4.

The most common configuration for Apache httpd and PHP uses, but this has some limitations and drawbacks:

  • a single PHP version of mod_php can be used
  • mod_php run in the httpd process, without any isolation
  • mod_phpis only supported for the prefork MPM

This article will explain how to configure Apache httpd to delegate PHP scripts execution to a backend using the FastCGI protocol, how to use a more recent PHP version, how to run multiple PHP versions, and how to improve Apache httpd performance.

The Apache httpd package available in RHEL provides all features needed to use such configuration.

1. Switching to php-fpm

1.1. Remove mod_php

It is recommended to remove or disable mod_php to reduce the memory footprint of each httpd process.

You can either remove the php package, which only provides this module:

or simply disable it by commenting out the LoadModule directive in /etc/httpd/conf.modules.d/10-php.conf.

# disabled # LoadModule php5_module modules/libphp5.so

1.2. Install php-fpm

You now can install the php-fpm and enable its service.

yum install php-fpm systemctl start php-fpm systemctl enable php-fpm

Notice: php-fpm package is available in the optional channel, which has to be enabled.

To configure PHP scripts execution, edit or create the /etc/httpd/conf.d/php.conf file:

The following lines prevent .user.ini files from being viewed by Web clients.

Allow php to handle Multiviews:

Add index.php to the list of files that will be served as directory indexes:

Following line, enable the http authorization headers:

SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1

Redirect the PHP scripts execution to the FPM backend.

 SetHandler "proxy:fcgi://127.0.0.1:9000" 

If you have some php_value directive in this file, you need to remove them, they are only for mod_php.

Now you can (re)start the web server and a simple PHP test page with;

Читайте также:  Https elearning otr ru mod book view php id 1417 chapterid 2228

it will show you are now running PHP through the FatCGI backend.

PHP Version 5.4.16 Server API= FPM/FastCGI

1.3. PHP tuning

The main FPM configuration file is /etc/php-fpm.conf, which have a lot of comments explaining each option.

FPM can run various pools, each one running PHP scripts with possible different options, the default pool (www) configuration file is /etc/php-fpm.d/www.conf, which also have lot of comments.

1.3.1. php_value, php-flag

PHP Options can be set using the php_value, php_admin_value, php_flag and php_admin_flag directives:

  • with mod_php, in the Apache httpd configuration files.
  • with FPM, in the pool configuration files.

1.3.2. .htaccess

Additional options can be set in a specific directory:

  • with mod_php, using a .htaccess file.
  • with FPM, using a .user.ini file (php_* keywords are not needed).

1.3.3. Process tuning

FPM run as a daemon and launch various processes to be able to handle various requests simultaneously, and offers various modes:

  • pm = ondemand, child is only started when a connection is open and stopped when idle, suitable for development environment
  • pm = dynamic, a set of idle processes is always running, more processes can be started if needed, suitable for production
  • pm = static, a fixed set of processes is always running, suitable for production, can be better for performance

1.4. Apache HTTP Server tuning

1.4.1. Threaded MPM

By default, the Apache HTTP Server uses a set of processes to manage incoming requests (prefork MPM).

As we now don’t use mod_php we can switch to a threaded MPM (worker of an event) so a set of threads will manage the requests, reducing the number of running processes and the memory footprint, and improving performance, especially when a lot of static files are served.

Switch the used MPM in the /etc/httpd/conf.modules.d/00-mpm.conf configuration files.

# disabled # LoadModule mpm_prefork_module modules/mod_mpm_prefork.so # disabled # LoadModule mpm_worker_module modules/mod_mpm_worker.so LoadModule mpm_event_module modules/mod_mpm_event.so

1.4.2. Unix Domain Socket

By default, FPM listens for incoming requests on a network socket but can use a Unix Domain Socket, which can slightly improve performance.

In FPM pool configuration:

listen = /run/php-fpm/www.sock listen.owner = apache listen.mode = 0660

In Apache httpd configuration:

SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost"

1.4.2. Separate frontend and backend servers

By default, FPM listens for incoming requests on a local network socket. Of course, it can run on a separate server, another virtual machine or a container (docker instance)

In FPM pool configuration:

listen = 10.0.0.2:9000 listen.allowed_clients = 10.0.0.1

In Apache httpd configuration:

SetHandler "proxy:fcgi://10.0.0.2:9000"

1.4.3 Multiple php backends

To be able to handle more simultaneous requests, we may want to balance the load between various PHP backends, which is easy.

Apache httpd configuration sample, with 3 backends:

# Load balancer creation BalancerMember fcgi://10.0.0.2:9000 BalancerMember fcgi://10.0.0.3:9000 BalancerMember fcgi://10.0.0.4:9000 # Redirect PHP execution to the balancer SetHandler "proxy:balancer://phpfpmlb" 

2. Running a recent PHP version

RHEL provides PHP version 5.4 which was the current version when RHEL-7 was released, but which can be too old for some recent projects.

Читайте также:  Css оформление главной страницы

PHP versions 5.6 and 7.0 are supported on RHEL today as part of Red Hat Software Collections (RHSCL), and PHP version 7.1 is currently in Beta testing with the 3.0 update of RHSCL.

In the example below, we’ll use version 7.0, but it can be used for the others available versions.

2.1. Installation

Install the Software Collection, after having enabled the RHSCL channel:

Install the FPM service for this version:

yum install rh-php70-php-fpm

Install any needed additional extensions:

yum install rh-php70-php-mbstring rh-php70-php-pgsql rh-php70-php-opcache

Tips: compare the list of available extensions to ensure everything needed is available.

php --modules | tee /tmp/54 scl enable rh-php70 'php --modules' | tee /tmp/70 diff /tmp/54 /tmp/70

Tips: never rely on the package name, but prefer extension name (e.g. php-mysqli or rh-php70-php-simplexml), as the package layout may change across versions.

2.2. Switch to newer PHP versions

When running FPM, this is as simple as stopping the old version service and starting the new one:

systemctl stop php-fpm systemctl start rh-php70-php-fpm

2.3. Additional packages

The Software Collections provide the same set of PHP extensions than standard packages in RHEL.

As users are used to finding some additional extensions, in the EPEL repository, an additional extension can be found in the community centos-sclo-sclo repository, for more information search sclo-php on https://www.softwarecollections.org/.

3. Running multiple versions of PHP

As PHP execution is redirected to the FastCGI service using the SetHandler directive, this can be set per vhost, project or directory.

In the example below, we will run both PHP version 5.4 from base system (for some legacy applications, already configured above) and PHP version 7.1 simultaneously.

3.1. Installation

Install the Software Collection, after having enabled the RHSCL beta channel:

yum install rh-php71 rh-php71-php-fpm rh-php71-php-mbstring rh-php71-php-opcache .

Configure FPM to listen to a different port than the one used by the default php-fpm service, in /etc/opt/rh/rh-php71/php-fpm.d/www.conf.

Ensure this port is not blocked by SELinux:

semanage port -a -t http_port_t -p tcp 9071
systemctl start rh-php71-php-fpm

Now, it is possible to choose PHP version for each directory, from Apache httpd configuration file.

# Use PHP 7.1 by default SetHandler "proxy:fcgi://127.0.0.1:9071" # Some legacy application use PHP 5.4  SetHandler "proxy:fcgi://127.0.0.1:9000" 

4. Conclusion

I hope this small article has shown the various benefits of switching to FPM for your PHP scripts:

  • process isolation between frontend (httpd) and backend (fpm)
  • performance improvement
  • running modern PHP version
  • running multiple PHP versions

Last updated: October 18, 2018

Источник

Оцените статью