Freebsd apache php fpm

How To Install and Configure PHP-FPM and Apache 2.4 on FreeBSD

Are you looking for instructions about how to install PHP-FPM on FreeBSD or how to configure Apache 2.4 and PHP-FPM? Keep on reading. This is how I did it on my FreeBSD laptop.

NOTE: I’m assuming you are in a hurry and want to get to the point ASAP. While I’m using FreeBSD at the time of this writing, the principles should apply to any *nix. It would be a matter of adjusting paths and, of course, using the correct package manager and package names. Hence, this is not a full fledged tutorial, but a series of pointers in the right direction. I’ll also point to official sources of documentation so you can know here this information comes from.

1. INSTALLING APACHE 2.4

Use the package manager in order to look for (and install) the apache string. Lots of results will be returned, so you can filter using the grep command.

$ pkg search apache | grep -i web apache24-2.4.54 Version 2.4.x of Apache web server . $ sudo pkg install apache24-2.4.54

NOTE: following hier(7), new directories were created under /usr/local , for example /usr/local/www/apache24/ , /usr/local/etc/apache24/ , etc.

You’ll be asked to include apache24_enable=»YES» in your /etc/rc.conf file if you want Apache to start at system boot.

2. INSTALLING PHP

Use the package manager in order to look for (and install) the “ php ” string. You’ll see results for different version of PHP and, as a bonus point, you’ll also see the package name for the PHP module used by Apache ( mod_php74-7.4.30_1 ).

$ pkg search php | grep -i scripting . mod_php74-7.4.30_1 PHP Scripting Language mod_php80-8.0.20_1 PHP Scripting Language mod_php81-8.1.8_1 PHP Scripting Language (8.1.X branch) mod_php82-8.2.0.a3_1 PHP Scripting Language (8.2.X branch) php74-7.4.30 PHP Scripting Language php80-8.0.20 PHP Scripting Language php81-8.1.8 PHP Scripting Language (8.1.X branch) php82-8.2.0.a3 PHP Scripting Language (8.2.X branch) . $ sudo pkg install php74-7.4.30 mod_php74-7.4.30_1

NOTE: following hier(7), new directories were created under /usr/local , for example /usr/local/etc/php (your .ini files are located here), /usr/local/etc/php-fpm.d (PHP-FPM workers configuration files are here), etc.

You’ll be asked to include php_fpm_enable=»YES» in your /etc/rc.conf file if you want PHP-FPM to start at system boot.

The package information displayed at installation time also includes a message telling us to add the following snippet somewhere in the Apache configuration file ( /usr/local/etc/apache24/httpd.conf ):

 SetHandler application/x-httpd-php SetHandler application/x-httpd-php-source 

But If you take a look at the bottom of /usr/local/etc/apache24/httpd.conf file, you’ll see the following statement:

