17 May 2015

Apache default configuration can be a bit slow in case of today's standard web applications. Especially on local environment where you develop your application, class, framework or plain old PHP from scratch. Basis of this configuration is Archlinux, but you should be able to configure it to any operating system that you might be using, cause my.cnf, php.ini and httpd.conf or apache.conf are the same on every platform. This configuration is for Apache2. If you are looking for an alternative then nginx is popular poweruser alternative.

Change root directory:

If you don't like where your operating system is putting your projects, you can configure it to anywhere you like. In Arch Linux web root directory is in /srv/http. I prefer /var/www myself. You could make it into symbolic link, as this example shows you.
Source code viewer
  1. mkdir /var/www
  2. chmod 751 /var/www
  3. chown http:users /var/www
  4. rmdir /srv/http
  5. ln -s /var/www/ /srv/http
Programming Language: Bash
Or you could just write the directory to httpd or apache2 configuration file, which I myself prefer.
Source code viewer
  1. --- a/httpd.conf
  2. +++ b/httpd.conf
  3. @@
  4. -DocumentRoot "/srv/http"
  5. -<Directory "/srv/http">
  6. +DocumentRoot "/var/www"
  7. +<Directory "/var/www">
Programming Language: Diff

Running multiple sites on localhost

Create at least two directories for two sites to test it out. You can also put in index.php or html to test if everything works as it should.
Source code viewer
  1. mkdir /var/www/site1
  2. mkdir /var/www/site2
Programming Language: Bash
Configure virtual hosts with wildcard to allow apache figure out which site to show to you. Uncomment "Include conf/extra/httpd-vhosts.conf" from "/etc/httpd/conf/httpd.conf". Edit vhosts configuration file "vim /etc/httpd/conf/extra/httpd-vhosts.conf" and set up your virtuals.
Source code viewer
  1. <VirtualHost *:80>
  2. ServerAdmin info@example.com
  3. ServerName sub.localhost
  4. ServerAlias *.localhost
  6. VirtualDocumentRoot /var/www/%-2+
  7. Options -Indexes +FollowSymLinks
  9. <Directory /var/www/*>
  10. Options Indexes FollowSymLinks
  11. Allow from all
  12. AllowOverride All
  13. Order Allow,Deny
  14. Require all granted
  15. </Directory>
  17. ErrorLog /var/log/httpd/.localhost-error_log
  18. CustomLog /var/log/httpd/.localhost-access-log combined
  19. </VirtualHost>
Programming Language: Text
Since hosts file does not support wildcard, we need dnsmasq. So download dnsmasq and set it to start at boot.
Source code viewer
  1. --- /etc/dnsmasq.conf
  2. +++ /etc/dnsmasq.conf
  3. @@
  4. -cache-size=128
  5. +cache-size=1000
  6. @@
  7. +address=/localhost/
Programming Language: Diff

PHP extensions

If you are going to use some CMS or framework, you will probably need some extensions for your web server. For an example, most cms's require gd library for image processing.

Install php-gd extensions to your server

Depending on your operating system the installation might vary by the package manager. For Mac or Windows there might be some more difficult ways.
Source code viewer
  1. yaourt -S php-gd
  2. # OR
  3. sudo apt-get install php-gd
  4. # OR
  5. sudo yum apt-get install php-gd
  7. # What I would install in default:
  8. yaourt -S php-gd
Programming Language: Bash

Edit php.ini

Edit php.ini with your favourite text-editor in Linux you can use command line tool vim for an example: "vim /etc/php/php.ini".
Source code viewer
  1. --- a/php.ini
  2. +++ b/php.ini
  3. @@
  4. -;extension=gd.so
  5. +extension=gd.so
  6. @@
  7. -;extension=mysqli.so
  8. +extension=mysqli.so
  9. -;extension=pdo_mysql.so
  10. +extension=pdo_mysql.so
  11. @@
  12. -;extension=zip.so
  13. +extension=zip.so
  14. +extension=json.so
Programming Language: Diff



Source code viewer
  1. --- /etc/php/php.ini
  2. +++ /etc/php/php.ini
  3. @@
  4. ; Increase on systems where PHP opens many files.
  5. ; http://php.net/realpath-cache-size
  6. -;realpath_cache_size = 16k
  7. +realpath_cache_size = 256k
  8. @@
  9. ; Maximum execution time of each script, in seconds.
  10. ; http://php.net/max-execution-time
  11. -max_execution_time = 30
  12. +max_execution_time = 60
  13. @@
  14. ; Maximum amount of memory a script may consume.
  15. ; http://php.net/memory-limit
  16. -memory_limit = 128M
  17. +memory_limit = 256M
  18. @@
  19. ; Maximum allowed size for uploaded files.
  20. ; http://php.net/upload-max-filesize
  21. -upload_max_filesize = 2M
  22. +upload_max_filesize = 10M
Programming Language: Diff


Source code viewer
  1. vim /etc/httpd/conf/httpd.conf
  2. # Comment out "LoadModule negotiation_module modules/mod_negotiation.so".
  3. # Comment out "Include conf/extra/httpd-multilang-errordoc.conf".
  4. # Comment out "Include conf/extra/httpd-languages.conf".
  5. # Comment out "LoadModule autoindex_module modules/mod_autoindex.so".
  6. # Comment out "Include conf/extra/httpd-autoindex.conf".
  7. # Comment out "LoadModule env_module modules/mod_env.so".
  8. systemctl restart httpd
Programming Language: Bash


MySQL performance configuration tuning / tweaking
NB: After every configuration file changes you have to restart apache2 (httpd daemon for some) or mysql.