| 1 | == Installing and configuring critical helper packages == |
| 2 | |
| 3 | Being able to send and receive email is often part of the web application development process. |
| 4 | |
| 5 | == Installing PHP, Apache, and MySQL == |
| 6 | |
| 7 | PHP and Apache should be already installed. You can check your version of PHP and Apache with the following two, separate commands in a Terminal window (Applications > Utilities > Terminal): |
| 8 | |
| 9 | {{{ |
| 10 | php -i |
| 11 | }}} |
| 12 | |
| 13 | Hopefully you're running php5. If not, you'll need to install it: http://php.net/downloads.php |
| 14 | |
| 15 | {{{ |
| 16 | cd /etc/apache2 |
| 17 | httpd -v |
| 18 | }}} |
| 19 | |
| 20 | If you are unable to complete the commands above, then you may need to install Apache from Unix source into /Library/Apache2: http://httpd.apache.org/download.cgi#apache22 |
| 21 | |
| 22 | It's unlikely that your mac has mysql installed already, so you need to download the latest version from here: http://dev.mysql.com/downloads/mysql. Download and run the mysql-5.5.xxx-xxx.pkg file, which will automatically install to /usr/local. To put a control in your system preferences pane and start mysql, in the .dmg file, run MySQL.prefPane by double-clicking. Start mysql and also ensure that mysql always starts on boot. |
| 23 | |
| 24 | == Configuring the environment == |
| 25 | |
| 26 | The goal of the configuration is to make it as easy as possible to setup a new development environment for a new project. |
| 27 | |
| 28 | Each new project will have a unique, non-public domain name based on the name of the workstation. Each project will have a corresponding directory in your home directory where you can place php and other files to be served by apache. |
| 29 | |
| 30 | In this example, let's assume the workstation is named "junior" and the username is "admin." A new project called "mayfirst" would have the development domain name: mayfirst.local (below we will edit the /etc/hosts file so that this non-public domain name will resolve to the development workstation). |
| 31 | |
| 32 | Create a projects directory in your home directory: |
| 33 | |
| 34 | {{{ |
| 35 | mkdir ~/projects |
| 36 | }}} |
| 37 | |
| 38 | There are a few tweaks that must be done before OS X will work with our setup: |
| 39 | |
| 40 | {{{ |
| 41 | echo "export PATH=\$PATH:/usr/local/mysql/bin" >> ~/.profile |
| 42 | sudo mkdir /var/mysql |
| 43 | sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock |
| 44 | }}} |
| 45 | |
| 46 | We need to do a lot of configuration to apache, which is almost all in the file /etc/apache2/httpd.conf. Start by commenting-out this line: |
| 47 | {{{ |
| 48 | #ServerRoot "/usr" |
| 49 | }}} |
| 50 | |
| 51 | and uncommenting the following line: |
| 52 | {{{ |
| 53 | LoadModule php5_module libexec/apache2/libphp5.so |
| 54 | }}} |
| 55 | |
| 56 | and commenting-out this first line and adding the second: |
| 57 | {{{ |
| 58 | #DocumentRoot "/Library/WebServer/Documents" |
| 59 | }}} |
| 60 | |
| 61 | Just below in the same file, /etc/apache2/httpd.conf, modify the default directory settings: |
| 62 | {{{ |
| 63 | <Directory /User/admin/projects> |
| 64 | Options All |
| 65 | Options FollowSymLinks |
| 66 | AllowOverride All |
| 67 | Order deny,allow |
| 68 | Deny from all |
| 69 | Allow from 127.0.0.1 |
| 70 | </Directory> |
| 71 | }}} |
| 72 | |
| 73 | There is now an entire block that must be commented-out regarding DocumentRoot. Insert a "#" in front of everything all the way to and including </Directory>. It starts with: |
| 74 | {{{ |
| 75 | <Directory "/Library/WebServer/Documents"> |
| 76 | }}} |
| 77 | |
| 78 | Also modify these lines so that they are as they appear below, which are sprinkled throughout the rest of this very long file and then save: |
| 79 | {{{ |
| 80 | #Include /private/etc/apache2/extra/httpd-userdir.conf |
| 81 | Include /private/etc/apache2/extra/httpd-vhosts.conf |
| 82 | Include /private/etc/apache2/extra/httpd-default.conf |
| 83 | }}} |
| 84 | |
| 85 | Apache can be configured so that it dynamically sets the document root based on the domain name. Now, we want to drastically modify /etc/apache2/extras/httpd-vhosts.conf. It's best to actually create a clean file. Do this by backing up the old one and then editing a clean file with the same name: |
| 86 | {{{ |
| 87 | mv /etc/apache2/extras/httpd-vhosts.conf /etc/apache2/extras/httpd-vhosts.conf.back |
| 88 | sudo nano /etc/apache2/extras/httpd-vhosts.conf |
| 89 | }}} |
| 90 | |
| 91 | While in editing mode, add this, save, and close: |
| 92 | {{{ |
| 93 | <Directory /User/admin/projects> |
| 94 | AllowOverride All |
| 95 | </Directory> |
| 96 | VirtualScriptAlias /User/admin/projects/%1/cgi-bin/ |
| 97 | VirtualDocumentRoot /User/admin/projects/%1/web/ |
| 98 | }}} |
| 99 | |
| 100 | This is the penultimate step to configure Apache. On a mac, you should be able to keep apache running without any issues with performance on your computer. But just to reduce traffic, since this not a live webserver, modify /etc/apache2/extras/httpd-mpm.conf with the following: |
| 101 | {{{ |
| 102 | <IfModule mpm_prefork_module> |
| 103 | StartServers 1 |
| 104 | MinSpareServers 1 |
| 105 | MaxSpareServers 2 |
| 106 | MaxClients 10 |
| 107 | MaxRequestsPerChild 100 |
| 108 | </IfModule> |
| 109 | }}} |
| 110 | |
| 111 | Reload Apache so these changes take effect by going to system preferences > sharing and enabling web sharing. |
| 112 | |
| 113 | NOTE: If you are developing a Drupal project, you will need to edit your .htaccess file. There's a line that says the following: |
| 114 | |
| 115 | {{{ |
| 116 | # If your site is running in a VirtualDocumentRoot at http://example.com/, |
| 117 | # uncomment the following line: |
| 118 | RewriteBase / |
| 119 | }}} |
| 120 | |
| 121 | You will want to uncomment the RewriteBase directive as shown above. |
| 122 | |
| 123 | == Steps to create a new project == |
| 124 | |
| 125 | With your environment setup, you can easily start a new project by following these steps: |
| 126 | |
| 127 | * Choose a name for the project. In this example, we'll choose the name mayfirst (and remember - in our example the computer name is junior). |
| 128 | * Create the mysql database with: |
| 129 | {{{ |
| 130 | mysql -u root -p -e "CREATE DATABASE mayfirst; GRANT ALL ON mayfirst.* to 'mayfirst'@'localhost' identified by 'mayfirst';" |
| 131 | }}} |
| 132 | * Add a new domain to your /etc/hosts file. Add it to the line that starts with 127.0.1.1 (you can safely create the line if it doesn't exist). For example: |
| 133 | {{{ |
| 134 | 127.0.0.1 localhost |
| 135 | 127.0.1.1 mayfirst.local |
| 136 | }}} |
| 137 | * Change into your projects directory: |
| 138 | {{{ |
| 139 | cd ~/projects |
| 140 | }}} |
| 141 | * Clone and checkout your source files via git |
| 142 | {{{ |
| 143 | git clone git@git.mayfirst.org:/mayfirst.git |
| 144 | }}} |
| 145 | * OR create your project folders with: |
| 146 | {{{ |
| 147 | mkdir ~/projects/mayfirst |
| 148 | mkdir ~/projects/mayfirst/web |
| 149 | mkdir ~/projects/mayfirst/cgi-bin |
| 150 | }}} |
| 151 | |
| 152 | Now you are ready to begin. You can start placing files in your project's web directory and access your project at the URL: !http://mayfirst.local/ |
| 153 | |
| 154 | == Keeping files in sync == |
| 155 | |
| 156 | If you are working on a database driven project (like Drupal) it can be difficult to keep your database and (in the case of Drupal) your files directory in sync. |
| 157 | |
| 158 | A bash script like the following one can help - by providing an easy way to synchronize your development platform with the live site: |
| 159 | |
| 160 | {{{ |
| 161 | #!/bin/bash |
| 162 | echo "Synchronizing database." |
| 163 | ssh <user>@<server> "mysqldump -u <remote-db-user> -p<remote-db-pass> <remote-db-name>" | mysql -u <local-db-user> -p<local-db-pass> <local-db-name> |
| 164 | |
| 165 | echo "Synchronizing files." |
| 166 | rsync -av --exclude '*/settings.php' --exclude '.svn*' --exclude '*/.svn*' --exclude 'sites*' <user>@<server>:<remote-path-to-files-directory> <local-path-to-files-director> |
| 167 | }}} |