📜 ⬆️ ⬇️

Installing php5.5 + php-fpm + mysql + nginx on Mac OS X Mavericks

Every web developer who chooses a Mac, after the initial setup of the system, is looking for working tools. And if everything is clear with IDE and editors, then it is difficult to find something similar in convenience to win-win OpenServer or Denwer for free. There is a great solution MAMP PRO , but it costs two thousand wooden. Yes, and work through Apache can confuse some.

Being engaged in the solution of this question, I came across an interesting material that tells how to set up a workspace in just 5-10 minutes using the console package manager Homebrew . I publish his translation, because someone like this instruction on setting up a web environment on a Mac will definitely come in handy.



“I just received a new MacBook Pro and decided to set it up from scratch, because I’ve been using the same Time Machine backup for about four years now. A good chance to get rid of the web server / LAMP stack ( L inux A pache M ySQL P HP) and replace it with Nginx and PHP-FPM as a FastCGI implementation. Below you can read how to configure Nginx, PHP-FPM, MySQL and PhpMyAdmin on OS X 10.9 / Mavericks.
')

Xcode



First of all, install the latest version of Xcode through the Mac App Store:
Download Xcode.app (via Mac App Store)

Once the download is complete, open Xcode in the /Applications folder and agree to the license.

Open a Terminal window and install Xcode using the following command:

 xcode-select --install 


Confirm the installation with the Install button.

Go back to Xcode, press ⌘ + , to access the settings and go to the Locations tab. Install the Command Line Tools to the latest available version, Xcode 5.0.2 (5A3005) in my example:

Xcode.app → Preferences → Location | Command Line Tools

Homebrew



Now you need to install Homebrew , which is a package manager for OS X. You may have already heard about apt-get or aptitude on Linux distributions for installing packages and dependencies for a specific application. brew also works only on computers running Mac OS X. It will also make sure that you get the latest updates for installed applications, so you will not have to worry about expired versions or security gaps, exploits, and so on.

First of all, we need Xquarz:

 curl http://xquartz-dl.macosforge.org/SL/XQuartz-2.7.5.dmg -o /tmp/XQuartz.dmg open /tmp/XQuartz.dmg 


Now we need to download and install Homebrew using the following command:
 ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)" 


We believe in any conflicts and problems:
 brew doctor 


Update the repositories and applications with Homebrew:
 brew update brew upgrade 


PHP-FPM



Because Homebrew does not have a default repository for PHP-FPM, we need to add it:
 brew tap homebrew/dupes brew tap josegonzalez/homebrew-php 


Install PHP-FPM with the following arguments:
 brew install --without-apache --with-fpm --with-mysql php55 


Homebrew will download the PHP-FPM source code and compile it for you. Give him some time, it may take a few minutes.

Setting up command line PHP



If you want to use PHP from the command line, you need to update the $PATH environment variable in the ~/.bash_profile file:
 echo 'export PATH="$(brew --prefix josegonzalez/php/php55)/bin:$PATH"' >> ~/.bash_profile 


Setup autorun



 mkdir -p ~/Library/LaunchAgents cp /usr/local/Cellar/php55/5.5.9/homebrew-php.josegonzalez.php55.plist ~/Library/LaunchAgents/ 


And start PHP-FPM:

 launchctl load -w ~/Library/LaunchAgents/homebrew-php.josegonzalez.php55.plist 


Make sure PHP-FPM is listening on port 9000:

 lsof -Pni4 | grep LISTEN | grep php 


The output should look something like this:

 php-fpm 69659 frdmn 6u IPv4 0x8d8ebe505a1ae01 0t0 TCP 127.0.0.1:9000 (LISTEN) php-fpm 69660 frdmn 0u IPv4 0x8d8ebe505a1ae01 0t0 TCP 127.0.0.1:9000 (LISTEN) php-fpm 69661 frdmn 0u IPv4 0x8d8ebe505a1ae01 0t0 TCP 127.0.0.1:9000 (LISTEN) php-fpm 69662 frdmn 0u IPv4 0x8d8ebe505a1ae01 0t0 TCP 127.0.0.1:9000 (LISTEN) 


Mysql



The next step is to install MySQL:

 brew install mysql 


Setup autorun



 cp /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents 


And start the database server:

 launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist 


Secure the installation



For the security of our MySQL server, we will call the secure_mysql_installation binary in the secure_mysql_installation to change the root password, remove the anonymous user and disable the remote login feature as root:
 mysql_secure_installation 




 > Enter current password for root (enter for none): 


Please enter your current password if it is already set.

 > Change the root password? [Y/n] 


Press enter, specifying the password for the root user. Optionally save it in LastPass or 1Password password managers.

 > Remove anonymous users? [Y/n] 


Yes, they are not necessary.

 > Disallow root login remotely? [Y/n] 


Yes, there is no need for authorization as root from any other IP except 127.0.0.1.

 > Remove test database and access to it? [Y/n] 


Yes. We do not need test tables.

 > Reload privilege tables now? [Y/n] 


Reloading the privilege table will allow us to make sure that the changes take effect.

Connection check



 mysql -uroot -p 


Enter the root password specified earlier and you will see the MySQL console:

 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> 


End the session with the \q command:

 mysql> \q Bye 


phpMyAdmin



Install autoconf which is required for phpMyAdmin:

 brew install autoconf 


Set the $ PHP_AUTOCONF environment variable:

 echo 'PHP_AUTOCONF="'$(which autoconf)'"' >> ~/.bash_profile 


