Not keen on the command line? Using something like MAMP may be a better option for you. I’m using vi to edit the files but feel free to use your text editor of choice (TextMate, emacs etc).
On OSX websites are stored in /Users/yourname/Sites so it is a good idea to store your site in there. Let’s create a site in there. Note you’ll need to change yourname in the example to whatever your name is.
cd /Users/yourname/Sites mkdir mysite.com cd mysite.com printf "My awesome site" > index.html
Great - we’ve created our site. Now let’s set up Apache.
First we need to allow virtual hosts by uncommenting a line in the Apache config file
sudo vi +/'# Virtual hosts' /etc/apache2/httpd.conf
Save the file and exit
Now we can add the virtual host
sudo vi /etc/apache2/extra/httpd-vhosts.conf
Add the following to the end of the file. Remember to change ‘yourname’ to whatever your name is and also ‘mysite.com’ to whatever your site folder name is. ServerName is the name you will use to access the site from a browser so if you want something specific change it.
<VirtualHost *:80> <Directory /Users/yourname/Sites/mysite.com> Options +FollowSymlinks +SymLinksIfOwnerMatch AllowOverride All </Directory> DocumentRoot /Users/yourname/Sites/mysite.com ServerName mysite.local </VirtualHost>
As there is no DNS associated with your site we need to set this.
sudo vi /etc/hosts
Add the following. This relates to the ServerName you set in the Step 2
Now we just need to start apache
sudo apachectl start
If you see
org.apache.httpd: Already loaded
Then it is already running so restart it using
sudo apachectl restart
Now open a browser and visit “http://mysite.local/" (or whatever ServerName you set). All being well you should see a page displaying “My awesome site”
If you need PHP then this needs to be enabled. You can do this using
sudo vi +/php5_module /etc/apache2/httpd.conf
#LoadModule php5_module libexec/apache2/libphp5.so
LoadModule php5_module libexec/apache2/libphp5.so
Then save and exit. Finally we need to restart apache:
sudo apachectl restart
Now lets see if PHP is running ok:
cd /Users/yourname/Sites cd mysite.com printf "<?php phpinfo() ?>" > index.php
Now if you open a browser and visit http://mysite.local/index.php you should see the phpinfo() page. Apache is set to serve index.html before index.php so if you delete the index.html you will also be able to access the phpinfo() page at http://mysite.local.
All done! To add further sites just repeat steps 3, 4 and 5.
Have an update or suggestion for this article? You can edit it here and send me a pull request.
Using HashiCorp Vault with LDAP
How to use HashiCorp Vault to setup an LDAP backed secret store with read-only access for users in groups and read-write access for specific users
Linux and Unix xargs command tutorial with examples
Tutorial on using xargs, a UNIX and Linux command for building and executing command lines from standard input. Examples of cutting by character, byte position, cutting based on delimiter and how to modify the output delimiter.
Copy a file in Go
How to copy a file in Go. The ioutil package does not offer a shorthand way of copying a file. Instead the os package should be used.