1.macOS自带有apache和php,需要彻底删除这两个,通过brew重新安装nginx,php最新版本;安装brew请参考https://www.jianshu.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 创建两个新的配置文件,配置系统打开最多文件限制(如果没有的话)
[geandeiMac:~ gean$ sudo vi /Library/LaunchDaemons/limit.maxfiles.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>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
复制以下内容
<?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 执行launchctl limit是配置生效。需要重启电脑(本人系统 OS X 10.13.6)
重启后终端执行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
修改成功之后也许还会提示另外一种错误,比如日志错误,比如之前会出现找不到php-fpm.conf文件,这可能是误删或者是本身就缺少这个文件导致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保存退出
- 配置网站域名服务
2.1 在nginx/新建servers文件夹,里面新建conf文件
比如域名为www.nihao.com 新建一个nihao.conf,这个文件能生效的前提是nginx.conf文件中的include servers/*.conf呈打开状态(去掉#)
直接复制nginx.conf里面的server{}块会提示以下错误
Warning: realpath(): open_basedir restriction in effect. File(/Volumes/程序开发/www/Html/nihao/app) is not within the allowed path(s): (/Volumes/程序开发/www/Html/nihao/public:/tmp/:/proc/) in /Volumes/程序开发/www/Html/nihao/thinkphp/base.php on line 22
Warning: is_file(): open_basedir restriction in effect. File(/Volumes/程序开发/www/Html/nihao/thinkphp/library/think/Error.php) is not within the allowed path(s): (/Volumes/程序开发/www/Html/nihao/public:/tmp/:/proc/) in /Volumes/程序开发/www/Html/nihao/thinkphp/library/think/Loader.php on line 114
Fatal error: Uncaught Error: Class 'think\Error' not found in /Volumes/程序开发/www/Html/nihao/thinkphp/base.php:62 Stack trace: #0 /Volumes/程序开发/www/Html/nihao/thinkphp/start.php(16): require() #1 /Volumes/程序开发/www/Html/nihao/public/index.php(17): require('/Volumes/\xE7\xA8\x8B\xE5\xBA\x8F...') #2 {main} thrown in /Volumes/程序开发/www/Html/nihao/thinkphp/base.php on line 62
或者会提示No input file specified
解决如下
[geandeiMac:~ gean$sudo vi /usr/local/etc/nginx/servers/nihao.conf
复制下面的代码到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/程序开发/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/程序开发/www/Html/nihao/public/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#下面这句很重要,必须添加,不然打开网页会提示错误或者空白页面
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服务,使用80端口必须加上sudo,不然没有权限
[geandeiMac:~ gean$ sudo nginx -s reload
后续继续更新......