A code is worth a thousand words

My perfect web server configuration on localhost

You are here

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

mkdir/var/www

chmod751/var/www

chown http:users /var/www

rmdir/srv/http

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

--- 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.

Source code viewer

mkdir/var/www/site1

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

<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

Since hosts file does not support wildcard, we need dnsmasq. So download dnsmasq and set it to start at boot.

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

sudoapt-get install php-gd

# OR

sudoyumapt-get install php-gd

# What I would install in default:

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".