I want to share my experience of translating Apache 2.4 to PCRE2, as even PHP 7 has long supported the PCRE2 library, while the open-source Apache Software Foundation still does not.
Of course, I might be ahead of the Apache release with PCRE2 support, as I am using the sources from the Apache Git, which suggests that PCRE2 support may be in the next release. However, for those who want PCRE2 support in Apache 2.4 right now and do not want to wait for the release, I am sharing one of the methods.
This article assumes that you are compiling all the necessary software from source. The list of software and versions at the time of writing this article:
PCRE2-10.33
APR 1.7.0
APR-util 1.6.1
Apache httpd 2.4.41
Step one: build and compile PCRE2
We will skip the part about downloading the sources from official sources since it is very obvious. So, you have extracted the archive, go to the directory with the PCRE2 sources, and execute the following command for UTF support:
./configure --prefix=/etc/webserver/pcre2-1033 --enable-pcre2-8 --enable-pcre2-16 --enable-pcre2-32 --enable-unicodeIn the prefix, specify your path if you do not want to use the default location for installing the library:
--prefix=/your/path/to/libraryOtherwise, compile without the prefix.
The other commands indicate enabling support for 8-bit, 16-bit, and 32-bit code blocks in PCRE. In this case, the build was performed with them.
And, of course, we compile this using the sequential execution of the commands:
make
make install If everything is fine and the compilation went without errors, we move on to the next step.
Step two: integrate the PCRE2 library into APR
Since Apache compiles the sources using APR, we need to connect the library in APR itself, otherwise, there may be errors about unknown functions in the Apache sources because we will be using the new PCRE2 functions.
We will skip the part about downloading the sources from official sources since it is very obvious. So, you have extracted the archive and executed the APR configuration:
./configure --prefix=/etc/webserver/apr-170Of course, in the prefix, specify your path if you do not want to use the standard location for installing the library, or do not specify it:
--prefix=/your/path/to/libraryAfter configuring, go to the directory: /etc/webserver/srcsrv/apr-1.7.0/build
Or: /your/path/to/library/build
Find the file apr_rules.mk in this directory and add the following line to the end of the file:
EXTRA_LIBS=-lrt -lcrypt -lpthread -ldlLinking the library:
-lpcre2-8 -L/your/path/to/library pcre2/libWe save and go to the root directory of the APR sources: /your/path/to/library.
We compile our modified APR:
make
make installIf everything is fine and the compilation went without errors, we move on to the next step.
Step three: build APR-util from sources for Apache
You downloaded this library from the official source, go to the root folder of the unpacked APR-util archive, and enter the following commands sequentially:
./configure --prefix=/etc/webserver/apr-util-161 --with-apr=/your/path/to/library/apr
make
make installOf course, in the prefix, specify your path if you do not want to use the standard location for installing the library, or do not specify it:
--prefix=/your/path/to/libraryHere we also link our APR:
--with-apr=/your/path/to/library/aprStep four: download sources from the Apache git for PCRE2 support
Important: Download the sources from the git of the latest version.
We need to download two source files, ap_regex.h and util_pcre.c, links below:
Now go to your Apache httpd source directory, and build Apache with the following commands:
./configure --prefix=/etc/webserver/apache-2441 --with-apr=/your/path/to/library/apr --with-apr-util=/your/path/to/library/apr-util --with-pcre=/your/path/to/library/pcre2/bin/pcre2-configOf course, in the prefix, specify your path if you do not want to use the standard location for installing the library, or do not specify it:
--prefix=/your/path/to/Apache httpdAlso, specify any additional commands for building Apache at your discretion, such as enabling or disabling modules and libraries.
Next, go to your Apache httpd source directory, mine is:
/etc/webserver/srcsrv/httpd-2.4.41You will naturally go to your directory, replace in the directory:
/etc/webserver/srcsrv/httpd-2.4.41/includeThe file ap_regex.h with the one we downloaded from the Apache git.
We also go to the directory:
/etc/webserver/srcsrv/httpd-2.4.41/serverReplace the file util_pcre.c with the one we downloaded from the Apache git.
Now we need to add the PCRE2 connection in Apache itself, we need to find the file ap_config_auto.h, it is located in the directory:
/etc/webserver/srcsrv/httpd-2.4.41/includeAt the very beginning of this file, insert the following lines:
/* Load PCRE2 */
#define HAVE_PCRE2 1 Well, now we are ready for the actual moment of compiling Apache httpd with PCRE2 support.
Go to your Apache httpd source directory and compile it by sequentially executing the commands:
make
make installNow, if everything went well and without errors, you will have a built and compiled Apache httpd with PCRE2 support, which means positive changes in the Apache modules using PCRE regular expressions, one of which is the Module rewrite.
In conclusion, this method allows using PCRE2 before the official release from the Apache Software Foundation; I hope a version with PCRE2 support will be released soon.
Also, during the testing of standard .htaccess, no errors occurred; if anyone has errors, please write in the comments.
P.S.
I was a bit stressed about using two different versions of PCRE in my stack, and I decided to resolve this.
Source: habr.com
