Setup a LAMP server of Mac OSX

Tyler Tyler (258)
0

This guide will show you how to set up a LAMP environment in OS X.

To start off, it might be odd to call it a LAMP server on Mac OSX, but that's what it's called. OSX is actually built off of BSD, not Linux. Either way, the "L" refers to the operating system that will be running our web server.

Apache is a very popular web server software, and it's already install on your Mac! It's easy to configure and quick to get started with. On your Mac, you'll need to get familiar with a command to manage your web server: apachectl. This command requires root to run so you'll actually run it like this:

sudo apachectl <cmd>

The basic commands you'll use are:

start

sudo apachectl start

stop

sudo apachectl stop

restart

sudo apachectl restart

MySQL is a very popular open source database software. Unfortunately, MySQL does not come install on the Mac so you'll have to install it. I recommend using homebrew (a great OSX package manager) to install it. First, install homebrew. To install go to http://brew.sh/ and follow the instructions. Next use homebrew to install MySQL. Open the Terminal application - Applications > Utilities > Terminal and type:

brew install mysql

Now get familiar with some basic MySQL commands:

start

mysql.server start

restart

mysql.server restart

stop

mysql.server stop

PHP is already installed on your Mac! This is the programming language used to perform logical operations on user input, interact with the database, and build the response to send back to the user.

In order to get started no configuration changes are necessary, but you will benefit from learning about the apache configuration files. The primary configuration file you should find is httpd.conf. httpd is the name of the apache daemon (don't worry if you don't know what a daemon is) so the configuration file is called httpd.conf and it's usually found in /etc/apache2/httpd.conf. Another file you'll wanted to find is httpd-vhosts.conf. This is the file where you can set up virtual hosts. A virtual host lets you setup multiple domain names for this web server. A basic vhost configuration lets you specify the hostname and the document root. Again, no changes are required to get started.

The document root is where you put your application. On your Mac, the default document root is /Library/WebServer/Documents. If you don't believe me, open up your httpd.conf file and search for DocumentRoot.

Now that you've got the basics, we'll build a basic web page. Use Finder (or Terminal) to navigate to your document root /Library/WebServer/Documents. Create a file called index.php and edit it. Keep in mind that you will have to use sudo to edit this file. One way to do this is to open Terminal and type:

sudo open -a TextEdit /Library/WebServer/Documents/index.php

Add the following to this file:

<!DOCTYPE>
<html>
<head>
    <title>My first web page</title>
</head>
<body>
    <p><?php echo date("F jS, Y"); ?></p>
</body>
</html>

Apache will look in the document root for a file called index.html or index.php. So you may have to delete or rename any other files in this folder that start with index. Now, restart the web server.

sudo apachectl restart

And open your browser and go to:

http://localhost

Our web page is a little boring as it only prints the current date, but this is all we need to get started!