Include /usr/local/etc/apache24/Includes/*.conf

This means that Apache will try to include any .conf file located inside /usr/local/etc/apache24/Includes/ . So, instead of copying the provided snippet directly into httpd.conf , create a new file under the Includes directory, and put the snippet there.

E.g. you can create a new /usr/local/etc/apache24/Includes/php7.4.conf (the file name doesn’t matter).

TIP: a “handler” is an internal Apache representation of the action to be performed when a file is called. Generally, files have implicit handlers, based on the file type. Normally, all files are simply served by the server, but certain file types are “handled” separately.

So, you are basically telling Apache what to do when .php(s) files are requested.

Take into consideration that we’ll be performing a slight change to those settings in a few minutes, as we want Apache to use the “FPM/FastCGI API“, instead of the traditional “Apache 2.0 Handler“, which is what we just configured.

3. CONFIGURING APACHE 2.4 + PHP-FPM

Now that we have PHP + Apache installed:

  1. Get sure that your workers configuration is correct: /usr/local/etc/php-fpm.d/www.conf .
  2. In your /usr/local/etc/apache24/httpd.conf file, uncomment the following lines:
LoadModule php7_module libexec/apache24/libphp7.so . LoadModule proxy_module libexec/apache24/mod_proxy.so . LoadModule proxy_fcgi_module libexec/apache24/mod_proxy_fcgi.so

According to Apache docs, mod_proxy.so and mod_proxy_fcgi.so are required in order to support FastCGI. Remember that this is what FPM is about: FastCGI Process Manager (FPM).

If you look a the examples provided in the Apache documentation, you’ll notice that they look very similar to the snippet we created in the previous section where we added a new handler. The only difference is that in these examples, handlers are based on the configuration of PHP-FPM workers.

In my case, this is how the PHP-FPM handler looks like:

 SetHandler "proxy:fcgi://localhost:9000" 

This is what Apache documentation says about it:

…The example configuration below will pass all requests for PHP scripts to the specified FastCGI server using reverse proxy. This feature is available in Apache HTTP Server 2.4.10 and later. For performance reasons, you will want to define a worker representing the same fcgi:// backend . The benefit of this form is that it allows the normal mapping of URI to filename to occur in the server, and the local filesystem result is passed to the backend. When FastCGI is configured this way, theserver can calculate the most accurate PATH_INFO .

So, now you have at least 2 options:

  1. You replace the handler we created in the previous section with a new handler using the settings of your PHP-FPM workers.
  2. You place the new snippet inside any VirtualHost block where you want it to apply.

I took the second approach and included it only in the VirtualHost block where I want it to apply. It looks as follows:

 ServerAdmin foo@example.com DocumentRoot "/usr/local/www/apache24/SymfonyProjects/current/public" Require all granted SetHandler "proxy:fcgi://localhost:9000" ServerName symfony-projects.local ServerAlias aspirebsd-local.com ErrorLog "/var/log/symfony-projects.local-error_log" CustomLog "/var/log/symfony-projects.local-access_log" common 

Whatever route you take, remember to restart the Apache service afterwards.

$ sudo service apache24 restart

4. TESTING YOUR CONFIGURATION

Create a PHP file calling the phpinfo() function. If everything worked as expected, the “Server API” row at the beginning of the page should indicate we are using “FPM/FastCGI API“, instead of the traditional “Apache 2.0 Handler“.

Attaboy! You should be running Apache 2.4 with PHP-FPM enabled by now, or at least have a better idea what path to follow. As I mentioned earlier, the principles should apply to any *nix platform you are using.

Источник

Apache, PHP-FPM, MariaDB and FreeBSD

I always liked the bsd-like ports that Slackware Linux uses and now that Digital Ocean offers FreeBSD I am having a great time working with it.

Today I want to show you how to setup an FAMP (Apache 2.4, MySQL and PHP on FreeBSD 10), we will be installing Apache using Event MPM, and PHP-FPM.

We will also be using both methods of installation in FreeBSD, first the ports way then, the pkg binary way.

Apache, MySQL and PHP on FreeBSD from ports

Apache 2.4 Event MPM

Let’s first start with Apache, as I said we will be installing the Event MPM which is a threaded MPM and will serve files a lot faster, actually normal files as fast as Nginx

cd /usr/ports/www/apache24/ make install clean 
| [x] ACCESS_COMPAT mod_access compatibility | [x] ACTIONS Action triggering on requests | [x] ALIAS Mapping of requests to different filesystem parts | [x] AUTHZ_CORE Core authorization provider vector module | [x] AUTHZ_HOST Host-based authorization control | [x] DIR Directory request handling |+[x] EXPIRES Expires header control |+[x] HEADERS HTTP header control |+[x] REWRITE Rule based URL manipulation |+[x] VERSION Determining httpd version in config files |+[x] VHOST_ALIAS Mass virtual hosting |+(*) MPM_EVENT MPM worker variant with the goal of consuming | [x] MIME Mapp file-ext. to MIME (recommended) | [x] PROXY Build enabled PROXY modules | [x] PROXY_FCGI FastCGI support module for mod_proxy 

I like to have Virtual sites as separate files and not as part of the monolitic httpd.conf file, so let’s configure Apache that way.

mkdir /usr/local/etc/apache24/conf.d 

Add this line at the end of the file /usr/local/etc/apache24/httpd.conf

Include etc/apache24/conf.d/*.conf 

PHP 5.6

Now it is the turn of PHP, we will build it with PHP-FPM option turned on.

cd /usr/ports/lang/php56 make install 
|+[x] CLI Build CLI version |+[x] CGI Build CGI version |+[x] FPM Build FPM version |+[ ] EMBED Build embedded library |+[ ] PHPDBG |+[ ] DEBUG Enable debug |+[ ] DTRACE Enable DTrace support |+[x] IPV6 Enable ipv6 support |+[ ] MAILHEAD Enable mail header patch |+[x] LINKTHR Link thread lib (for threaded extensions) |+[ ] ZTS Force Zend Thread Safety (ZTS) build 

Finally clean the installation.

We now have PHP 5.6 installed, but we need the modules, so:

cd ../php56-extensions/ make install clean 

Be sure to mark, GD, CURL and MySQL as well as MySQLi

|+[x] CURL CURL support |+[x] GD GD library support |+[x] MYSQL MySQL database support |+[x] MYSQLI MySQLi database support 

Those among the default ones.

MariaDB 10.0

We are going to install MariaDB instead of MySQL, if you do not know already, MariaDB is a fork of MySQL, where new developments are taking place, but complete compatibility is being manteined

We are going to install MariaDB 10.0 which according to the oficial site is:

MariaDB 10.0 is the current stable version of MariaDB. It is built on the MariaDB 5.5 series with backported features from MySQL 5.6 and entirely new features not found anywhere else.

cd /usr/ports/databases/mariadb100-server make install clean 

And keep all defaults. To secure your intallation run:

mysql_secure_installation 

Set a password, and then accept all defaults.

Finishing up installation

Preparing services to run

We need to configure all servers to autostart when the computer starts, to do that you need to edit the vim /etc/rc.conf and add this lines.

apache24_enable="YES" php_fpm_enable="YES" mysql_enable="YES" 

We need to take one more step for Apache. Edit the file /usr/local/etc/httpd.conf and add this line just before the Include…. line.

We can now start all services:

service apache24 start service mysql-server start service php-fpm start 

Make sure you have these modules enabled

LoadModule authn_core_module libexec/apache24/mod_authn_core.so LoadModule authz_core_module libexec/apache24/mod_authz_core.so LoadModule log_config_module libexec/apache24/mod_log_config.so LoadModule expires_module libexec/apache24/mod_expires.so LoadModule headers_module libexec/apache24/mod_headers.so LoadModule version_module libexec/apache24/mod_version.so LoadModule unixd_module libexec/apache24/mod_unixd.so LoadModule vhost_alias_module libexec/apache24/mod_vhost_alias.so LoadModule dir_module libexec/apache24/mod_dir.so LoadModule rewrite_module libexec/apache24/mod_rewrite.so LoadModule mime_module libexec/apache24/mod_mime.so LoadModule actions_module libexec/apache24/mod_actions.so LoadModule alias_module libexec/apache24/mod_alias.so LoadModule authz_host_module libexec/apache24/mod_authz_host.so LoadModule access_compat_module libexec/apache24/mod_access_compat.so LoadModule proxy_module libexec/apache24/mod_proxy.so LoadModule proxy_fcgi_module libexec/apache24/mod_proxy_fcgi.so 

Test installation

We will create a testing site in order to test everything, and be sure it is all setup and ready to work.

Create a folder for your site

mkdir -p /usr/local/www/site1/ chown -R www:www /usr/local/www/site1/ 

Configure a virtual host

Create a file in /usr/local/etc/apache24/conf.d/site1.conf , you can name it any other way you want, and add this content inside:

  ServerAdmin webmaster@example.com DocumentRoot "/usr/local/www/site1/public_html" ServerName freebsd.garron.me ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/www/site1/public_html/$1 ErrorLog "/usr/local/www/site1/logs/error.log" CustomLog "/usr/local/www/site1/logs/access.log" common ScriptAlias /cgi-bin/ "/usr/local/www/site1/cgi-bin/" Options +Indexes +FollowSymLinks +ExecCGI AllowOverride AuthConfig FileInfo Require all granted Allow from all  

Test the configuration

I will need to create a file named phpinfo.php with this contents:

Once saved, I can go to http://freebsd.garron.me/phpinfo.php, and the info of PHP should appear. This is the important line:

That means, you are running PHP-FPM, and this is also a command you want to run to verify the MPM

If you enjoyed the article, please share it

You can also subscribe in different ways

Источник

Читайте также:  Html input number length
Оцените статью