1. 程式人生 > >INSTALL NGINX, PHP AND MYSQL ON OS X

INSTALL NGINX, PHP AND MYSQL ON OS X

Recently I got a new MacBook Pro and decided to set it up from scratch, because I've used the same Time Machine backup to migrate from about four years over and over again.

Perfect time to get rid of the LAMP (Linux Apache MySQL PHP) web server stack and replace it with Nginx and PHP-FPM. Below you can read a detailed guide how to setup Nginx, PHP-FPM, MySQL and phpMyAdmin on OS X 10.9 / Mavericks.

Updated for Yosemite users: Updated the guide for OS X 10.10 since Yosemite is officially released. The steps are basically the same as for the Mavericks installation.

Xcode

First of all, get the latest Xcode version (6.1) via the Mac App Store: Mac App Store link

As soon as the download is finished, open Xcode.app in your/Applications

 folder and agree to the licence.

Open a new Terminal.app window and install the Xcode command line tools:

xcode-select --install

Confirm the installation dialog with Install.

Back in Xcode, hit  + , to access the Preferences and then navigate to the Locations tab to set the Command Line Tools

 to the latest version available — Xcode 6.1 (61A1052c) in my example:

Xcode.app → Preferences → Location | Command Line Tools

Make sure you have at least Xcode 6.1!

Homebrew

Now you need to install Homebrew, a package manager for OS X — kind of like apt is one for Linux. brew works the same, just for Mac operating systems. It will make sure that you receive the latest updates of your installed packages, so you don't need to worry about outdated versions or vulnerable security flaws, etc.

First, you need to download and install Homebrew using the following command:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Check for any conflicts or problems (If you have conflicts, sort them out before you continue with this guide):

brew doctor

Make sure the doctor responds with something along the lines:

Your system is ready to brew.

In case you already had Homebrew installed, update the existingHomebrew installation as well as the installed packages:

brew update && brew upgrade

PHP-FPM

Because Homebrew doesn't come with a formula for PHP-FPM by default, you need to tap (or register) a special PHP repository first:

brew tap homebrew/dupes
brew tap homebrew/php

Now you can install PHP using the following command. The arguments make sure it compiles with MySQL support and doesn't configure the default Apache:

brew install --without-apache --with-fpm --with-mysql php56

Homebrew is now going to download and compile the PHP-FPM source code for you. Give it some time, it could take some minutes. ☕

Setup PHP CLI binary

If you want to use the PHP command line tools, you need to update the$PATH environment variable of your shell profile.

If you use the default Bash shell:

echo 'export PATH="/usr/local/sbin:$PATH"' >> ~/.bash_profile && . ~/.bash_profile

If you use ZSH:

echo 'export PATH="/usr/local/sbin:$PATH"' >> ~/.zshrc && . ~/.zshrc

If you are not sure which one you use, run echo $SHELL in a Terminal.app window. Since I use ZSH it returns this:

/bin/zsh

Setup auto start

Create a folder for the LaunchAgents and symlink the start/stop service:

mkdir -p ~/Library/LaunchAgents
ln -sfv /usr/local/opt/php56/homebrew.mxcl.php56.plist ~/Library/LaunchAgents/

Now try to start PHP-FPM:

launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist

Assure PHP-FPM is running. To do so, check if there is an open listener 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

Next step is to install our MySQL server:

brew install mysql

And set up the start/stop service, so the MySQL server gets automatically started and stopped when the Mac is shutdown/powered on:

ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents

To start if manually for now, run:

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

Secure the installation

To secure our MySQL server, we'll execute the providedsecure_mysql_installation binary to change the root password, remove anonymous users and disable remote root logins:

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

Press ENTER since you don't have one set.

Change the root password? [Y/n]

Confirm using ENTER to accept the suggested default answer (Y), choose a root password, add it to your 1Password keychain or just write it down and finally enter it here in the prompt.

> Remove anonymous users? [Y/n]

Yes - ENTER. They are not necessary.

