Restart Mysql Mac Brew

$ brew services restart mysql 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. Jan 14, 2021 The bitnami installer worked well enough. I did have to restart it a couple of times before figuring out that I also needed to go back to the “Application Manager” and turn on MySQL. When it finished its thing, I had a new WordPress install at 127.0.0.1 which isn’t exactly what I wanted.

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.

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

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:

Restart Mysql Mac Brew 2019

Now we are ready to start PHP-FPM (this will also set it to launch on startup).

Setup Apache to work with PHP-FPM

Restart Mysql Mac Brew

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.

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).

Instructions

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.

Download

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

Further Reading

Question or issue on macOS:

I’m trying to install mysql on MAC OS version 10.11.2 by following the methods in this link,

The following command has been successfully executed,

but when I type the following command,

I get the following error:

Please help. Thanks in advance.

How to solve this problem?

Solution no. 1:

Restart Mysql Mac Brew 2019

All credit goes to coder wall

Remove MySQL completely

ps -ax grep mysql
stop and kill any MySQL processes
brew remove mysql
brew cleanup
sudo rm /usr/local/mysql
sudo rm -rf /usr/local/var/mysql
sudo rm -rf /usr/local/mysql*
sudo rm ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/My*
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
edit /etc/hostconfig and remove the line MYSQLCOM=-YES-
rm -rf ~/Library/PreferencePanes/My*
sudo rm -rf /Library/Receipts/mysql*
sudo rm -rf /Library/Receipts/MySQL*
sudo rm -rf /private/var/db/receipts/*mysql*
edit ~/.bash_profile and remove any aliases for mysql or mysqlAdmin
restart your computer just to ensure any MySQL processes are killed
try to run mysql, it shouldn’t work

Reinstall MySQL with Homebrew

brew doctor
brew update
brew install mysql
unset TMPDIR
mysqld -initialize –log-error-verbosity –user=whoami –basedir=”$(brew –prefix mysql)” –datadir=/usr/local/var/mysql –tmpdir=/tmp
mysql.server start
brew services start mysql

Solution no. 2:

Try running this, it worked for me. (Got it from the comments found here)

Mysql

Restart Mysql Mac Brew Free

Posting here just in case anyone faced the same problem as me.

Solution no. 3:

Mysql Mac Download

Based on this link: https://bugs.launchpad.net/mysql-sandbox/+bug/1396092 and this https://blog.omgmog.net/post/installing-mysql-on-os-x-mountain-lion/ It looks like you may simply need to unset TMPDIR prior to running the install_db command.

Restart Mysql Mac Brew Free

Hope this helps!