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.Or you could just write the directory to httpd or apache2 configuration file, which I myself prefer.Source code viewer
mkdir /var/www chmod 751 /var/www chown http:users /var/www rmdir /srv/http ln -s /var/www/ /srv/httpProgramming Language: Bash
Source code viewer
--- a/httpd.conf +++ b/httpd.conf @@ -DocumentRoot "/srv/http" -<Directory "/srv/http"> +DocumentRoot "/var/www" +<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.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
mkdir /var/www/site1 mkdir /var/www/site2Programming Language: Bash
Since hosts file does not support wildcard, we need dnsmasq. So download dnsmasq and set it to start at boot.Source code viewer
<VirtualHost *:80> ServerAdmin info@example.com ServerName sub.localhost ServerAlias *.localhost VirtualDocumentRoot /var/www/%-2+ Options -Indexes +FollowSymLinks <Directory /var/www/*> Options Indexes FollowSymLinks Allow from all AllowOverride All Order Allow,Deny Require all granted </Directory> ErrorLog /var/log/httpd/.localhost-error_log CustomLog /var/log/httpd/.localhost-access-log combined </VirtualHost>Programming Language: Text
Source code viewer
--- /etc/dnsmasq.conf +++ /etc/dnsmasq.conf @@ -cache-size=128 +cache-size=1000 @@ +address=/localhost/127.0.0.1 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
yaourt -S php-gd # OR sudo apt-get install php-gd # OR sudo yum apt-get install php-gd # What I would install in default: yaourt -S php-gdProgramming 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
--- a/php.ini +++ b/php.ini @@ -;extension=gd.so +extension=gd.so @@ -;extension=mysqli.so +extension=mysqli.so -;extension=pdo_mysql.so +extension=pdo_mysql.so @@ -;extension=zip.so +extension=zip.so +extension=json.soProgramming Language: Diff
Performance
PHP
Source code viewer
--- /etc/php/php.ini +++ /etc/php/php.ini @@ ; Increase on systems where PHP opens many files. ; http://php.net/realpath-cache-size -;realpath_cache_size = 16k +realpath_cache_size = 256k @@ ; Maximum execution time of each script, in seconds. ; http://php.net/max-execution-time -max_execution_time = 30 +max_execution_time = 60 @@ ; Maximum amount of memory a script may consume. ; http://php.net/memory-limit -memory_limit = 128M +memory_limit = 256M @@ ; Maximum allowed size for uploaded files. ; http://php.net/upload-max-filesize -upload_max_filesize = 2M +upload_max_filesize = 10MProgramming Language: Diff
Apache
Source code viewer
vim /etc/httpd/conf/httpd.conf # Comment out "LoadModule negotiation_module modules/mod_negotiation.so". # Comment out "Include conf/extra/httpd-multilang-errordoc.conf". # Comment out "Include conf/extra/httpd-languages.conf". # Comment out "LoadModule autoindex_module modules/mod_autoindex.so". # Comment out "Include conf/extra/httpd-autoindex.conf". # Comment out "LoadModule env_module modules/mod_env.so". systemctl restart httpdProgramming Language: Bash
MySQL
MySQL performance configuration tuning / tweakingNB: After every configuration file changes you have to restart apache2 (httpd daemon for some) or mysql.