> Disallow root login remotely? [Y/n]

ENTER — No need to log in as root from any other IP than 127.0.0.1.

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

ENTER — You don't need the testing tables.

> Reload privilege tables now? [Y/n]

ENTER — Reload the privilege tables to ensure all of the changes made so far will take effect immediately.

Test connection

mysql -uroot -p

Enter your root password and you should see the MySQL console:

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

mysql>

Since you now know that this works, you can log out and quit the session using \q:

mysql> \q
Bye

phpMyAdmin

Install autoconf, which is needed for the installation of phpMyAdmin:

brew install autoconf

And set the $PHP_AUTOCONF environment variable:

If you use the default Bash shell:

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

or if you use ZSH:

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

Since now you're all set, you can finish this part with the actual installation of phpMyAdmin:

brew install phpmyadmin

Nginx

Install the default Nginx with:

brew install nginx


Setup auto start

Since you want to use port 80 (default HTTP port), you have to run theNginx process with root privileges:

sudo cp -v /usr/local/opt/nginx/*.plist /Library/LaunchDaemons/
sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

(Only the root user is allowed to open listening ports < 1024)

Test web server

Start Nginx for the first with:

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

The default configuration currently active so it will listen on port 8080 instead of the HTTP default port 80. Ignore that for now:

curl -IL http://127.0.0.1:8080

The output should look like this:

HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Mon, 19 Oct 2014 19:07:47 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 19 Oct 2014 19:01:32 GMT
Connection: keep-alive
ETag: "5444dea7-264"
Accept-Ranges: bytes

Stop Nginx again:

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

More configuration

nginx.conf

Create these bunch of folders which we're going to use for the upcoming configuration:

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

Remove the current nginx.conf (which is also available as/usr/local/etc/nginx/nginx.conf.default in case you want to restore the defaults) and download my custom 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 configuration is simple and as lightweight as possible: worker settings, log format/paths and some includes. None of unnecessary (and probably commented out) stuff like in the nginx.conf.default.

Load PHP FPM

Download my custom PHP-FPM configuration from GitHub:

curl -L https://gist.github.com/frdmn/7853158/raw/php-fpm -o /usr/local/etc/nginx/conf.d/php-fpm

Setup example 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 my example virtual hosts (including 404/403 error pages and aphpinfo() status site) using git:

git clone http://git.frd.mn/frdmn/nginx-virtual-host.git /var/www
rm -rf /var/www/.git

And remove the .git folder so your content won't get tracked by git.

Setup SSL

Create a folder for our SSL certificates and private keys:

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

Generate 4096 bit RSA keys and the self-sign the certificates in one command:

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/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=phpmyadmin" -keyout /usr/local/etc/nginx/ssl/phpmyadmin.key -out /usr/local/etc/nginx/ssl/phpmyadmin.crt

Enable virtual hosts

Now you need to symlink the virtual hosts that you want to enable into the sites-enabled folder:

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

And start Nginx again:

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

Final tests

Congratulations, that's it! Everything should be set up and running. Click on the links below to ensure that your virtual hosts show the correct content:

Control the services

Because your probably need to restart the one or other service sooner or later, you probably want to set up some aliases:

curl -L https://gist.github.com/frdmn/7853158/raw/bash_aliases -o /tmp/.bash_aliases
cat /tmp/.bash_aliases >> ~/.bash_aliases

If you use the default Bash shell:

echo "source ~/.bash_aliases" >> ~/.bash_profile && . ~/.bash_profile

or if you use ZSH:

echo "source ~/.bash_aliases" >> ~/.zshrc &&  ~/.zshrc

Now you can use handy short aliases instead of typing the longlaunchctl commands:

Nginx

You can start, stop and restart Nginx with:

nginx.start
nginx.stop
nginx.restart

To quickly tail the latest error or access logs:

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

Check config:

sudo nginx -t

PHP-FPM

Start, start and restart PHP-FPM:

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

Check config:

php-fpm -t

MySQL

Start, start and restart your MySQL server:

mysql.start
mysql.stop
mysql.restart

FAQ

Here are some of the frequently asked questions from the comments. In case you encounter any issues or problems, check below to find a solution.

Nginx: '[emerg] mkdir() "/usr/local/var/run/nginx/client_body_temp"'

Upgraded to Yosemite and now Nginx doesn't start anymore? Try to reinstall the brew formula:

brew reinstall --force nginx

PHP-FPM: 'lsof -Pni4 | grep LISTEN | grep php' doesn't return anything

Make sure your $PATH variable is properly set:

echo $PATH | grep php56

If that command doesn't return anything at all, you probably forgot to adjust your .zshrc/.bash_profile. Make sure to add this line at the end:

export PATH="$(brew --prefix homebrew/php/php56)/bin:$PATH"

git: 'Could not resolve host: git.frd.mn'

Probably an outage of my private hosted GitLab server. To fix this, simply try to get in touch with me. Either via TwitterE-Mail IRC ([email protected]/espernet) or the comment section below.

curl: 'Failed to connect to localhost port 80: Connection refused'

This is an IPv6 related issue, originating in the /etc/hosts file of your Mac. To fix this, find the line "fe80::1%lo0 localhost" and comment it out. Or just use this one-liner:

sudo sed -i "" 's/^fe80\:\:/\#fe80\:\:/g' /etc/hosts

brew: 'configure: error: Can not find OpenSSL's '

Make sure Xcode as well as Xcode's CLI tools as installed and up to date!

相關推薦

INSTALL NGINX, PHP AND MYSQL ON OS X

Recently I got a new MacBook Pro and decided to set it up from scratch, because I've used the same Time Machine backup to migrate from about four years o

實踐補充 Installing Tomcat 7.0.x on OS X

ocp npr epc cgi security hssf xca 之前 epub 我的 Mac 下是1.6的 SDK,下載 Tomcat 8.0 執行後,訪問 http://127.0.0.1:8080 並無反應,並且關閉腳本會報錯 :Unsupported major

memcache緩存服務器(nginx php memcache mysql)

memcachememcache緩存服務器(nginx php memcache mysql)環境:192.168.1.23 nginx+php192.168.1.28 memcache192.168.1.27 mysql一、安裝 nginx (192.168.1.23)1、解壓 zlib 和pcre

how to install node.js and npm on Ubuntu

To install Node.js, type the following command in your terminal: sudo apt-get install nodejs Then install the Node package manager, npm: sud

配置nginxphp-fpm踩過的坑,(附裝nginx,php-fpm,mysql,redis教程)

近來在centos7上搭建了lnmp的環境,遇到了個坑。如果沒有裝nmp的話可以看這個文章 https://blog.csdn.net/qq_39677681/article/details/82025445 我所遇到的坑: 要讓php-fpm能正常在伺服器上正常解析php,要配置這兩個

斐訊k3搭建nginx+php+MariaDB(mysql )的教程

安裝nginx+php+MariaDB 以前用k3安裝了onmp,中間踩了不少坑。以前發到貼吧的,現在發過來記錄一下,順便給大家參考一下。 斐訊k3效能比較強,拿來建小型網站還是可以的。但是內建儲存太小,需要額外的u盤或是行動硬碟來掛載。我是官改1.6,但是理論

Marathon on OS X初體驗

本文描述了在OS X上安裝使用Marathon的過程。 Marathon簡介 Marathon是Mesosphere公司為Mesos生態圈打造的一個輕量級、擴充套件性很強的排程long-running service的排程框架。支援RESTful api

運維筆記43 使用saltstack配置完整線上服務(haproxy+keepalived,nginx+php+memcache,mysql主從)

概述: 之前我們所介紹過了很多實用的服務,有負載均衡類的,web服務類的,資料庫類的等等,這些服務有的配置容易,有的配置困難,那我們現在設想一下在生成環境中,有上百臺,甚至上千臺伺服器的情況下,難道要我們去挨個去配置每一臺伺服器嗎,這是無法想象的,所以有了sa

Nginx+PHP+memcache+MySQL實現快取伺服器

memcached快取應用 系統環境: 主機名 作業系統 ip地址 服務軟體 memcached centos7.5 192.168.80.102 memcached-1.5.9.tar.gz libevent-2.1.8-stable.tar.gz web1

HTK on OS X

On OS X, HTK has its own dependencies which you'll need to install. The process, in four parts are: Command line Tools It is necessar

How can I install the `ll` command on Mac OS X?

In OS X 10.9.5 since Mavericks (and at least up to El Capitan) you have to add an alias command to your .bash_profile file in your home folder: ~/.bash_pr

CentOS7.X安裝LMMP環境Nginx+PHP+Mysql詳解

前言: 作為PHP開發者,我們常用的線上環境就是LNMP,合理的搭建也是必須掌握的技能,下面就利用原始碼的方式詳細介紹下LNMP環境Nginx+PHP+Mysql的詳細搭建步驟: 版本說明: Nginx:nginx-1.14.1(最新版本) PHP:php-7.2.12(最新版本) Mysql:my

Mac OS X 配置 Apache+Mysql+PHP 詳細教程

檔案儲存之後,給它賦予相應的許可權: sudo chmod 755 /etc/apache2/users/haibor.conf 接下來重啟 Apache,以使該配置檔案生效: sudo apachectl restart 之後你就可以通過瀏覽器訪問你的使用者級目錄網頁了,你可以隨便防個網頁進去測試一下。根

XAMPP: 在 MAC OS X 下安裝 Apache + PHP + MySQL 環境

XAMPP for Mac OS X 類似在 Windows 環境下的 AppServ。XAMPP 是由 Apache Friends 所主導的非營利的軟體開發專案,其目的是為了推廣 Apache Web Server,因此開發了這個跨平臺的快速架站包,XAMPP

Install NumPy, SciPy, scikit-learn on Mac OS X

Outline 1. install Xcode –> 2. install pip –> 3. install brew –> 4. install NumPy –> 5. install gfortran (import

Mac os X下使用NginxPHP的連線問題

安裝Nginx 使用brew包管理工具來安裝Nginx http://brew.sh/ 官網上一目瞭然,使用非常簡單。 安裝成功後,直接在terminal裡執行brew install nginx即可 安裝完,在/usr/local/Cellar/下可以看到安裝的nginx

Linux(CentOS6.x)下使用yum軟體管理工具安裝LNMP(Nginx+PHP+Mysql)環境並配置虛擬主機vhost

 前言:之前本小生也嘗試過原始碼安裝,即在官網下載nginx,php,mysql的tar包然後手工一步一步的搭建,但是期間發現,每個軟體在linux環境執行時都需要大量的依賴包,比如nginx需要gcc等,php需要pcre等,都需要逐個的去官網找安裝包安裝,比較麻煩和繁瑣,

記一次診斷Centos 7.X伺服器Nginx PHP Mysql環境異常處理的方法和

一、問題現象 1.公司伺服器資訊 IP地址:192.168.17.254 作業系統:Cent OS 7.X  2.表現現象:訪問過多伺服器WEB頁面訪問異常,打不開,PHPMYADMIN訪問速度慢。 各種電話QQ找我處理,快爆炸了,下定決心走入排查Linux環境之路。 本人

ANPM-Apache_httpd-Nginx-PHP-MySQL 官方預編譯包源(Pre-Built Packages Repository)收集

apache httpd nginx php mysql ANPM-Apache_httpd-Nginx-PHP-MySQL 官方預編譯包源(Pre-Built Packages Repository)收集Apache_httpdNginxhttp://nginx.org/en/linux

CentOS6.3上安裝與配置nginx+php+mysql環境

需要 目前 htm evel mem dev gin ins comm 1. 目前nginx采用是源碼包安裝的方式(yum安裝失敗),下載地址:http://nginx.org/en/download.html 我這裏的安裝包是:nginx-1.12.0.tar.gz