mac上php+nginx配置

追憶丶年華發表於2018-01-16

brew的安裝:
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)”
php安裝和配置
brew search php55
brew info php55—檢視安裝時是否需要帶上引數--with-fpm
brew install php55 —with-fpm
brew install php-mcrypt(加密用的,不一定要安裝的)
安裝之後使用php -v檢視版本,如果不是你安裝的版本需要更改path
執行下列命令更改:
cd
vim .profile
export PATH=/usr/local/bin:$PATH
以下是對php-fpm.conf配置檔案的相關修改
1.使用者名稱修改:
user = zhaoye--使用者
group = staff—使用者組
2.埠
listen = 127.0.0.1:9000=>listen = /opt/run/php.socket(這裡的socket是要給以後nginx配置用的)
3.php.ini配置檔案修改
date.timezone = Asia/Shanghai
4.找到homebrew-php.josegonzalez.php55.plist (通過brew info php55查詢)
拷貝到~/Library/LaunchAgents,因為是埠大於1024的都不是root啟動
mv homebrew-php.josegonzalez.php55.plist php-fpm.plist(修改檔名)
修改php-fpm.plist檔案
如下可以作為參考
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>KeepAlive</key>
<false/>(這裡不能為true,否則一直保持)
<key>Label</key>
<string>php-fpm</string>(啟動服務的命令名)
<key>ProgramArguments</key>
<array>
<string>/usr/local/sbin/php-fpm</string>
<string>--fpm-config</string>
<string>/usr/local/etc/php/5.5/php-fpm.conf</string>(讀取配置檔案的路徑)
</array>
<key>RunAtLoad</key>
<true/>
<key>UserName</key>
<string>mac</string>
<key>WorkingDirectory</key>
<string>/usr/local/var</string>
<key>StandardErrorPath</key>
<string>/var/log/php/php-fpm.log</string>
</dict>
</plist>

最後通過
launchctl load -w ~/Library/LaunchAgents/php-fpm.plist
launchctl start php-fpm
啟動
到這裡php就已經OK了

除錯:
可以安裝brew install php-xdebug+google_xdebug用來除錯
cd /etc/php/5.5/conf.d
給ext-xdebug.ini新增xdebug.remote_enable = On開啟除錯模式


nginx安裝和配置
1.brew install nginx
2.修改nginx的啟動plist檔案
根據brew info nginx可以看到/usr/local/opt/nginx/homebrew.mxcl.nginx.plist
cp /usr/local/opt/nginx/homebrew.mxcl.nginx.plist /Library/LaunchDaemons/nginx.plist
(nginx是80埠是需要root啟動的,所以拷貝到根目錄下的Library/LaunchDaemons)
修改nginx.plist檔案
參考如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>nginx</string>(啟動服務名)
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<false/>(被殺掉之後自己能啟動)
<key>UserName</key>
<string>root</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/opt/nginx/bin/nginx</string>(nginx配置路徑)
<string>-g</string>
<string>daemon off;</string>
</array>
<key>WorkingDirectory</key>
<string>/usr/local</string>
</dict>
</plist>
3.修改nginx配置檔案

cd /usr/local/etc/nginx回到nginx配置檔案目錄
這裡是新建了2個目錄site-enabled,site-available用於配置檔案(為了升級時減少修改量)
site-available裡面存在真正的配置檔案,site-enabled裡面建連結指向site-available裡面的配置檔案
具體操作如下:
mkdir sites-enabled
mkdir sites-available
在site-available裡面新增配置檔案:localhost,test-seekyun.com
修改nginx.conf
參考如下:
user zhaoye admin;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

include /usr/local/etc/nginx/sites-enabled/*;

# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}


# HTTPS server
#
#server {
# listen 443;
# server_name localhost;

# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_timeout 5m;

# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

}


裡面的include /usr/local/etc/nginx/sites-enabled/*;標識了
其實際配置是在site-enabled裡面,site-enabled裡面的
test-seekyun.com -> ../sites-available/test-seekyun.com
是一個軟連結指向../sites-available/test-seekyun.com
test-seekyun.com配置檔案內容如下:
server {
listen 80;
server_name jk.myseekyun.com;(這裡的服務名等下是要在/etc/hosts加上的)

#charset koi8-r;

#access_log logs/host.access.log main;

root /Users/zhaoye/workspace/jkweb;(php程式碼工程所在的目錄)

location / {
index index.html index.htm index.php;
if (!-f $request_filename) {
rewrite ^.*$ /index.php last;
}
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass unix:/opt/run/php.socket;(這個是和上面php-fpm.conf裡面的socket)
include fastcgi_params;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}

location /favicon.ico {
error_log /dev/null crit;
return 404;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

localhost配置檔案如下:
server {
listen 80;

#charset koi8-r;

#access_log logs/host.access.log main;

root /Users/zhaoye/workspace/;(php程式碼所在的目錄)

location / {
index index.html index.htm index.php;
autoindex on;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass unix:/opt/run/php.socket;(這個是和php-fpm.conf名稱配置一致)
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
cd site-enabled
ln -s site-available/localhost
ln -s site-available/test-seekyun.com
到這裡所有的nginx配置都可以了

4.sudo -s(需要root啟動)
launchctl load -w /Library/LaunchDaemons/nginx.plist
launchctl start nginx最後通過launchctl啟動nginx服務
launchctl stop nginx關閉nginx服務

相關文章