Let's start installing phpMyAdmin:

 brew install phpmyadmin 


Nginx



Install Nginx with the command:

 brew install nginx 


Setup autorun



Since we are using port 80, you need to run Nginx as root:
 sudo cp /usr/local/opt/nginx/*.plist /Library/LaunchDaemons/ sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist 


Test web server



Run Nginx:

 sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist 


The default configuration listens on port 8080 instead of the standard HTTP port 80. For now, ignore this:
 curl -IL http://localhost:8080 


The answer should look like this:

 HTTP/1.1 403 Forbidden Server: nginx/1.4.4 Date: Sun, 08 Dec 2013 03:33:41 GMT Content-Type: text/html Content-Length: 168 Connection: keep-alive 


Stop Nginx again:

 sudo launchctl unload /Library/LaunchDaemons/homebrew.mxcl.nginx.plist 


Further setup



nginx.conf



Create folders which will be necessary for us at the subsequent configuration of Nginx:

 mkdir -p /usr/local/etc/nginx/logs mkdir -p /usr/local/etc/nginx/sites-available mkdir -p /usr/local/etc/nginx/sites-enabled mkdir -p /usr/local/etc/nginx/conf.d mkdir -p /usr/local/etc/nginx/ssl sudo mkdir -p /var/www sudo chown :staff /var/www sudo chmod 775 /var/www 


Delete the current nginx.conf file (which will always be available at /usr/local/etc/nginx/nginx.conf.default if you want to take a look at its code) and load the settings I created using curl from GitHub:
 rm /usr/local/etc/nginx/nginx.conf curl -L https://gist.github.com/frdmn/7853158/raw/nginx.conf -o /usr/local/etc/nginx/nginx.conf 


The config file is as simple and lightweight as possible: worker settings, log paths / formats, and several includes. Nothing superfluous, unlike nginx.conf.default .

Download PHP FPM



Download my PHP-FPM settings from GitHub:
 curl -L https://gist.github.com/frdmn/7853158/raw/php-fpm -o /usr/local/etc/nginx/conf.d/php-fpm 


Creating virtual hosts



 curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_default -o /usr/local/etc/nginx/sites-available/default curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_default-ssl -o /usr/local/etc/nginx/sites-available/default-ssl curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_phpmyadmin -o /usr/local/etc/nginx/sites-available/phpmyadmin 


Clone a test virtual host (including rewrites for 404, 403 and phpinfo() ) using git :
 git clone http://git.frd.mn/frdmn/nginx-virtual-host.git /var/www rm -rf /var/www/.git 


And delete the /var/www/.git folder so that git does not track subsequent changes.

SSL setup



Create a folder for our SSL certificates and private keys:

 mkdir -p /usr/local/etc/nginx/ssl 


Generate 4096bit RSA keys and self-signed certificates with the following command:

 openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=State/L=Town/O=Office/CN=phpmyadmin" -keyout /usr/local/etc/nginx/ssl/localhost.key -out /usr/local/etc/nginx/ssl/localhost.crt openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=State/L=Town/O=Office/CN=localhost" -keyout /usr/local/etc/nginx/ssl/phpmyadmin.key -out /usr/local/etc/nginx/ssl/phpmyadmin.crt 


Enable virtual hosts



Now we need to create symlinks in the sites-enabled folder for virtual hosts in order to enable them:
 ln -sfv /usr/local/etc/nginx/sites-available/default /usr/local/etc/nginx/sites-enabled/default ln -sfv /usr/local/etc/nginx/sites-available/default-ssl /usr/local/etc/nginx/sites-enabled/default-ssl ln -sfv /usr/local/etc/nginx/sites-available/phpmyadmin /usr/local/etc/nginx/sites-enabled/phpmyadmin 


Starting Nginx again:

 sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist 


Latest tests



Here it is, everything should work. Click on the links below to verify this:



Service Management



Due to the fact that sooner or later you need to restart one or another entry, you may need additional aliases:
 curl -L https://gist.github.com/frdmn/7853158/raw/bash_aliases -o /tmp/.bash_aliases cat /tmp/.bash_aliases >> ~/.bash_aliases echo "source ~/.bash_aliases" >> ~/.bash_profile 


You can either open a new window / session of the Terminal or manually restart ~/.bash_profile with the command:
 source ~/.bash_profile 


Now you can use aliases instead of typing long launchctl commands, as above.

Nginx



You can start, stop and restart Nginx using the commands:

 nginx.start nginx.stop nginx.restart 


Quick access to logs:

 nginx.logs.access nginx.logs.default.access nginx.logs.phpmyadmin.access nginx.logs.default-ssl.access nginx.logs.error nginx.logs.phpmyadmin.error 


Config check:

 [sudo] nginx -t 


PHP-FPM



Start, stop and restart PHP-FPM:

 php-fpm.start php-fpm.stop php-fpm.restart 


Config check:

 [sudo] php-fpm -t 


Mysql



Start, stop and restart the MySQL server:

 mysql.start mysql.stop mysql.restart 


Let me know if you are stuck or have any extras! ”




I'd add from myself that also when creating local domains and setting up Nginx to work with them, do not forget to register the “IP domain.name” pair in the hosts using the sudo vi /etc/hosts command.

PS A colleague mrded reports that he wrote a script to automatically install this good:
github.com/mrded/brew-emp

Source: https://habr.com/ru/post/217245/


All Articles