This is the first in a series of articles detailing how to setup an agile PHP development environment. Each article should be fairly contained, although I would still recommend you read the summary to get the recommendations for how to approach these articles to benefit the most.
Implementation Time(1-5 hours depending on OS, apache skill, and sites)
The implementation time is most dependant on your own skill with apache, followed closely by the configuration changes required by your sites. Finally, if you can't easily setup the method you want to serve PHP(MPM changes, mod_php, suexec, fastcgi, etc.) locally easily this could bork the time table I provided significantly.
Process
The process we will be going by will be simple.
- Get a LAMP stack running.
- Configure httpd.conf
- Configure virtual host for each project
- Configure /etc/hosts(or similar OS specific file) for seperate projects
- Install sites to ~/workspace folder
- (Optional) Configure tcp_wrappers or windows firewall to allow external access
Get a LAMP stack running
I am not going to go into details about this, because it is beyond the scope of the article really. This topic is huge and can be very complicated. If you are running linux, please visit your distributions home page for instructions. Here are some for Gentoo(using mpm-itk):
- I have the following use flags in /etc/make.conf "mysql apache2 php5" as well as APACHE2_MPMS="itk"
- edit /etc/portage/package.use I recommend the following use configurations.
- dev-lang/php doc mysql mysqli xml simplexml hash curl pdo ctype json tokenizer
- www-servers/apache doc rewrite itk
- dev-db/mysql doc
- emerge -av php mysql apache (this is if you don't want to do an apache split e-build)
If you are running OSX and don't mind, send me information to post here about the basics for installation on of setting this up.
Regardless of your OS, I recommend enabling mod_rewrite, mod_setenv, and using the mpm-itk MPM if at all possible(keep in mind this can be a real pain on some distros, its easy with Gentoo =P ). mpm-itk will make apache run as your user so that you dont have to chmod 0777 all the files that you want apache to edit. It is not the best solution for this effect, but the easiest to setup for some.
Configure httpd.conf
So now you have LAMP/WAMP running and are ready to get to my tips for PHP developers. I recommend setting up your httpd.conf to be as minimal as possible. Here is what I recommend:
ServerRoot [path to server root]
#Load Modules ( all of your LoadModule statements here )
ServerName [local hostname of computer]
User apache
Group apache
DirectoryIndex index.php
Include /etc/apache2/modules.d/*.conf
Include /etc/apache2/vhosts.d/*.conf
That's it! The load modules section should be pretty large but this is a really simple configuration. Gentoo does most of this for you. The modules.d folder should contain all of the specific configuration changes you require of each specific module. I use nearly the defaults here. The vhost.d folder we will be going into more detail pretty shortly here. Note that you don't see a default host here.
Configure virtual host for each project
We will be configuring each project as a virtual host. That will allow us to run multiple projects locally without problems. We will also need to configure a default virtual host. This can be a custom page you have put together listing all of your projects or a specific site you want displayed by default. That I will leave to your preference. We will make a series of files in vhost.d numbered and named by project. Here is an example 01_vhost_site1.localhost.conf:
ServerName site1.localhost
ServerAlias site1.localhost
AssignUserID [your user] [your user's group]
ErrorLog /home/[your user]/workspace/logs/site1.localhost-error
DocumentRoot /home/clintv/workspace/site1/
Options -Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
There are some changes you might need to make. The AssignUserID is line is only if you enabled mpm-itk. If you went the fastcgi route, or some other route you will need to add the appropriate line to set the sites user instead. You will need to make sure to create the ~/workspace/logs folder for all of your logs from your sites. You may also want to change the "Allow from all" to "Allow from 127.0.0.1" if you want to prevent external access to the site. I do this with my firewall/tcp_wrappers, personally. I don't like directory browsing, but if you do remove the - in "-Indexes". You will be placing your site at ~/workspace/[project]. This plays well with Eclipse for those that like to use it. If you need subdomains, I recommend keeping them in the projects conf file and duplicating the above for the seperate path to the subdomain.
If you need SSL for a project, then make an additional config file that is for serving the site in SSL. Here is 01_vhost_ssl_site1.localhost.conf:
Listen 443
ServerName site1.localhost
ServerAlias site1.localhost
AssignUserID [your user] [your user's group]
SSLEngine On
SSLCertificateFile /etc/apache2/ssl/site1.crt
SSLCertificateKeyFile /etc/apache2/ssl/site1.key
ErrorLog /home/[your user]/workspace/logs/site1.localhost-error
DocumentRoot /home/clintv/workspace/site1/
Options -Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
There is not much extra here. We have changed the port to 443, setup the SSL certificate and key locations, and are logging to the same log file. You may need to specify a specific local IP(see below) for each SSL site if you are dealing with multiple. You may also prefer to log to a seperate file.
Configure /etc/hosts(or similar OS specific file) for seperate projects
With all of our new virtual host, we need to let our OS know where to find the sites. The file we will be editing is /etc/hosts. On windows, this file is located in windows/system32/drivers/etc/hosts. Here is an example with multiple projects:
127.0.0.1 localhost.localdomain localhost [your hostname]
127.0.0.1 site1.localhost site2.localhost store.site2.localhost site3.localhost
127.0.1.1 site4.localhost
::1 localhost
This is simple enough. I have basically listed off each [project].localhost to resolve to 127.0.0.1. site4 actually requires a seperate SSL ip address, so I have assigned it 127.0.1.1. Also, site2 has a subdomain of store. This would allow me to have a seperate path and emulate the real server conditions of a subdomain.
Install sites to ~/workspace folder
This is fairly easy. Unpack your project to ~/workspace/[project]. This is where you would check out your source code repository. I will explain the way I setup this directory in the article about setting up an Agile PHP build system. As a heads up, I usually put the actual website in a folder named src.
(Optional) Configure tcp_wrappers or windows firewall to allow external access
If you want someone to be able to access the site externally, you will need to configure you firewall so that they can connect. If your in linux, your magic phrase is tcp_wrappers. Here is the lines to enable http and https for /etc/hosts.allow:
HTTP: all
HTTPS: all
If your in windows, then it is the windows firewall. You may also need to configure port forwarding from your router if your hosting across the internet. A good dynamic dns service goes a long way as well. I like to use no-ip because they have a linux client that will update your ip address if it changes. If you do this, I would recommend creating a special vhost conf file for that hostname your sharing. Keep in mind you will only be able to host SSL for 1 site this way unless you have multiple internet ip addresses.
Summary
This article has armed you with the tools to configure apache in a logical way for multiple project development. Feel free to make your own changes, and if you have great personal tips, post them as comments for the rest of the visitors please. =)
0 comments:
Post a Comment