Skip to content

Instantly share code, notes, and snippets.

@soft2help
Created August 25, 2021 10:05
Show Gist options
  • Select an option

  • Save soft2help/a902843b3f8c462c92a353d58696a448 to your computer and use it in GitHub Desktop.

Select an option

Save soft2help/a902843b3f8c462c92a353d58696a448 to your computer and use it in GitHub Desktop.
php compilation to allow thousands of websocket connections
cd ~
sudo apt-get update
sudo apt install libldap2-dev
sudo apt install apache2-dev
sudo apt install libgmp-dev
sudo ln -sf /usr/lib/x86_64-linux-gnu/libldap.so /usr/lib/libldap.so
sudo ln -sf /usr/lib/x86_64-linux-gnu/liblber.so /usr/lib/liblber.so
sudo ln -sf /usr/include/x86_64-linux-gnu/gmp.h /usr/include/gmp.h
wget https://github.com/php/php-src/archive/php-7.2.2.tar.gz
tar --extract --gzip --file php-7.2.2.tar.gz
cd ~/php-src-php-7.2.2/
./buildconf --force
sudo nano /usr/include/x86_64-linux-gnu/bits/typesizes.h
sudo nano /etc/security/limits.conf
sudo shutdown -r now
ulimit -Hn
ulimit -Sn
cd /home/administrador/php-src-php-7.2.2/
sudo apt install -y bison libxml2-dev libcurl4 libcurl4-openssl-dev
cd /etc/php7/apache2/
ln -s /etc/php/7.2/apache2/conf.d config.d
sudo ln -s /etc/php/7.2/apache2/conf.d config.d
sudo ln -s /usr/include/x86_64-linux-gnu/curl /usr/include/
cd /home/administrador/php-src-php-7.2.2/
sudo apt remove libicu-dev
apt-cache madison libicu-dev
sudo apt install libicu-dev=60.2-3ubuntu3
sudo apt install icu-devtools
sudo apt install libicu-dev=60.2-3ubuntu3
sudo apt install icu-devtools=60.2-3ubuntu3
sudo apt install libicu-dev=60.2-3ubuntu3
./configure --prefix=/usr/local/php7 --with-apxs2=/usr/bin/apxs --enable-sysvsem --enable-mbstring --with-curl --with-config-file-path=/etc/php7/apache2 --with-config-file-scan-dir=/etc/php7/apache2/conf.d --enable-pcntl --enable-intl --with-ldap --enable-fd-setsize=100000
sudo make clean
sudo make
sudo make install
sudo make test
/usr/local/php7/bin/php prueba-fd.php
/usr/local/php7/bin/php -i | grep php.ini
sudo rm /etc/php7/apache2/php.ini /etc/php7/apache2/conf.d
sudo mkdir /etc/php7/apache2/conf.d
sudo cp /etc/php/7.2/apache2/conf.d/25-memcached.ini /etc/php7/apache2/conf.d/
sudo cp /usr/lib/php/20170718/memcached.so /usr/local/php7/lib/php/extensions/no-debug-non-zts-20170718/
sudo cp /etc/php/7.2/apache2/conf.d/20-igbinary.ini /etc/php7/apache2/conf.d/
sudo cp /usr/lib/php/20170718/igbinary.so /usr/local/php7/lib/php/extensions/no-debug-non-zts-20170718/
sudo cp /etc/php/7.2/apache2/conf.d/20-msgpack.ini /etc/php7/apache2/conf.d/
sudo cp /usr/lib/php/20170718/msgpack.so /usr/local/php7/lib/php/extensions/no-debug-non-zts-20170718/
sudo cp /etc/php/7.2/apache2/conf.d/20-zmq.ini /etc/php7/apache2/conf.d/
sudo cp /usr/lib/php/20170718/zmq.so /usr/local/php7/lib/php/extensions/no-debug-non-zts-20170718/
cd /usr/local/php7/bin/php
sudo ln -sf /usr/local/php7/bin/php /usr/bin/phpwebsocket
phpwebsocket -v
phpwebsocket -m
#if its necesary add any extension so ...
cd php-src-php-7.2.2/ext/openssl
sudo /usr/local/php7/bin/phpize
sudo make clean
./configure --with-openssl --with-php-config=/usr/local/php7/bin/php-config
sudo make
sudo make install
sudo make test
#can be necesary add to enable openssl
cd /etc/php7/apache2/conf.d
cat "extension=openssl.so" > 20-openssl.ini
# instructions
sudo apt-get update
sudo apt-get install libldap2-dev libldap-2.4-2 libtool-bin libzip-dev lbzip2 libxml2-dev bzip2 re2c libbz2-dev apache2-dev libjpeg-dev libxpm-dev libxpm-dev libgmp-dev libgmp3-dev libmcrypt-dev libmysqlclient-dev mysql-server mysql-common libpspell-dev librecode-dev apache2 bison openssl libssl-dev pkg-config libssl-dev libcurl4-openssl-dev libjpeg-dev libpng12-dev libfreetype6-dev libmcrypt-dev
sudo ln -sf /usr/lib/x86_64-linux-gnu/libldap.so /usr/lib/libldap.so
sudo ln -sf /usr/lib/x86_64-linux-gnu/liblber.so /usr/lib/liblber.so
sudo ln -sf /usr/include/x86_64-linux-gnu/gmp.h /usr/include/gmp.h
sudo apt-get install git
git clone https://github.com/php/php-src
cd php-src/
git checkout PHP-7.0.3
sudo apt-get install autoconf /* puede ser necesario */
./buildconf --force
/* change 100000 by the number that you want achieve in file descriptor */
sudo nano /usr/include/x86_64-linux-gnu/bits/typesizes.h:
#define __FD_SETSIZE 100000
sudo nano /etc/security/limits.conf:
* soft nofile 100000
* hard nofile 100000
#Reboot Server
#Check the FD number
ulimit -Hn
ulimit -Sn
#Go to the folder where we download php-src
cd /home/xxx/php-src
./configure --prefix=/usr/local/php7 --with-config-file-path=/etc/php7/apache2 --with-config-file-scan-dir=/etc/php7/apache2/conf.d --enable-mbstring --enable-zip --enable-bcmath --enable-pcntl --enable-ftp --enable-exif --enable-calendar --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-wddx --enable-intl --with-curl --with-mcrypt --with-iconv --with-gmp --with-pspell --with-gd --with-jpeg-dir=/usr --with-png-dir=/usr --with-zlib-dir=/usr --with-xpm-dir=/usr --with-freetype-dir=/usr --with-t1lib=/usr --enable-gd-native-ttf --enable-gd-jis-conv --with-openssl --with-pdo-mysql=/usr --with-gettext=/usr --with-zlib=/usr --with-bz2 --with-recode=/usr --with-apxs2=/usr/bin/apxs --with-mysqli=/usr/bin/mysql_config --with-ldap --with-xdebug --enable-fd-setsize=100000
sudo make clean
sudo make -j `cat /proc/cpuinfo | grep processor | wc -l`
sudo make install
sudo make test
cd /usr/local/php7/bin
#made an php file with the following content
<?php
$pid = getmypid();
$sockets = array();
$loopNbr = 1025; // This normally could go up to the compile --enable-fd-setsize value, but the limit will be anyway 1024, like if it has been hardcoded somewhere
$sysLimit = min(posix_getrlimit()['soft openfiles'], posix_getrlimit()['hard openfiles']);
$nbrInitialOpenedFiles = (int)exec("ls /proc/$pid/fd | wc -l");
echo "There is initially $nbrInitialOpenedFiles opened files".PHP_EOL."The sysLimit is $sysLimit".PHP_EOL."In some few seconds, we will try to open $loopNbr sockets".PHP_EOL;
sleep(5);
for ($i = 0; $i < $loopNbr; $i++) {
echo $i > ($sysLimit - $nbrInitialOpenedFiles) ? 'Normal crash : openfiles system limitation. Edit /etc/security/limits.conf, increasing soft & hard values' . PHP_EOL : null;
$s = stream_socket_client("127.0.0.1:80", $errno, $errstr, 30, STREAM_CLIENT_ASYNC_CONNECT | STREAM_CLIENT_CONNECT);
if ($s) {
$sockets[$i] = $s;
echo 'Stream n°' . $i . ' opened' . PHP_EOL;
echo(exec("lsof -i -a -p $pid") . PHP_EOL);
} else {exit;}
}
echo ($loopNbr + $nbrInitialOpenedFiles) > 1024 ? 'Well Done.' . PHP_EOL : 'something is wrong please review all instrutions' . PHP_EOL;
$read = $sockets;
stream_select($read, $w = null, $e = null, null, null);
./php --version
./php textsocket.php
# if at the end we dont have Well DOne message it means that we can not made more that 1024 conections.
#php compile
sudo ./configure '--enable-fd-setsize=65536' '--enable-exif' '--enable-ftp' '--enable-sockets' '--enable-shmop' '--enable-mbstring' '--enable-mbregex' '--enable-bcmath' '--enable-calendar' '--with-gd' '--enable-gd-native-ttf' '--with-gettext' '--with-curl' '--disable-debug' '--enable-fpm' '--with-fpm-user=www-data' '--with-fpm-group=www-data' '--with-mysql' '--with-pdo-mysql' '--disable-phpdbg' '--enable-opcache' '--with-zlib' '--with-bz2' '--with-iconv' '--with-mcrypt' '--enable-zip' '--with-tidy' '--with-mysqli' '--with-openssl' '--with-jpeg-dir=/usr/lib/x86_64-linux-gnu'
# Or to install in specific folder as /usr/local/php add --prefix=/usr/local/php
sudo libtool --finish ./libs
sudo a2dismod mpm_worker
sudo a2dismod mpm_even
sudo a2enmod mpm_prefork
sudo a2enmod php7
sudo service apache2 restart
./configure --help
sudo make && make install
sudo make test
#when init websocket server
/usr/local/php7xx --ini
#change location of init file
php -c /home/me/php.ini -f /home/me/websocketserver.php
/usr/local/php7xx servidorwebsocket.php
PHP 7.2
Sudo apt update
sudo apt install -y libzip-dev bison autoconf build-essential pkg-config git-core libltdl-dev libbz2-dev libxml2-dev libxslt1-dev libssl-dev libicu-dev libpspell-dev libenchant-dev libmcrypt-dev libpng-dev libjpeg8-dev libfreetype6-dev libmysqlclient-dev libreadline-dev libcurl4-openssl-dev
#download src
wget https://github.com/php/php-src/archive/php-7.2.2.tar.gz
tar --extract --gzip --file php-7.2.2.tar.gz
#compile
./buildconf --force
CONFIGURE_STRING="--prefix=/etc/php7 --with-bz2 --with-zlib --enable-zip --disable-cgi \
--enable-soap --enable-intl --with-openssl --with-readline --with-curl \
--enable-ftp --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \
--enable-sockets --enable-pcntl --with-pspell --with-enchant --with-gettext \
--with-gd --enable-exif --with-jpeg-dir --with-png-dir --with-freetype-dir --with-xsl \
--enable-bcmath --enable-mbstring --enable-calendar --enable-simplexml --enable-json \
--enable-hash --enable-session --enable-xml --enable-wddx --enable-opcache \
--with-pcre-regex --with-config-file-path=/etc/php7/cli \
--with-config-file-scan-dir=/etc/php7/etc --enable-cli --enable-maintainer-zts \
--with-tsrm-pthreads --enable-debug --enable-fpm \
--with-fpm-user=www-data --with-fpm-group=www-data"
# In the new version libicu the icu-config was removed so you should install version 60 that was available for our distribuition.
WARNING configure
configure: WARNING: unrecognized options: --with-mcrypt, --with-t1lib, --enable-gd-native-ttf, --with-xdebug
mcrypt is deprecated you should use openssl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment