mac搭建nginx和wordpress開發環境

對於不懂後端的我,作這件事真是受盡折磨。 在不懈努力下,終於成功。 下面寫下筆記,與你們分享。php

第一步:關閉Apache及開機啓動

要使用nginx,最好停用mac中自帶的Apache。停用很簡單:html

sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plistmysql

第二步:安裝homebrew

homebrew是mac下的包管理器,相似於linux下的yumapt。使用homebrew安裝nginxphpmysql要比手動安裝方便不少。官網地址:http://brew.sh/index_zh-cn.htmllinux

安裝:nginx

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

第三步:安裝nginx

安裝:brew install nginxgithub

啓動:sudo nginxsql

中止:sudo nginx -s quitshell

配置nginx:數據庫

/usr/local/var/log/nginx/下,新建文件:access.logerror.log

配置/usr/local/etc/nginx/nginx.conf

#user  nobody;
worker_processes  1;

error_log  /usr/local/var/log/nginx/error.log;

pid        /usr/local/var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    access_log  /usr/local/var/log/nginx/access.log;
    sendfile        on;
    keepalive_timeout  65;

    #
    include conf.d/*.conf;
}

/usr/local/etc/nginx/下,新建文件夾conf.d。在conf.d/下,新建文件default.conf,配置default.conf

server {
        listen       80  default_server;
        server_name  localhost;    #域名,自定義
        root   網站根目錄;    #自定義,如/var/www
        index  index.html index.htm
    }

測試:在你設定的根目錄下(例如/var/www/),新建一個靜態頁index.html,啓動nginx,在瀏覽器中輸入localhost,成功看到靜態頁內容。

第四步:安裝php

首先,在brew中添加php的源:

brew tap josegonzalez/php

brew tap homebrew/dupes

查看已添加的源:brew tap

搜索可安裝的php:brew search php
一看結果,我靠,怎麼這麼多!不懂php的我,徹底不懂這些是什麼東西。 不過不用管他們。只要知道該安裝哪些就好。

看到別人安裝最多的是php55,安裝前首先查看一下安裝相關參數的說明:

brew search php55

結果:

--disable-opcache
    Build without Opcache extension
--homebrew-apxs
    Build against apxs in Homebrew prefix
--with-apache
    Enable building of shared Apache 2.0 Handler module, overriding any options which disable apache
--with-cgi
    Enable building of the CGI executable (implies --without-apache)
--with-debug
    Compile with debugging symbols
--with-enchant
    Build with enchant support
--with-fpm
    Enable building of the fpm SAPI executable (implies --without-apache)
--with-gmp
    Build with gmp support
--with-homebrew-curl
    Include Curl support via Homebrew
--with-homebrew-libxslt
    Include LibXSLT support via Homebrew
--with-homebrew-openssl
    Include OpenSSL support via Homebrew
--with-imap
    Include IMAP extension
--with-libmysql
    Include (old-style) libmysql support instead of mysqlnd
--with-mssql
    Include MSSQL-DB support
--with-pdo-oci
    Include Oracle databases (requries ORACLE_HOME be set)
--with-phpdbg
    Enable building of the phpdbg SAPI executable (PHP 5.4 and above)
--with-postgresql
    Build with postgresql support
--with-thread-safety
    Build with thread safety
--with-tidy
    Include Tidy support
--without-bz2
    Build without bz2 support
--without-mysql
    Remove MySQL/MariaDB support
--without-pcntl
    Build without Process Control support
--without-pear
    Build without PEAR
--without-snmp
    Build without SNMP support
--HEAD
    Install HEAD version

沒搞明白這些是什麼意思,先這麼裝吧:

brew install php55 --with-fpm, --with-enchant, --with-debug

成功安裝後,啓動php-fpm:(php-fpm至關於一個接口,nginx和php之間通訊經過php-fpm這個東西)

launchctl load -w /usr/local/opt/php55/homebrew.mxcl.php55.plist

中止php-fpm:

launchctl unload -w /usr/local/opt/php55/homebrew.mxcl.php55.plist

設置快捷指令:打開~/.bash_profile,添加

alias php55.start=launchctl\ load\ -w\ /usr/local/opt/php55/homebrew.mxcl.php55.plist
alias php55.stop=launchctl\ unload\ -w\ /usr/local/opt/php55/homebrew.mxcl.php55.plist

快捷指令設置以後,重啓shell,就能夠用php55.startphp55.stop來啓動和中止php-fpm了。

從新配置nginx:配置文件/usr/local/etc/nginx/conf.d/default.conf

server {
        listen       80  default_server;
        server_name  localhost;    #域名,自定義
        root   網站根目錄;    #自定義,如/var/www
        index  index.html index.htm

        # pass the PHP scripts to FastCGI slinerver listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME   網站根目錄$fastcgi_script_name;   #如/var/www$fastcgi_script_name
            include        fastcgi_params;
            fastcgi_intercept_errors on;
        }
    }

測試:重啓nginx,啓動php-fpm後,在網站根目錄下新建文件index.php,設置index.php的內容:<?php phpinfo(); ?>。而後瀏覽器中輸入:localhost/index.php,看到php信息,成功。

第五步:安裝mysql

安裝:brew install mysql

啓動:launchctl load -w /usr/local/opt/mysql/homebrew.mxcl.mysql.plist

中止:launchctl unload -w /usr/local/opt/php55/homebrew.mxcl.php55.plist

設置快捷指令:同php-fpm快捷指令同樣,打開~/.bash_profile,添加

alias mysql.start=launchctl\ load\ -w\ /usr/local/opt/mysql/homebrew.mxcl.mysql.plist
alias mysql.stop=launchctl\ unload\ -w\ /usr/local/opt/mysql/homebrew.mxcl.mysql.plist

初始化mysql:運行/usr/local/opt/mysql/bin/mysql_secure_installation 將有一個嚮導知道你一步一步設定安全和配置信息。

安裝phpmyadmin: 在http://www.phpmyadmin.net/home_page/downloads.php下載最新版的phpmyadmin,解壓後將目錄下的全部文件放到網站根目錄/phpmyadmin下(如/var/www/phpmyadmin),而後瀏覽器中輸入localhost/phpmyadmin出現首頁,輸入數據庫帳號(root)和密碼,登錄成功。

第六步:安裝wordpress

下載:從https://wordpress.org/download/上下載最新版的wordpress。
解壓後將目錄下的全部文件放到網站根目錄/wordpress下(如/var/www/wordpress)。

設置本地域名:打開文件/etc/hosts,另起一行輸入127.0.0.1 mywordpress,保存文件。

我遇到一件很頭疼的事:明明nginx,php,mysql都沒有問題,但是每當初始化wordpress總會遇到nginx報錯:

google一查,原來wordpress官方專門有個配置nginx的教程:http://codex.wordpress.org/Nginx

從新配置nginx

配置/usr/local/etc/nginx.conf

#user  nobody;
worker_processes  1;

error_log  /usr/local/var/log/nginx/error.log;

pid        /usr/local/var/run/nginx.pid;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    access_log  /usr/local/var/log/nginx/access.log;
    sendfile        on;
    keepalive_timeout  65;

    #php max upload limit cannot be larger than this
    client_max_body_size 13m;
    index              index.php index.html index.htm;

    # Upstream to abstract backend connection(s) for PHP.
    upstream php {
      #this should match value of "listen" directive in php-fpm pool
      #server unix:/tmp/php-fpm.sock;
        server 127.0.0.1:9000;
    }

    #
    #
    include conf.d/*.conf;
}

/usr/local/etc/nginx/下新建目錄global,在/usr/local/etc/nginx/global/下新建文件添加文件restrictions.confwordpress.conf,分別添加以下內容:

restrictions.conf:

# Global restrictions configuration file.
# Designed to be included in any server {} block.</p>
location = /favicon.ico {
  log_not_found off;
  access_log off;
}

location = /robots.txt {
  allow all;
  log_not_found off;
  access_log off;
}

# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~ /\. {
  deny all;
}

# Deny access to any files with a .php extension in the uploads directory
# Works in sub-directory installs and also in multisite network
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~* /(?:uploads|files)/.*\.php$ {
  deny all;
}

wordpress.conf:

# WordPress single blog rules.
# Designed to be included in any server {} block.

# This order might seem weird - this is attempted to match last if rules below fail.
# http://wiki.nginx.org/HttpCoreModule
location / {
  try_files $uri $uri/ /index.php?$args;
}

# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

# Directives to send expires headers and turn off 404 error logging.
location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
       access_log off; log_not_found off; expires max;
}

# Uncomment one of the lines below for the appropriate caching plugin (if used).
#include global/wordpress-wp-super-cache.conf;
#include global/wordpress-w3-total-cache.conf;

# Pass all .php files onto a php-fpm/php-fcgi server.
location ~ [^/]\.php(/|$) {
  fastcgi_split_path_info ^(.+?\.php)(/.*)$;
  if (!-f $document_root$fastcgi_script_name) {
    return 404;
  }
  # This is a robust solution for path info security issue and works with "cgi.fix_pathinfo = 1" in /etc/php.ini (default)

  include fastcgi.conf;
  fastcgi_index index.php;
#   fastcgi_intercept_errors on;
  fastcgi_pass php;
}

/usr/local/etc/nginx/conf.d/下新建文件 mywordpress.conf,配置文件內容

server {
  server_name mywordpress;
  root 網站根目錄/mywordpress;  #自定義,如/var/www/mywordpress

  index index.php;

  include global/restrictions.conf;
  include global/wordpress.conf;
}

最後一步:重啓nginx,啓動php-fpm和mysql,在瀏覽其中輸入mywordpress,出現初始化嚮導,按照嚮導設置數據庫信息,而後成功。


若有問題歡迎留言與我聯繫! 或者郵箱:linchen.1987@foxmail.com

相關文章
相關標籤/搜索