WebFaction
Community site: login faq
2
1

I'd like to use PHP7 with curl, with HTTP/2 support enabled. What do I need to do?

asked 12 Aug '16, 18:22

seanf
12.2k42136
accept rate: 37%


First, you'll need to be on one of WebFaction's CentOS7 servers, because the build tools on our older servers are too old to build nghttp2. If you're not on a CentOS7 server (web500 or higher, or one of our cloud plans) then you can request a migration via the control panel.

Once you're on a CentOS7 server, run the following commands to install nghttp2 and an updated curl in your home directory:

mkdir -p ~/tmp ~/src
export TMPDIR=~/tmp
cd ~/src
git clone https://github.com/tatsuhiro-t/nghttp2.git
cd nghttp2
autoreconf -i
automake
autoconf
./configure --prefix=$HOME
make
make install
cd ~/src
export LDFLAGS="-L$HOME/lib $LDFLAGS"
export LD_LIBRARY_PATH=$HOME/lib:$LD_LIBRARY_PATH
export PATH=$HOME/bin:$PATH
wget http://curl.haxx.se/download/curl-7.46.0.tar.bz2
tar -xvjf curl-7.46.0.tar.bz2
cd curl-7.46.0
./configure --prefix=$HOME --with-nghttp2=$HOME --with-ssl
make
make install

Next, run curl --vesion to ensure that you have the new version (7.46) installed. The output should look something like this:

curl 7.46.0 (x86_64-pc-linux-gnu) libcurl/7.46.0 OpenSSL/1.0.1e
zlib/1.2.7 libssh2/1.4.3 nghttp2/1.14.0-DEV
Protocols: dict file ftp ftps gopher http https imap imaps ldap
ldaps pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp 
Features: IPv6 Largefile NTLM NTLM_WB SSL libz HTTP2 UnixSockets

Next, run curl --http2 -I nghttp2.org to ensure that HTTP/2 is working as expected. The output should look something like this:

HTTP/1.1 101 Switching Protocols
Connection: Upgrade
Upgrade: h2c

HTTP/2.0 200
date:Fri, 12 Aug 2016 18:12:19 GMT
content-type:text/html
last-modified:Thu, 21 Jul 2016 14:06:56 GMT
etag:"5790d700-19e1"
accept-ranges:bytes
content-length:6625
x-backend-header-rtt:0.001839
server:nghttpx nghttp2/1.14.0-DEV
via:2 nghttpx
x-frame-options:SAMEORIGIN
x-xss-protection:1; mode=block
x-content-type-options:nosniff

Next, create a PHP 7 application via the control panel if you've not done so already.

Next, create a file named my_php.cgi in your PHP 7 application's directory with the following contents, replacing "your_username" and "your_appname" with your user name and app name respectively:

#!/bin/sh

USERNAME=your_username
APPNAME=your_appname

# set the env variables
export LD_LIBRARY_PATH=/home/$USERNAME/lib

# run the original script via PHP CGI
exec /home/php-cgi/php70.cgi /home/$USERNAME/webapps/$APPNAME$SCRIPT_URL

Next, make the my_php.cgi script executable:

chmod 711 ~/webapps/your_appname/my_php.cgi

Finally, create a .htaccess file in your PHP 7 application directory (if you don't have one already) and add the following to it:

Action php_custom /my_php.cgi
<FilesMatch .php$>
    SetHandler php_custom
</FilesMatch>

That's it! When you're done, your PHP 7 application will be able to use the newer curl library with HTTP/2 support.

Note that this technique can be adapted to almost any CGI or PHP application that needs to use custom libraries or environment variables.

permanent link

answered 12 Aug '16, 18:22

seanf
12.2k42136
accept rate: 37%

edited 17 Aug '16, 18:39

Following the instructions above e and getting help from the amazin webfaction support I managed to configure on my shared hosting account HTTP/2 support for a PHP7 application.

For my purposes I needed to install a custom OpenSSL too, because the versione of the installed one is too old. Because of this I compiled a custom PHP 7 from scratch to get it works. Following are the step I made to achieve a full custom installation of PHP 7 + cURL + ngHTTP2 + openSSL 1.0.2g:

mkdir -p ~/tmp ~/src
export TMPDIR=~/tmp

ngHTTP2

cd ~/src
git clone https://github.com/tatsuhiro-t/nghttp2.git
cd nghttp2
autoreconf -i
automake
autoconf
./configure --prefix=$HOME
make
make install

export LDFLAGS="-L$HOME/lib $LDFLAGS"
export LD_LIBRARY_PATH=$HOME/lib:$LD_LIBRARY_PATH
export PATH=$HOME/bin:$PATH

OpenSSL

cd ~/src
wget http://www.openssl.org/source/openssl-1.0.2g.tar.gz
tar -xzf openssl-1.0.2g.tar.gz 
cd openssl-1.0.2g
./Configure --prefix=$HOME linux-x86_64
make
make install

cURL

cd ~/src
wget http://curl.haxx.se/download/curl-7.47.1.tar.bz2
tar -xvjf curl-7.47.1.tar.bz2
cd curl-7.47.1
LIBS="-ldl" ./configure --disable-shared --prefix=$HOME --with-nghttp2=$HOME --with-ssl=$HOME
make
make install

test cURL

curl --http2 -I nghttp2.org

PHP

cd ~/src
wget http://php.net/distributions/php-7.0.10.tar.bz2
tar -xjf php-7.0.10.tar.bz2 
cd php-7.0.10
./configure --prefix=$HOME --with-pdo-mysql --with-pdo-pgsql=/usr/pgsql-9.1 --enable-bcmath --enable-calendar --enable-exif --enable-ftp --enable-mbstring --enable-soap --enable-zip --with-curl=$HOME --with-freetype-dir --with-gd --with-gettext --with-gmp --with-iconv --with-jpeg-dir --with-kerberos --with-mhash --with-mysql --with-mysqli --with-openssl=$HOME --with-pgsql=/usr/pgsql-9.1 --with-png-dir --with-regex --with-xmlrpc --with-xsl --with-zlib-dir --with-config-file-path=$HOME/lib --without-pear --enable-sockets --enable-intl --with-mysql-sock=/var/lib/mysql/mysql.sock
make
make install

Convert your app to use your own PHP installation

cd ~/webapps/example_app
cp $HOME/bin/php-cgi my_php.cgi
echo "
Action php_custom /my_php.cgi
<FilesMatch .php$>
SetHandler php_custom
</FilesMatch>
">>.htaccess

Don't forget to make the CGI script executable:

chmod 711 my_php.cgi
chmod 711 .
permanent link

answered 06 Sep '16, 04:49

ovosodo
313
accept rate: 0%

edited 06 Sep '16, 15:19

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×15
×9
×3

question asked: 12 Aug '16, 18:22

question was seen: 6,875 times

last updated: 06 Sep '16, 15:19

                              
WEBFACTION
REACH US
SUPPORT
LEGAL
© COPYRIGHT 2003-2021 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM