Setup a LAMP server
This guide will show you how to set up a LAMP environment in OS X.
1. L is for Linux
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.
2. A is for Apache
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:
sudo apachectl start
sudo apachectl stop
sudo apachectl restart
3. M is for MySQL
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:
4. P is for PHP
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.
Get familiar with the apache configuration
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.
5. The document root
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.
6. Your first web page
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:
<title>My first web page</title>
<p><?php echo date("F jS, Y"); ?></p>
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:
Our web page is a little boring as it only prints the current date, but this is all we need to get started!
7. Postfix in Mac OSX
No need it because Postfix already exist as pre-installed on Mac OSX. Open your terminal, make sure you can see response if you type below:
postfix: error: to submit mail, use the Postfix sendmail command
postfix: fatal: the postfix command is reserved for the superuser
Great, then we are going to configure it to be able for sending email.
We are going to change postfix configuration file. Run this command below:
$ sudo vi /etc/postfix/main.cf
Make sure if these lines below are exist
mail_owner = _postfix
setgid_group = _postdrop
Then, add these lines at the end of file
# Use local mail delivery in MacOS
Follow the guide here: https://gist.github.com/tamlm/e719cdb79c6b0078d9aa32dd31a329a6
# Use Gmail SMTP in MacOS
relayhost = smtp.gmail.com:587
smtp_sasl_mechanism_filter = plain
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_use_tls = yes
smtp_tls_security_level = encrypt
tls_random_source = dev:/dev/urandom
From codes above, you can see that we use Gmail SMTP for our postfix. Next step is add our gmail’s username and password.
$ sudo vim /etc/postfix/sasl_passwd
and add this line
Then run command below to create the hash db file for Postfix.
$ sudo postmap /etc/postfix/sasl_passwd
Configure the postfix daemon
$ sudo vi /System/Library/LaunchDaemons/org.postfix.master.plist
Add these lines before </dict>
Start the postfix service
$ sudo postfix start
* Note that if you make changes in postfix configuration file (/etc/postfix/main.cf), you have to run command
$ sudo postfix reload
Here is the command you need to execute to test postfix.
$ echo "Test sending email from Postfix" | mail -s "Test Postfix" email@example.com
If success, you will receive email in your inbox.
If you want to see mail queues, execute this command
If you want to see mail log, execute this command
$ tail -f /var/log/mail.log
Setting up mail server on Mac OSX can be done using pre-installed Postfix. Having mail server in local environment will give you advantage for your web application that have sending email feature.