Part 1: macOS 10.15 Catalina Web Development Environment. Developing web applications on macOS is a real joy. There are plenty of options for setting up your development environments, including the ever-popular MAMP Pro that provides a nice UI on top of Apache, PHP and MySQL. MySQL Installation Verification. I hope this article will help you to install Nginx, PHP, MySQL on your Mac OS. Let me know in case you’re stuck at some point or have general feedback.
- Mac Os Php
- Install Apache Mysql Php Mac Os X
- Apache Mysql Php For Mac Os 10.10
- Apache Mysql Php For Mac Os Versions
I recently started a new job and was given the opportunity to choose my work computer. The rest of the development team was using Mac and as of this writing we’ll be using OS X 10.8 Mountain Lion. The problem for me is that I’ve always used Windows. However, I’ve heard great things about Apple, plus I have an iPhone, so I thought… why not? The rest of this post details how I set up my new MacBook Pro for my development stack including Apache, MySQL, PHP, phpMyAdmin, etc.
I’ve learned a little bit about MacPorts and Homebrew and I understand that these can be very helpful package managers. Unfortunately, when I tried using them it was hard for me to understand where everything was going and how things “magically” worked. Therefore, I decided to do all of the installation myself manually. I felt more in control and had better knowledge of how everything was hooked up and where things were.
There are a few articles that I found online that were very helpful and I’ll list them here for reference:
- Rob Allen’s Setting up PHP & MySQL on OS X 10.8 Mountain Lion See Rob’s updated blog post for Mavericks which is Setting up PHP & MySQL on OS X Mavericks.
The rest of this post basically goes through the 4 links above with some modifications.
/usr/local
Ensure that the following directories exist. If not, create them. See man mkdir for the -p flag on the last command in this list for more info.
- sudo mkdir /usr/local/include
- sudo mkdir /usr/local/bin
- sudo mkdir /usr/local/lib
- sudo mkdir -p /usr/local/man/man1
MySQL
- Download the “Mac OS X ver. 10.7 (x86, 64-bit), DMG Archive” for MySQL 5.6.x from mysql.com and install the pkg, the MySQL.StartupItem, and the MySQL.prefPane.
- Add /usr/local/mysql/bin to the path: nano ~/.bash_profile and add:at the top of the file.
- source ~/.bash_profile and/or quit terminal and reopen
- At this point you should be able to reopen terminal and type mysql -v which will list the mysql version and open the mysql prompt. Type q to quit out of mysql and return to your home terminal.
- Set up MySQL root passwordwhere new-password is your password of choice. Use single ‘quotes’ surrounding your new-password. Clear the history by typing history -c so that ‘new-password’ isn’t in plain text on the disk.
- Now ensure that the mysql.sock file can be found by PHP (fixes the 2002 socket error):
- Ensure that MySQL is running: mysql -v then quit q
- sudo mkdir /var/mysql
- sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock
Apache
Mac Os Php
Apple has Apache is installed by default. We just have to use the command line now to start and stop it as well as do a little bit of configuration to fit our needs.
- cd /etc/apache2
- Give write permission the config file to root: sudo chmod u+w httpd.conf
- sudo nano httpd.conf
- Find #LoadModule php5_module libexec/apache2/libphp5.so and remove the leading #
- Find AllowOverride None within the <Directory “/Library/WebServer/Documents”>section and change toAllowOverride All so that .htaccess files will work.
- Change all instances of Document Root from /Library/WebServer/Documents/ to /Users/yourusername/Sites/
- Change permissions back: sudo chmod u-w httpd.conf
- Restart Apache: sudo apachectl restart
- Open Finder and navigate to /Users/yourusername/Sites/ Note: You may have to create this directory if it doesn’t already exist.
- In your Sites directory, create a new file called phpinfo.php with <?php phpinfo(); inside it.
- Use Firefox to navigate to http://localhost/phpinfo.php and check that the PHP version is displayed (5.3.15 at the time of writing).
- Ensure that Apache will start after a reboot:
sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist
php.ini
Install Apache Mysql Php Mac Os X
- cd /etc
- Copy the default php.ini file to one that you continue to modify to suit your own needs now and in the future: sudo cp php.ini.default php.ini
- Change write permissions on this file: sudo chmod ug+w php.ini
- Begin editing your php.ini file and change the following: sudo nano php.ini
- error_reporting = E_ALL | E_STRICT
- display_errors = On
- html_errors = On
- extension_dir = “/usr/lib/php/extensions/no-debug-non-zts-20100525”
- Change all instances of/var/mysql/mysql.sock to /tmp/mysql.sock
- date.timezone = UTC or whatever timezone you prefer
- post_max_size = 64M
- upload_max_filesize = 64M
- Save the php.ini file in nano with “Ctrl + x”, “Y” to save changes, and “Enter” to write to same filename.
- Restart Apache: sudo apachectl restart
Xdebug
The four stages of Xdebug are 1) What is xdebug? 2) How do I install it? 3) Huh, this is neat 4) OMG I CAN’T LIVE WITHOUT THIS! Apple must agree because Mountain Lion ships with it.
- sudo nano /etc/php.ini
- Find the line:
;zend_extension=”/usr/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so”
and uncomment it by removing the semicolon at the start. If that line is not already in your php.ini file then add it to the end of the file. - If you want to configure your Xdebug settings then scroll to the end of the file and look for the [xdebug] section. I like these settings but you can configure it however you want:
xdebug.idekey=”phpstorm-xdebug”
xdebug.remote_enable=1
xdebug.remote_port=”9000″
xdebug.var_display_max_children = 999
xdebug.var_display_max_data = 99999
xdebug.var_display_max_depth = 100 - Restart Apache: sudo apachectl restart and check your phpinfo page that we created when setting up Apache to verify that Xdebug is now loaded
See also my post on using Xdebug with PhpStorm.
PEAR
- cd /usr/lib/php
- sudo php install-pear-nozlib.phar
- pear config-set php_ini /private/etc/php.ini
- pecl config-set php_ini /private/etc/php.ini
- sudo pear channel-update pear.php.net
- sudo pecl channel-update pecl.php.net
- sudo pear upgrade-all
- Edit /etc/php.ini, search for include_path, uncomment it by removing the semicolon, and add /usr/lib/php/pear so that the line looks like include_path = “.:/php/includes:/usr/lib/php/pear”
phpMyAdmin
- Download phpMyAdmin, the all-languages.tar.gz package, uncompress and move the folder with its contents into your document root renaming folder to ‘phpmyadmin’. /Users/yourusername/Sites/phpmyadmin/
- cd ~/Sites/phpmyadmin
- mkdir config
- chmod o+w config
- Run setup in your browser: http://localhost/phpmyadmin/setup/
- You need to create a new localhost MySQL server connection. Click New Server
- Switch to the Authentication tab and set the local MySQL root user and the password. Add in the username “root” and the password that you set up earlier for the MySQL root user set up. Click on save and you are returned to the previous screen. NOTE: This is not the OS X Admin or root password, it is the MySQL root user.
- Make sure you click on “Save.” A config.inc.php is now in the /config directory of your phpmyadmin directory. Move this file to the root level of /phpmyadmin and then remove the now empty /config directory.
- Now going to http://localhost/phpmyadmin/ will now allow you to interact with your MySQL databases.
- To upgrade phpmyadmin just download the latest version and copy the older config.inc.php from the existing directory into the new folder and replace. Be sure to keep a backup the older one just in case.
mcrypt
After installing phpMyAdmin you may get the following warning… “The mcrypt extension is missing.” Complete the following procedures to fix this warning.
- Xcode
- Get the free download version of Xcode from the Apple app store. Install it to your /Applications folder. It will be called ‘Install Xcode’ or just Xcode.app. You need this to use the compilers to compile the source code that you will download.
- Go to Xcode preferences and then look in the ‘Downloads’ button.
- Install the command line tools from the preferences of Xcode.
- Getting mcrypt on OS X Mountain Lion
- Open a Terminal window to work in
- Change to home account directory: cd ~
- Make a directory that you will work in: mkdir mycrypt
- Change to the directory that you just created: cd mcrypt
- Download libmcrypt 2.5.8 from Sourceforge
- Get the PHP code in a tar.gz or .bz2 format for the version of PHP that you are currently running. See your phpinfo.php file to be sure or you can type php -v at the command line of your Terminal.
- Move both of these files that you downloaded into your working directory (mcrypt in this instance) and go back to Terminal
- Expand the first file: tar -zxvf libmcrypt-2.5.8.tar.gz
- Expand the second file: tar -zxvf php-5.4.17.tar.gz
- Remove the compressed archives: rm *.gz
- Any errors on the command line including C++ and g++ mostly are due to Xcode not being installed or the command line tools missing. If you have Xcode but still get the errors, launch Xcode > Preferences > Downloads > Install ‘Command Line Tools’
- Configuring libmcrypt
- Change directory into libmcrypt: cd libmcrypt-2.5.8
- Libmcrypt needs to be configured. Enter: ./configure
- make
- sudo make install
- Autoconf errors
- If the below error occurs after you try the following compile of mcrypt, then autoconf is not installed.Autoconf is not installed with the latest Xcode but may be present on some OS X from an upgraded older Xcode.If you need autoconf – some more Terminal heavy lifting:
- cd ~/mcrypt
- curl -O http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gz
- tar xvfz autoconf-latest.tar.gz
- cd autoconf-2.69/
- ./configure
- make
- sudo make install
- If the below error occurs after you try the following compile of mcrypt, then autoconf is not installed.
- Compile mcrypt PHP extension
- cd ../php-5.4.17/ext/mcrypt/
- /usr/bin/phpize
- Output should be:
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525 - ./configure
- make
- sudo make install
- The result of this should be:
Installing shared extensions: /usr/lib/php/extensions/no-debug-non-zts-20100525/
- Enabling mcrypt.so PHP extension
- Open /etc/php.ini and add the line below at the end: extension=mcrypt.so
- Restart Apache
- sudo apachectl restart
- That’s it! Check your phpMyAdmin login page or browse to your phpinfo.php to see if it loaded correctly. If it didn’t load then you may need to declare the extensions directory in /etc/php.ini
Wrapping Up
Well, that’s about it. We’ve covered a lot and hopefully you have your local web development stack up and running. There’s lots left to do including setting up your IDE, version control, etc. See my other post, recovering from the computer crash, for other items on the list to set up when starting with a new computer or a fresh OS install.
Did I miss anything? What else do you do when configuring your local web server stack?
- GratefulI just want to say thank you so much. This is exactly what I’ve been searching for for two days now :). I still have one problem though. After downloading and installing the crypt files the phpmyadmin is still saying that the extension is missing. I also cannot find the mcrypt.so file or module or whatever it’s supposed to be (sigh i’m not a great programmer yet). Suffice it to say that everything else seems to work properly so far. Is mcrypt necessary? Also, i get another message from phpmyadmin saying that the configuration storage is deactivated. I’ve uncommented the code lines but i get no change.Thanks again anyway. I will figure it all out soon enough. This was fun.
Leave a Reply
Step by step instructions to setup a local development environment running multiple versions of PHP simultaneously.
Sure, you could use MAMP like many other developers out there, and there’s nothing wrong with that, but setting things up this way will give you a better understanding of how all of these things come together and nearly everything you learn here will help you in the future if you need to setup Linux servers; Which in all likelihood you will do as understanding DevOps is a useful skill to have.
I took the steps below using a clean install of macOS Sierra, so hopefully I have covered everything that you need to do. macOS Sierra / Xcode is bundled with Apache and PHP, but we won’t be using these as it is an old version of PHP and messing around trying to update it could potentially break something. A disclaimer is necessary here, if you already have a functional development environment, and you decide to follow these steps, you can’t hold me responsible if it all goes wrong and you break your current setup.
Install Xcode and Xcode Command Line Tools
Open up the App Store and search for Xcode to install it.
Once it has installed, you’ll need to open Xcode at least once as there is a Terms & Conditions prompt to accept before you can use the command line tools. Once you have got past the prompt, if any appears you may have already cleared it previously, open the Terminal and enter the command below to install Xcode Command Line Tools.
The $ is just to indicate this is a command prompt, do not enter it with the command.
Ensure the system installation of Apache is not running.
Apache Mysql Php For Mac Os 10.10
Enter this command to stop Apache if it is running, the second line below (not prefixed with $) is the error I received from the command, this is to be expected as it shouldn’t be running.
This command will stop Apache from starting on boot.
Install Homebrew
To install Homebrew, check for the latest installation instructions at http://brew.sh/, but when I installed it I ran the command below.
Homebrew has a utility that will check if everything is setup correctly, run this after installation.
Add Homebrew taps
taps are extra repositories containing formulae to install software, we will need to add these by entering the commands below.
Enable auto-completion for brew commands (optional)
You can skip this step if you want, but to help you use brew, you can enable auto-completion for commands; So when you type
brew
and mash the tab button you’ll see what commands you can run.After installation you should see a message telling you how to enable auto-completion, mine told me to add this to
~/.bash_profile
Once you have done this, open a new terminal (or tab), and now you should have auto-completion hints when typing
brew
and then hitting tab several times.Install Apache
Now we are ready to install Apache 2.4
The next step involves using
apachectl
again, you may remember we used this right at the start to disable the system version of Apache. But now that we have installed Apache using Homebrew, apachectl
will be controlling our version of Apache, not the system version. So let’s check this has worked by typing:Homebrew installs everything within
/usr/local/
so there is no danger of it conflicting with any system-level software. If you get anything else returned as the file path for apachectl, then something hasn’t been setup properly, likely your PATH
isn’t setup correctly, run brew doctor
again to check.Assuming everything is fine, we are now ready to start Apache. You’ll need to use
sudo
with this command because it opens port 80. Don’t use sudo when you don’t need to, it could mess up file ownership and/or permissions.Now open your web browser and go to http://127.0.0.1, you should see a message saying “It works!”
You should now set Apache to launch on startup.
Install PHP
![Apache mysql php mac os x Apache mysql php mac os x](/uploads/1/1/8/9/118948887/985563327.jpg)
I’ll be installing PHP 5.6 and 7.0, as previous projects I have worked on are deployed to servers running PHP 5.x, so it is sensible to try to match your development environment to your production environment.
Install PHP 5.6
Because we will be installing multiple versions of PHP, we need to change the default PHP-FPM (FastCGI Process Manager) port from
9000
to something else, so I suggest setting it to 9056
.Edit
/usr/local/etc/php/5.6/php-fpm.conf
and replace this line:With this line:
Now we are ready to start PHP-FPM (this will also set it to launch on startup).
Setup Apache to work with PHP-FPM
Edit
/usr/local/etc/apache2/2.4/httpd.conf
We need to enable some Apache modules, they have been commented out, so find these lines and remove the
#
to uncomment them.Apache Mysql Php For Mac Os Versions
So it should now look like…
We need to configure Apache to attempt to load
index.php
by default, so find these lines.And change it to this.
We also need to enable virtual hosts, so find this line and uncomment it.
Now save the file.
We now need to edit the virtual hosts configuration file
/usr/local/etc/apache2/2.4/extra/httpd-vhosts.conf
By default, all of your virtual hosts would go into this file, but it would soon become a mess once you have setup just a few sites. It’s better to use individual files, so either delete or comment out all the lines in this file, and add this to the end.
This will read all the files with the
.conf
extension in that folder, so to add/edit/remove a virtual host, we just add/edit/delete the individual file. Now we are ready to setup our first virtual host. Create a directory for the config files to live in.Now create a file for our virtual host
/usr/local/etc/apache2/2.4/vhosts/_localhost.conf
and enter the configuration below.Now we need to create the folder for the site files.
Now create an
index.php
file with the following code in that folder.Because we have changed Apache’s settings, we need to restart it.
To get our new virtual host to work in the browser, we need to edit our
/etc/hosts
file. You’ll need to use sudo
to edit this file, so assuming you are comfortable using Vim…Add this line to the bottom of the file.
Now open your web browser, and go to http://php56.apache.localhost, you should see the output from
phpinfo()
showing PHP 5.6.29 is installed.You’ll also see an error saying “It is not safe to rely on the system’s timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function.”
To resolve this edit
/usr/local/etc/php/5.6/php.ini
, and find this line.Remove the
;
to uncomment the setting and set the timezone to UTC
.Now we have changed PHP’s settings, we need to restart PHP-FPM.
If you refresh the browser you’ll now see that the error has gone.
Now we have one version of PHP working with Apache, let’s install PHP 7.0.
Install PHP 7.0
First we need to run this, otherwise the installation will fail as there will conflicts.
Now we can install PHP 7.0 with the same extensions as above.
Again, because we are installing multiple versions of PHP we need to change the default FPM port from
9000
to something else, so I suggest setting it to 9070
. The layout of the config files is slightly different in this version of PHP.Edit
/usr/local/etc/php/7.0/php-fpm.d/www.conf
and replace this line:With this line:
To avoid the same error we had before, edit
/usr/local/etc/php/7.0/php.ini
and find:Replace the line with:
Now we are ready to start PHP-FPM (this will also set it to launch on startup).
Edit our existing Apache virtual host config file to test PHP 7.0 is working, edit
/usr/local/etc/apache2/2.4/vhosts/_localhost.conf
and add this block to the end of the file.Because we have changed Apache’s settings, we need to restart it.
Again, we need to create an entry in our
/etc/hosts
file.Add this line to the end of the file.
Now open your web browser, and go to http://php70.apache.localhost, you should see the output from
phpinfo()
showing PHP 7.0.14 is installed.Install MySQL
Now lets start MySQL and get it to run on startup.
I would recommend you setup a password for root, and only allow access from localhost. There is an easy way to do this.
It’s an interactive prompt, here were my responses.
I’ve noticed that since working on older projects, that MySQL is returning errors to PHP when doing
SELECT DISTINCT
or GROUP BY
queries. Turned out it the default sql_mode
in MySQL 5.7 is more restrictive than previous versions. Now, by default it is:Now this is fine to leave as the default, I would recommend you leave the settings alone unless you have problems.
Initially I tried fixing some of the custom queries, but then started noticing problems from queries generated by WordPress’
get_posts()
function, and as I work on so many different WordPress sites I figured that I would end up wasting loads of time trying to fix these on other sites.You can override this setting by creating a config file for mysql, create a file
/usr/local/etc/my.cnf
with these settings:Now if you restart the MySQL server, you shouldn’t be getting these errors.
Summary
Now you should have everything you need to get started, each time you need to work on a new project, reference the previous steps to setup a new virtual host for it and restart Apache. In the future, if you need to install a different version of PHP, you can reference the steps here, and it should work.
If you got stuck at any point, and want to reference my configuration files, you can browse them here:https://github.com/lukearmstrong/localhost