1.macOS自帶有apache和php巾钉,需要徹底刪除這兩個该编,通過brew重新安裝nginx,php最新版本灭忠;安裝brew請參考http://www.reibang.com/p/e374c2e5b7ce
2.安裝好nginx之后需要配置多站點寂恬;
3.安裝好php之后首先測試能否訪問php文件槽畔,很顯然是不行的骤视,因為會遇到各種問題鞍爱;
①提示
An error occurrentd.
sorry,the page you are looking for is currently unavailable.
Please try again later.
.......
如何解決?
- 開啟php-fpm即可
[geandeiMac:~ gean$ cd /usr/local/etc/php/7.4/sbin/
[geandeiMac:~ gean$ sudo php-fpm 這種方式只能開啟一次专酗,重啟又得重新開啟所以用下面這種方式.
[geandeiMac:~ gean$ nohup ./php-fpm > a.log &
- 開啟過程中會遇到很多錯誤
2.1 如睹逃,提示文件限制,需要修改默認的open files值為655360。
[geandeiMac:~ gean$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) unlimited
max memory size (kbytes, -m) unlimited
open files (-n) 256
pipe size (512 bytes, -p) 1
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 2048
virtual memory (kbytes, -v) unlimited
2.2 創(chuàng)建兩個新的配置文件祷肯,配置系統(tǒng)打開最多文件限制(如果沒有的話)
[geandeiMac:~ gean$ sudo vi /Library/LaunchDaemons/limit.maxfiles.plist
復(fù)制以下內(nèi)容
<?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>limit.maxfiles</string>
<key>ProgramArguments</key>
<array>
<string>launchctl</string>
<string>limit</string>
<string>maxfiles</string>
<string>655360</string>
<string>655360</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>ServiceIPC</key>
<false/>
</dict>
</plist>
:wq保存退出
[geandeiMac:~ gean$ sudo vi /Library/LaunchDaemons/limit.maxproc.plist
復(fù)制以下內(nèi)容
<?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>limit.maxproc</string>
<key>ProgramArguments</key>
<array>
<string>launchctl</string>
<string>limit</string>
<string>maxproc</string>
<string>2048</string>
<string>2048</string>
</array>
<key>RunAtLoad</key>
<true />
<key>ServiceIPC</key>
<false />
</dict>
</plist>
:wq保存退出
2.3 以上兩個文件 需要owned by root:wheel
[geandeiMac:~ gean$ chown root:wheel /Library/LaunchDaemons/limit.maxproc.plist
[geandeiMac:~ gean$ chown root:wheel /Library/LaunchDaemons/limit.maxproc.plist
2.4 執(zhí)行l(wèi)aunchctl limit是配置生效沉填。需要重啟電腦(本人系統(tǒng) OS X 10.13.6)
重啟后終端執(zhí)行ulimit -a查看是否修改成功
[geandeiMac:~ gean$ launchctl limit
[geandeiMac:~ gean$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) unlimited
max memory size (kbytes, -m) unlimited
open files (-n) 655360
pipe size (512 bytes, -p) 1
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 2048
virtual memory (kbytes, -v) unlimited
修改成功之后也許還會提示另外一種錯誤,比如日志錯誤佑笋,比如之前會出現(xiàn)找不到php-fpm.conf文件翼闹,這可能是誤刪或者是本身就缺少這個文件導(dǎo)致php-fpm啟動不了。
②訪問控制器里面的方法蒋纬,提示
404 Not Found
原因橄碾,nginx不支持pathinfo模式,
只能通過http://www.xxx.com/index.php?s=index/index來訪問
如何解決颠锉?需要http://www.xxx.com/index/index以這樣的形式訪問,隱藏index.php
1.在fastcgi.conf文件中添加fastcgi_param PATH_INFO $fastcgi_path_info;
[geandeiMac:~ gean$sudo vi /usr/local/etc/nginx/fastcgi.conf
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REQUEST_SCHEME $scheme;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
:wq保存退出
- 配置網(wǎng)站域名服務(wù)
2.1 在nginx/新建servers文件夾史汗,里面新建conf文件
比如域名為www.nihao.com 新建一個nihao.conf琼掠,這個文件能生效的前提是nginx.conf文件中的include servers/*.conf呈打開狀態(tài)(去掉#)
直接復(fù)制nginx.conf里面的server{}塊會提示以下錯誤
Warning: realpath(): open_basedir restriction in effect. File(/Volumes/程序開發(fā)/www/Html/nihao/app) is not within the allowed path(s): (/Volumes/程序開發(fā)/www/Html/nihao/public:/tmp/:/proc/) in /Volumes/程序開發(fā)/www/Html/nihao/thinkphp/base.php on line 22
Warning: is_file(): open_basedir restriction in effect. File(/Volumes/程序開發(fā)/www/Html/nihao/thinkphp/library/think/Error.php) is not within the allowed path(s): (/Volumes/程序開發(fā)/www/Html/nihao/public:/tmp/:/proc/) in /Volumes/程序開發(fā)/www/Html/nihao/thinkphp/library/think/Loader.php on line 114
Fatal error: Uncaught Error: Class 'think\Error' not found in /Volumes/程序開發(fā)/www/Html/nihao/thinkphp/base.php:62 Stack trace: #0 /Volumes/程序開發(fā)/www/Html/nihao/thinkphp/start.php(16): require() #1 /Volumes/程序開發(fā)/www/Html/nihao/public/index.php(17): require('/Volumes/\xE7\xA8\x8B\xE5\xBA\x8F...') #2 {main} thrown in /Volumes/程序開發(fā)/www/Html/nihao/thinkphp/base.php on line 62
或者會提示No input file specified
解決如下
[geandeiMac:~ gean$sudo vi /usr/local/etc/nginx/servers/nihao.conf
復(fù)制下面的代碼到nihao.conf
server {
listen 80;
server_name www.nihao.com;
#charset koi8-r;
#下面access_log和error_log兩個日志文件如果打開需要在nginx.conf文件中去掉下面三行的#
#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/nihao.access.log main;
error_log logs/nihao.error.log;
location / {
root /Volumes/程序開發(fā)/www/Html/nihao/public/;
index index.html index.htm index.php;
#這里的if判斷主要是為了配置url隱藏index.php和支持pathinfo模式,同時支持index.php/模塊/方法 這種形式的訪問
if (!-e $request_filename) {
rewrite ^/index.php(.*)$ /index.php?s=$1 last;
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
}
#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$ {
root /Volumes/程序開發(fā)/www/Html/nihao/public/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#下面這句很重要停撞,必須添加瓷蛙,不然打開網(wǎng)頁會提示錯誤或者空白頁面
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
#下面的配置主要解決Warning: realpath(): open_basedir restriction in effect.
set $real_script_name $fastcgi_script_name;
if ($real_script_name ~ "^(.+?\.php)(/.+)$") {
set $real_script_name $1;
}
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
#fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PHP_VALUE "open_basedir=$document_root/../:/tmp/:/proc/";
fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/../:/tmp/:/var/tmp/:/proc/";
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
:wq保存退出
2.2 如果在瀏覽器輸入自定義域名訪問的話,需要在hosts文件中添加127.0.0.1 www.nihao.com的域名解析
[geandeiMac:~ gean$ sudo vi /etc/hosts
添加一行
127.0.0.1 www.nihao.com
:wq保存退出
所有配置完成之后重啟nginx服務(wù),使用80端口必須加上sudo戈毒,不然沒有權(quán)限
[geandeiMac:~ gean$ sudo nginx -s reload
后續(xù)繼續(xù)更新......