Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jeffsmonteiro/54ac4a001d86a1b64a6024f06402d285 to your computer and use it in GitHub Desktop.
Save jeffsmonteiro/54ac4a001d86a1b64a6024f06402d285 to your computer and use it in GitHub Desktop.
Increate max no of open files limit in Ubuntu 16.04 for Apache2
# maximum capability of system
user@ubuntu:~$ cat /proc/sys/fs/file-max
708444
# available limit
user@ubuntu:~$ ulimit -n
1024
# To increase the available limit to say 200000
user@ubuntu:~$ sudo vim /etc/sysctl.conf
# add the following line to it
fs.file-max = 200000
# run this to refresh with new config
user@ubuntu:~$ sudo sysctl -p
# edit the following file
user@ubuntu:~$ sudo vim /etc/security/limits.conf
# add following lines to it
* soft nproc 200000
* hard nproc 200000
* soft nofile 200000
* hard nofile 200000
root soft nproc 200000
root hard nproc 200000
root soft nofile 200000
root hard nofile 200000
# edit the following file
user@ubuntu:~$ sudo vim /etc/pam.d/common-session
# add this line to it
session required pam_limits.so
# logout and login and try the following command
user@ubuntu:~$ ulimit -n
200000
# default mod_php just run with mpm_prefork and to get
# a good and fast setup for Apache mpm_worker is better (much more)
# but to use mpm_work module is necessary to migrate from mod_php to php-fpm
# remove mod_php
user@ubuntu:~$ apt-get remove libapache2-mod-php7.0
# install php-fpm
user@ubuntu:~$ apt-get -y install php7.0-fpm libapache2-mod-fastcgi
# disable the conflicting modules
user@ubuntu:~$ a2dismod mpm_worker mpm_prefork
# enable the required modules
user@ubuntu:~$ a2enmod actions fastcgi alias rewrite mpm_event
# add the following configuration to /etc/apache2/conf-available/php7.0-fpm.conf
<IfModule mod_fastcgi.c>
SetHandler php7-fcgi .php
Action php7-fcgi /php7-fcgi virtual
Alias /php7-fcgi /usr/lib/cgi-bin/php7-fcgi
FastCgiExternalServer /usr/lib/cgi-bin/php7-fcgi -socket /var/run/php/php7.0-fpm.sock -pass-header Authorization -idle-timeout 3600
<Directory /usr/lib/cgi-bin>
Require all granted
</Directory>
</IfModule>
# enable the php-fpm configuration
sudo a2enconf php7.0-fpm
# restart services
service apache2 restart
service php7.0-fpm restart
# in Apache2 main config /etc/apache2/apache2.conf
<IfModule mpm_worker_module>
ServerLimit 250
StartServers 10
MinSpareThreads 75
MaxSpareThreads 250
ThreadLimit 64
ThreadsPerChild 32
MaxRequestWorkers 8000
MaxConnectionsPerChild 10000
</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment