Many developers install WAMP or XAMPP in their computers in order to have a local development environment. However after long time working under Ubuntu, having a 3rd party interface… it sucks. It’s really fast to set up the environment but if you want some additional features everything becomes a problem, plus the additional resurces needed for the monitor. I don’t want to talk shit about WAMP or XAMPP developers, they make a really good job, but I want my RAM and CPU!

Unhopfully things are not like Apache 1.3 and PHP4 with a Windows installer, just click-next-finish. Today we’ve 32 and 64 bit versions and it seems that the Apache Foundation doesn’t want to expend resorces maintaining these packages. PHP also has more integration posibilities  with other environments such Nginx and it has no sense to have installers.

But no problem! You can download Apache and compile DIY or download compiled versions for Windows, like ApacheHaus.

WARNING: I need to say that I don’t recomend to use this kind of environment for sites in ptoduction, if you want to host a site please use a good OS like GNU/Linux or BSD.

Download

You need to have in mind if you’ll work under a 32 or 64 bit environment, Apache and PHP should be working under the same architecture in order to work as Apache module. You won’t have this issue if you’ll execute PHP as cgi/fcgi.
There’s no 64bit version for PHP under 5.5.

In this post I’ll use Apache 2.4 and PHP 5.6, both 64 bit.

To download PHP visit windows.php.net/download/ and choose VC11 x64 Thread Safe version. You need to have the right version of the VC (VisualC) installed into your system; at the right side you’ve the links to download the needed VC version. Download it and unpack into C:\php\5.6

From ApacheHaus at apachehaus.com/cgi-bin/download.plx,download the Apache 2.4.x VC14 version. VC14 download link is at PHP site. Once downloaded unpack it at C:\Apache24

It seems to be obvious but it isn’t, Apache and PHP must be placed at the same drive that your OS, so you must replace C: drive for the one with your Windows installation.

There’s no problem changing the folder locations and place all together. Just keep this in mind to replace all the settings.

Starting Up

Once unpacked both Apache and PHP we need to “tell” Apache where is PHP hosted, so we create the file C:\Apache24\conf\extra\httpd-php.conf whith the following

# Load PHP libraries
LoadFile "C:/php/5.6/php5ts.dll"
LoadFile "C:/php/5.6/libpq.dll"
# Load PHP Apache module
LoadModule php5_module "C:/php/5.6/php5apache2_4.dll"

# Associate .php files to PHP handler
<FilesMatch ".php$">
    SetHandler application/x-httpd-php
</FilesMatch>

# Associate .phps files to PHP handler
<FilesMatch ".phps$">
    SetHandler application/x-httpd-php-source
</FilesMatch>

# Set a path to PHP
<IfModule php5_module>
    PHPINIDir "C:/php/5.6"
</IfModule>

# Set default MIME for PHP files to text/html
<IfModule mime_module>
    AddType text/html .php .phps
</IfModule>

Now we edit the C:\Apache24\conf\httpd.conf file and we add the following line before vhosts settings, that’s commented by defualt.

# PHP Settings
Include conf/extra/httpd-php.conf

# Virtual hosts
#Include conf/extra/httpd-vhosts.conf

In PHP, you need to generate the C:\php\5.6\php.ini file. Package includes 2 example files with default settings php.ini-development y php.ini-production, so copy the desired one and rename to php.ini.

It’s important to update the paths in some of the php.ini settings:

713. include_path = ".;c:\php\5.6\includes"

735. extension_dir="c:\php\5.6\ext"

805. upload_tmp_dir="c:\php\5.6\tmp"

1430. session.save_path="c:\php\5.6\tmp"

It’s very recommended but not fully necesary to include Apache and PHP into PATH environment variable. The fastest way (but not the only) is clicking with the secondary mouse button over Start and choose “System”, at the left menu there click over “Advanced System Settings” and then the tab “Avanzaced”, the button “Environment variables…” placed almost at the foot. At the block “System variables” locate  “Path”variable, edit and add 2 adittional paths C:\Apache24\bin and C:\php\5.6.

syspath

After updating path environment you should restar Windows, but you don’t need to do it to continue this tutorial.

Open a console as Administrator. If you had one already open, open a new one in order to get the new environment settings. There execute httpd -k install, then  a firewall security alert will pop up. Allow it or you won’t be able to access hosted sites.

apacheinit

To start or stop the Apache service use httpd -k start o httpd -k stop. httpd -k restart to restart it, as we will need to do each time we change Apache or PHP settings.

IMPORTANT: if Skype is running, port 80 will be in use so Apache won’t be able to use it, so you need to go to Skype settings and disable the use of this port in the options.

skype

If you’re not used with the Console remember that Apache is as Windows service, so you can control it from the service control panel as any other service. (Execute [WIN+R]-> services.msc)

At this panel you can set if Apache will run at Windows start as well, etc.

services_apache

With these settings open a web browser and go to http://localhost where you’ll get the following default ApacheHays page. If don’t, check again the steps.

apachehaus

 

Apache vhosts

If you didn’t notice yet, I’ve some “professional deformation” and I like some things in some ways, and having the websites I’m working on into folders is good, but assign a vhost to each one is much better. Why? Because it’ll be like the production environment and will be easier moving it to the final location. It’s not the same http://localhost/myproject1/ than http://myproject1.local, right? It’s cleaner, but not, you don’t date more men/women because of that, anyway.

First step is telling Windows that the domain “myproject1.local” is us and he doesn’t need to check any DNS server (or it may, you never know what the hell is been cooked under the pants). For this edit %WINDIR%\System32\drivers\etc\hosts file.

hosts file is a bitch, the editor must be run with administrator rights and sometimes from Windows 7 in advance changes don’t take effect unless you overwrite it, editing into another location and then replacing the original file.

Syntax will be <IP address> <domain name>, so remember that any IP address like 127.x.x.x will be locahost. Having many declared domains doesn’t get resources from the system, so you can declare as many IP/domains relations as you need.

In a *nix environment (GNU/Linux, BSD, etc.) this is made into /etc/hosts file.

hosts

After doing this, open the browser and visit the domain. You should get the ApacheHaus default page. If you want access from another computer, sotty, you must set the domain into the remote computer as well. But it can be done!

Well, now we need to set Apache to use vhosts, so edit C:\Apache24\conf\httpd.conf and uncomment the line were is loading vhosts settings. Add also another line to load any vhost file that we will add into the folder conf\sites.

# Virtual hosts
Include conf/extra/httpd-vhosts.conf
Include conf/sites/*.conf

At conf folder create a new folder named “sites” (or whatever you want as it matches with  the settings) and there we’ll adding the .conf files.
Each file may contain one or more vhosts settings, as you like as always. I’d like to have one file by project and I tell you like that.
To disable a site/project, just change file extension or delete it… And then restart Apache, of course.

Each vhost tells Apache to what IP and domain has to response, port, root folder… Even file access rights and other additional settings that may be needed just for this vhost so shouldn’t be global.

# Declare vhost:
# * -> response to any IP address on any network interface
# 80 -> listen to request made at port 80
<VirtualHost *:80>
        ServerAdmin pepitoperez@example.com

	# Domain
        ServerName miproyecto1.local
	# Other domains to listen to
        ServerAlias miproyecto1 myproject1.com

	# Project files location
	DocumentRoot "C:\www\vhosts\myproject1"

	# Access log
	CustomLog logs/miproyecto1.access.log combined
        # Error log
	ErrorLog logs/miproyecto1.error.log

	# Additional settings
	<Directory "C:\www\vhosts\myproject1">
                Options FollowSymlinks MultiViews
                AllowOverride All
		Require all granted
                AcceptPathInfo On
        </Directory>
</VirtualHost>

And that’s it! Add a <VirtualHost> item for each site you want, with their individual settings, if it’s SSL set the port to 443, add the certificates, etc. But that’s another story.

For more information about Apache settings visit https://httpd.apache.org/docs/2.4/es/

 

And that’s all, I hope this post is usefull.

Tags: , , ,

Deja un comentario

This site uses Akismet to reduce spam. Learn how your comment data is processed.