Now that we know what we need in order to set up a server on a computer it’s time to prep your own one. This article will create a MAMP environment – meaning a server based on MacOS, Apache, MySQL, and PHP.
Understanding Bash, iterm, and vim
First thing to know is that the “Finder” won’t help much for most of the things you’re going to do. The Finder and the rest of the GUI makes it easy to find your way around applications and files, but it also hides a lot of the OS and its functions from you. Not so the “Shell” which will be the “Bash” shell if you’re using a Mac (and a lot of times also when you’re using Linux). Oddly enough, this was once created to make conversation between the OS and the user easier, although nowadays it just looks like a black screen with some cryptographic code.
The nice thing about Bash is that it allows you to access stuff that is usually hidden from the user on the GUI. To do so, you need a terminal window, either the pre-installed one on mac, or you can download iterm. The terminal is used to interact with Bash. Finally, for editing stuff on the terminal, you need vim, which is basically a text editing program without a GUI. It’s usually pre-installed on macs.
Apache and PHP
Apache is usually pre-installed on macs, but if it’s not, you need to download it from the net. To find out if it’s installed use the command “httpd -v” – if it’s installed, it should show you the version of Apache on your computer. But what you need to do is making it usable for PHP files. PHP should be preinstalled on Apache and you just need to comment it in. Other programming languages have to be downloaded I guess. For PHP, you simply have to remove the “#” in front of the PHP module in the Apache file. This will program Apache to process PHP files. Where is the Apache config file located? Most likely either in the var directory or the etc directory of your root. Since it’s a config file you want to edit, etc is more likely to have it. So cd /etc/apache2 should bring you to the right folder. Then, open the config file with vim by typing “sudo vim httpd.conf”. it will ask for your password. After entering the password for your computer and hitting enter the file will be open in vim.
- Finding the file in vim: /php will search for the php module. keep in mind that these programs are case sensitive.
- Editing: hit “i” which will start the insert mode. Delete the “#” in front of “LoadModule php5_module libexec/apache2/libphp5.so”
- Quit editing: hit “escape”
- Save: type “:w” this will “write” (save) the file.
- Quitting vim: “:q” will go out of vim and into the terminal again.
Follow the instructions on coolestguidesontheplanet: http://coolestguidesontheplanet.com/get-apache-mysql-php-phpmyadmin-working-osx-10-10-yosemite/
Creating a new file on localhost
Use iTerm (bash) to edit some files:
in vim edit the config file similar to an html file:
CustomLog “/private/var/log/apache2/dummy-host.example.com-access_log” common
This will create a new “server” on localhost (which is basically just a folder):
DocumentRoot is where all your web files are located (it’s a directory, inside the directory put your web files)
ServerName is the website domain (awesomephototours.com)
ServerAlias is optional if you like to modify your server name
ErrorLog is where stuff gets stored when something goes wrong
CustomLog remembers requests to web pages (like history in web browser)
DirectoryIndex specifies which file to go to when not specified
After that you need to tell your computer that the folder is on your computer by editing the hostfile (“hosts”):
“hosts” is located in the /etc directory.
Use sudo vim hosts to edit the file
Create a new IP and tell it what website it is
Now everytime you type that URL in your browser it will first serve that URL mentioned in the hosts file
A # in front of the file will deactivate that line, to activate it again delete the #.
Important! When you make changes to virtual host file you need to restart apache for it to restart the file again
Before restarting apache you need to run the command “sudo apachectl configtest” to make sure there are no syntax errors in your code. Otherwise the server won’t start. It should spit out the line “Syntax OK” if you read that it means you can restart Apache.
Keep in mind that if the URL in the hosts file is activated (so not “commented out” with a # in front of it) it means that the browser will ALWAYS show the URL specified in your computer. So if you have a WordPress blog it won’t show the version on your www server but the “dev” version on your computer. To show the “real version” again, you need to place the # in front of the URL in the file.
Apache and VirtualHost
In order for Apache to read the virtual host file, you need to allow Apache to process virtual host. Go back to /etc/apache2 and remove the # in front of “Include /private/etc/apache2/extra/httpd-vhosts.conf” (underneath “Virtual hosts”)