macOS monterey 12.6.1安装homebrew + nginx + php + mysql
Posted Rudon滨海渔村
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了macOS monterey 12.6.1安装homebrew + nginx + php + mysql相关的知识,希望对你有一定的参考价值。
效果图
主要步骤
详细步骤
- 参考“Homebrew国内如何自动安装(国内地址)(Mac & Linux)”安装brew,
(因为有些国内IP无法访问安装homebrew,所以以下是国内的资源,贼快)
命令:/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
- 先使用brew安装mysql
主要命令:brew install mysql
注意,这时安装好了mysql,但是缺少权限,mysql无法进行“mysql_secure_installation”操作,错误提示:
Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
错误解释:/tmp/mysql.sock 导致mysql无法连接
最大的可能性:'/tmp/mysql.sock' 创建失败、或者目录无权限读写
参考解决办法: https://www.jianshu.com/p/f9c044747133
我个人习惯把socket文件都塞到/var/systemKits/socket/,在mysql配置文件中指向这里,
具体操作:sudo mkdir -p /var/systemKits/socket/ sudo chmod -R 0777 /var/systemKits/socket/
再找出mysql配置文件并修改:
sudo vi /usr/local/etc/my.cnf
并添加以下代码,保存:
[mysqld] socket=/var/systemKits/socket/mysql.sock [client] socket=/var/systemKits/socket/mysql.sock [mysql] socket=/var/systemKits/socket/mysql.sock
my.cnf最终效果:
# Default Homebrew MySQL server config [mysqld] # Only allow connections from localhost bind-address = 127.0.0.1 mysqlx-bind-address = 127.0.0.1 socket=/var/systemKits/socket/mysql.sock [client] socket=/var/systemKits/socket/mysql.sock [mysql] socket=/var/systemKits/socket/mysql.sock
再重启mysql服务:brew services restart mysql
如图: - 开始设置Mysql的root密码,命令行输入
回车,开始设置root密码。mysql_secure_installation
会先问你要不要什么额外的密码组件,我这里直接no回车,再设置密码
- 安装php、nginx
brew install php brew install nginx
- 服务开启start、关闭stop、重启restart:
brew services restart nginx brew services restart php brew services restart mysql
- 查看php、nginx版本:
nginx -v
php -v
- 先备份好nginx和php的配置文件,“后悔药”准备好。
1)备份php配置文件夹 /usr/local/etc/php/
2)备份nginx配置文件夹 /usr/local/etc/nginx/
- 快速了解PHP-FPM
PHP-FPM详解 - walkingSun - 博客园[TOC] # 作用PHP-FPM(PHP FastCGI Process Manager)意:PHP FastCGI 进程管理器,用于管理PHP 进程池的软件,用于接受web服务器的请求。PHPhttps://www.cnblogs.com/followyou/p/9460058.html
(超级好文!内容已经被我无耻的复制到本文底部,以防丢失,若侵权请告知,谢大神!)
- 为php设置效率最高的非分布式的PHP-FPM进程池监听方式,
先根据自己的爱好方式,提前为php-fpm要用的socket文件准备好可读写的目录,
例如:
编辑 /usr/local/etc/php/8.1/php-fpm.d/www.conf ,修改listen监听方式,sudo mkdir -p /var/systemKits/socket/ sudo chmod -R 0777 /var/systemKits/socket/
从
修改为listen = 127.0.0.1:9000
路径可以随意...listen = /var/systemKits/socket/php-fpm.socket
完整代码:; Start a new pool named 'www'. ; the variable $pool can be used in any directive and will be replaced by the ; pool name ('www' here) [www] ; Per pool prefix ; It only applies on the following directives: ; - 'access.log' ; - 'slowlog' ; - 'listen' (unixsocket) ; - 'chroot' ; - 'chdir' ; - 'php_values' ; - 'php_admin_values' ; When not set, the global prefix (or /usr/local/Cellar/php/8.1.12) applies instead. ; Note: This directive can also be relative to the global prefix. ; Default Value: none ;prefix = /path/to/pools/$pool ; Unix user/group of processes ; Note: The user is mandatory. If the group is not set, the default user's group ; will be used. user = _www group = _www ; The address on which to accept FastCGI requests. ; Valid syntaxes are: ; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific IPv4 address on ; a specific port; ; '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on ; a specific port; ; 'port' - to listen on a TCP socket to all addresses ; (IPv6 and IPv4-mapped) on a specific port; ; '/path/to/unix/socket' - to listen on a unix socket. ; Note: This value is mandatory. ;listen = 127.0.0.1:9000 listen = /var/systemKits/socket/php-fpm.socket ; Set listen(2) backlog. ; Default Value: 511 (-1 on FreeBSD and OpenBSD) ;listen.backlog = 511 ; Set permissions for unix socket, if one is used. In Linux, read/write ; permissions must be set in order to allow connections from a web server. Many ; BSD-derived systems allow connections regardless of permissions. The owner ; and group can be specified either by name or by their numeric IDs. ; Default Values: user and group are set as the running user ; mode is set to 0660 ;listen.owner = _www ;listen.group = _www ;listen.mode = 0660 ; When POSIX Access Control Lists are supported you can set them using ; these options, value is a comma separated list of user/group names. ; When set, listen.owner and listen.group are ignored ;listen.acl_users = ;listen.acl_groups = ; List of addresses (IPv4/IPv6) of FastCGI clients which are allowed to connect. ; Equivalent to the FCGI_WEB_SERVER_ADDRS environment variable in the original ; PHP FCGI (5.2.2+). Makes sense only with a tcp listening socket. Each address ; must be separated by a comma. If this value is left blank, connections will be ; accepted from any ip address. ; Default Value: any ;listen.allowed_clients = 127.0.0.1 ; Specify the nice(2) priority to apply to the pool processes (only if set) ; The value can vary from -19 (highest priority) to 20 (lower priority) ; Note: - It will only work if the FPM master process is launched as root ; - The pool processes will inherit the master process priority ; unless it specified otherwise ; Default Value: no set ; process.priority = -19 ; Set the process dumpable flag (PR_SET_DUMPABLE prctl) even if the process user ; or group is different than the master process user. It allows to create process ; core dump and ptrace the process for the pool user. ; Default Value: no ; process.dumpable = yes ; Choose how the process manager will control the number of child processes. ; Possible Values: ; static - a fixed number (pm.max_children) of child processes; ; dynamic - the number of child processes are set dynamically based on the ; following directives. With this process management, there will be ; always at least 1 children. ; pm.max_children - the maximum number of children that can ; be alive at the same time. ; pm.start_servers - the number of children created on startup. ; pm.min_spare_servers - the minimum number of children in 'idle' ; state (waiting to process). If the number ; of 'idle' processes is less than this ; number then some children will be created. ; pm.max_spare_servers - the maximum number of children in 'idle' ; state (waiting to process). If the number ; of 'idle' processes is greater than this ; number then some children will be killed. ; pm.max_spawn_rate - the maximum number of rate to spawn child ; processes at once. ; ondemand - no children are created at startup. Children will be forked when ; new requests will connect. The following parameter are used: ; pm.max_children - the maximum number of children that ; can be alive at the same time. ; pm.process_idle_timeout - The number of seconds after which ; an idle process will be killed. ; Note: This value is mandatory. pm = dynamic ; The number of child processes to be created when pm is set to 'static' and the ; maximum number of child processes when pm is set to 'dynamic' or 'ondemand'. ; This value sets the limit on the number of simultaneous requests that will be ; served. Equivalent to the ApacheMaxClients directive with mpm_prefork. ; Equivalent to the PHP_FCGI_CHILDREN environment variable in the original PHP ; CGI. The below defaults are based on a server without much resources. Don't ; forget to tweak pm.* to fit your needs. ; Note: Used when pm is set to 'static', 'dynamic' or 'ondemand' ; Note: This value is mandatory. pm.max_children = 5 ; The number of child processes created on startup. ; Note: Used only when pm is set to 'dynamic' ; Default Value: (min_spare_servers + max_spare_servers) / 2 pm.start_servers = 2 ; The desired minimum number of idle server processes. ; Note: Used only when pm is set to 'dynamic' ; Note: Mandatory when pm is set to 'dynamic' pm.min_spare_servers = 1 ; The desired maximum number of idle server processes. ; Note: Used only when pm is set to 'dynamic' ; Note: Mandatory when pm is set to 'dynamic' pm.max_spare_servers = 3 ; The number of rate to spawn child processes at once. ; Note: Used only when pm is set to 'dynamic' ; Note: Mandatory when pm is set to 'dynamic' ; Default Value: 32 ;pm.max_spawn_rate = 32 ; The number of seconds after which an idle process will be killed. ; Note: Used only when pm is set to 'ondemand' ; Default Value: 10s ;pm.process_idle_timeout = 10s; ; The number of requests each child process should execute before respawning. ; This can be useful to work around memory leaks in 3rd party libraries. For ; endless request processing specify '0'. Equivalent to PHP_FCGI_MAX_REQUESTS. ; Default Value: 0 ;pm.max_requests = 500 ; The URI to view the FPM status page. If this value is not set, no URI will be ; recognized as a status page. It shows the following information: ; pool - the name of the pool; ; process manager - static, dynamic or ondemand; ; start time - the date and time FPM has started; ; start since - number of seconds since FPM has started; ; accepted conn - the number of request accepted by the pool; ; listen queue - the number of request in the queue of pending ; connections (see backlog in listen(2)); ; max listen queue - the maximum number of requests in the queue ; of pending connections since FPM has started; ; listen queue len - the size of the socket queue of pending connections; ; idle processes - the number of idle processes; ; active processes - the number of active processes; ; total processes - the number of idle + active processes; ; max active processes - the maximum number of active processes since FPM ; has started; ; max children reached - number of times, the process limit has been reached, ; when pm tries to start more children (works only for ; pm 'dynamic' and 'ondemand'); ; Value are updated in real time. ; Example output: ; pool: www ; process manager: static ; start time: 01/Jul/2011:17:53:49 +0200 ; start since: 62636 ; accepted conn: 190460 ; listen queue: 0 ; max listen queue: 1 ; listen queue len: 42 ; idle processes: 4 ; active processes: 11 ; total processes: 15 ; max active processes: 12 ; max children reached: 0 ; ; By default the status page output is formatted as text/plain. Passing either ; 'html', 'xml' or 'json' in the query string will return the corresponding ; output syntax. Example: ; http://www.foo.bar/status ; http://www.foo.bar/status?json ; http://www.foo.bar/status?html ; http://www.foo.bar/status?xml ; ; By default the status page only outputs short status. Passing 'full' in the ; query string will also return status for each pool process. ; Example: ; http://www.foo.bar/status?full ; http://www.foo.bar/status?json&full ; http://www.foo.bar/status?html&full ; http://www.foo.bar/status?xml&full ; The Full status returns for each process: ; pid - the PID of the process; ; state - the state of the process (Idle, Running, ...); ; start time - the date and time the process has started; ; start since - the number of seconds since the process has started; ; requests - the number of requests the process has served; ; request duration - the duration in µs of the requests; ; request method - the request method (GET, POST, ...); ; request URI - the request URI with the query string; ; content length - the content length of the request (only with POST); ; user - the user (PHP_AUTH_USER) (or '-' if not set); ; script - the main script called (or '-' if not set); ; last request cpu - the %cpu the last request consumed ; it's always 0 if the process is not in Idle state ; because CPU calculation is done when the request ; processing has terminated; ; last request memory - the max amount of memory the last request consumed ; it's always 0 if the process is not in Idle state ; because memory calculation is done when the request ; processing has terminated; ; If the process is in Idle state, then informations are related to the ; last request the process has served. Otherwise informations are related to ; the current request being served. ; Example output: ; ************************ ; pid: 31330 ; state: Running ; start time: 01/Jul/2011:17:53:49 +0200 ; start since: 63087 ; requests: 12808 ; request duration: 1250261 ; request method: GET ; request URI: /test_mem.php?N=10000 ; content length: 0 ; user: - ; script: /home/fat/web/docs/php/test_mem.php ; last request cpu: 0.00 ; last request memory: 0 ; ; Note: There is a real-time FPM status monitoring sample web page available ; It's available in: /usr/local/Cellar/php/8.1.12/share/php/fpm/status.html ; ; Note: The value must start with a leading slash (/). The value can be ; anything, but it may not be a good idea to use the .php extension or it ; may conflict with a real PHP file. ; Default Value: not set ;pm.status_path = /status ; The address on which to accept FastCGI status request. This creates a new ; invisible pool that can handle requests independently. This is useful ; if the main pool is busy with long running requests because it is still possible ; to get the status before finishing the long running requests. ; ; Valid syntaxes are: ; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific IPv4 address on ; a specific port; ; '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on ; a specific port; ; 'port' - to listen on a TCP socket to all addresses ; (IPv6 and IPv4-mapped) on a specific port; ; '/path/to/unix/socket' - to listen on a unix socket. ; Default Value: value of the listen option ;pm.status_listen = 127.0.0.1:9001 ; The ping URI to call the monitoring page of FPM. If this value is not set, no ; URI will be recognized as a ping page. This could be used to test from outside ; that FPM is alive and responding, or to ; - create a graph of FPM availability (rrd or such); ; - remove a server from a group if it is not responding (load balancing); ; - trigger alerts for the operating team (24/7). ; Note: The value must start with a leading slash (/). The value can be ; anything, but it may not be a good idea to use the .php extension or it ; may conflict with a real PHP file. ; Default Value: not set ;ping.path = /ping ; This directive may be used to customize the response of a ping request. The ; response is formatted as text/plain with a 200 response code. ; Default Value: pong ;ping.response = pong ; The access log file ; Default: not set ;access.log = log/$pool.access.log ; The access log format. ; The following syntax is allowed ; %%: the '%' character ; %C: %CPU used by the request ; it can accept the following format: ; - %userC for user CPU only ; - %systemC for system CPU only ; - %totalC for user + system CPU (default) ; %d: time taken to serve the request ; it can accept the following format: ; - %secondsd (default) ; - %millisecondsd ; - %millid ; - %microsecondsd ; - %microd ; %e: an environment variable (same as $_ENV or $_SERVER) ; it must be associated with embraces to specify the name of the env ; variable. Some examples: ; - server specifics like: %REQUEST_METHODe or %SERVER_PROTOCOLe ; - HTTP headers like: %HTTP_HOSTe or %HTTP_USER_AGENTe ; %f: script filename ; %l: content-length of the request (for POST request only) ; %m: request method ; %M: peak of memory allocated by PHP ; it can accept the following format: ; - %bytesM (default) ; - %kilobytesM ; - %kiloM ; - %megabytesM ; - %megaM ; %n: pool name ; %o: output header ; it must be associated with embraces to specify the name of the header: ; - %Content-Typeo ; - %X-Powered-Byo ; - %Transfert-Encodingo ; - .... ; %p: PID of the child that serviced the request ; %P: PID of the parent of the child that serviced the request ; %q: the query string ; %Q: the '?' character if query string exists ; %r: the request URI (without the query string, see %q and %Q) ; %R: remote IP address ; %s: status (response code) ; %t: server time the request was received ; it can accept a strftime(3) format: ; %d/%b/%Y:%H:%M:%S %z (default) ; The strftime(3) format must be encapsulated in a %<strftime_format>t tag ; e.g. for a ISO8601 formatted timestring, use: %%Y-%m-%dT%H:%M:%S%zt ; %T: time the log has been written (the request has finished) ; it can accept a strftime(3) format: ; %d/%b/%Y:%H:%M:%S %z (default) ; The strftime(3) format must be encapsulated in a %<strftime_format>t tag ; e.g. for a ISO8601 formatted timestring, use: %%Y-%m-%dT%H:%M:%S%zt ; %u: remote user ; ; Default: "%R - %u %t \\"%m %r\\" %s" ;access.format = "%R - %u %t \\"%m %r%Q%q\\" %s %f %millid %kiloM %C%%" ; The log file for slow requests ; Default Value: not set ; Note: slowlog is mandatory if request_slowlog_timeout is set ;slowlog = log/$pool.log.slow ; The timeout for serving a single request after which a PHP backtrace will be ; dumped to the 'slowlog' file. A value of '0s' means 'off'. ; Available units: s(econds)(default), m(inutes), h(ours), or d(ays) ; Default Value: 0 ;request_slowlog_timeout = 0 ; Depth of slow log stack trace. ; Default Value: 20 ;request_slowlog_trace_depth = 20 ; The timeout for serving a single request after which the worker process will ; be killed. This option should be used when the 'max_execution_time' ini option ; does not stop script execution for some reason. A value of '0' means 'off'. ; Available units: s(econds)(default), m(inutes), h(ours), or d(ays) ; Default Value: 0 ;request_terminate_timeout = 0 ; The timeout set by 'request_terminate_timeout' ini option is not engaged after ; application calls 'fastcgi_finish_request' or when application has finished and ; shutdown functions are being called (registered via register_shutdown_function). ; This option will enable timeout limit to be applied unconditionally ; even in such cases. ; Default Value: no ;request_terminate_timeout_track_finished = no ; Set open file descriptor rlimit. ; Default Value: system defined value ;rlimit_files = 1024 ; Set max core size rlimit. ; Possible Values: 'unlimited' or an integer greater or equal to 0 ; Default Value: system defined value ;rlimit_core = 0 ; Chroot to this directory at the start. This value must be defined as an ; absolute path. When this value is not set, chroot is not used. ; Note: you can prefix with '$prefix' to chroot to the pool prefix or one ; of its subdirectories. If the pool prefix is not set, the global prefix ; will be used instead. ; Note: chrooting is a great security feature and should be used whenever ; possible. However, all PHP paths will be relative to the chroot ; (error_log, sessions.save_path, ...). ; Default Value: not set ;chroot = ; Chdir to this directory at the start. ; Note: relative path can be used. ; Default Value: current directory or / when chroot ;chdir = /var/www ; Redirect worker stdout and stderr into main error log. If not set, stdout and ; stderr will be redirected to /dev/null according to FastCGI specs. ; Note: on highloaded environment, this can cause some delay in the page ; process time (several ms). ; Default Value: no ;catch_workers_output = yes ; Decorate worker output with prefix and suffix containing information about ; the child that writes to the log and if stdout or stderr is used as well as ; log level and time. This options is used only if catch_workers_output is yes. ; Settings to "no" will output data as written to the stdout or stderr. ; Default value: yes ;decorate_workers_output = no ; Clear environment in FPM workers ; Prevents arbitrary environment variables from reaching FPM worker processes ; by clearing the environment in workers before env vars specified in this ; pool configuration are added. ; Setting to "no" will make all environment variables available to PHP code ; via getenv(), $_ENV and $_SERVER. ; Default Value: yes ;clear_env = no ; Limits the extensions of the main script FPM will allow to parse. This can ; prevent configuration mistakes on the web server side. You should only limit ; FPM to .php extensions to prevent malicious users to use other extensions to ; execute php code. ; Note: set an empty value to allow all extensions. ; Default Value: .php ;security.limit_extensions = .php .php3 .php4 .php5 .php7 ; Pass environment variables like LD_LIBRARY_PATH. All $VARIABLEs are taken from ; the current environment. ; Default Value: clean env ;env[HOSTNAME] = $HOSTNAME ;env[PATH] = /usr/local/bin:/usr/bin:/bin ;env[TMP] = /tmp ;env[TMPDIR] = /tmp ;env[TEMP] = /tmp ; Additional php.ini defines, specific to this pool of workers. These settings ; overwrite the values previously defined in the php.ini. The directives are the ; same as the PHP SAPI: ; php_value/php_flag - you can set classic ini defines which can ; be overwritten from PHP call 'ini_set'. ; php_admin_value/php_admin_flag - these directives won't be overwritten by ; PHP call 'ini_set' ; For php_*flag, valid values are on, off, 1, 0, true, false, yes or no. ; Defining 'extension' will load the corresponding shared extension from ; extension_dir. Defining 'disable_functions' or 'disable_classes' will not ; overwrite previously defined php.ini values, but will append the new value ; instead. ; Note: path INI options can be relative and will be expanded with the prefix ; (pool, global or /usr/local/Cellar/php/8.1.12) ; Default Value: nothing is defined by default except the values in php.ini and ; specified at startup with the -d argument ;php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f www@my.domain.com ;php_flag[display_errors] = off ;php_admin_value[error_log] = /var/log/fpm-php.www.log ;php_admin_flag[log_errors] = on ;php_admin_value[memory_limit] = 32M
-
【配置Nginx】nginx的默认localhost目录:
/usr/local/var/www
为了不与其他的自定义域名冲突,将localhost指向其子目录,修改/usr/local/etc/nginx/nginx.conf,在
假设需要另加一个本地网站xx.com,创建好目录:
server_name localhost;
的后面增加一行:
server_name localhost;
root /usr/local/var/www/localhost/;
然后手动创建好此文件夹:
mkdir -p /usr/local/var/www/localhost/
没有权限的话,前面加sudo与空格,再创建
mkdir -p /usr/local/var/www/xx.com/
- 开始“整合nginx + php”:
修改/usr/local/etc/nginx/nginx.conf
1)将nginx的localhost的默认监听端口从8080改为80 (大概在第36行)
原来
改为listen 8080;
2)在nginx的localhost的默认入口文件列表中,添加index.phplisten 80;
原来
改为index index.html index.htm;
看情况自行修改优先顺序,左边为最优先index index.html index.htm index.php;
3)为nginx的localhost的PHP解析工作方式,设置为socket方式(默认是TCP方式)
原来# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \\.php$ # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #
改为
4)使用try_files告诉nginx主动去找index.php去解析,# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~ \\.php$ fastcgi_pass unix:/var/systemKits/socket/php-fpm.socket; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params;
在“server ”的里面设置:
5)启用nginx的错误日志,找到以下行并去除注释 + 删掉最后的main,try_files $uri $uri/ /index.php?$args;
从
到#error_log logs/error.log;
error_log /usr/local/var/log/nginx/error.log;
6)启用nginx的访问记录日志,找到以下行并去除注释 + 删掉最后的main,
从
到#access_log logs/access.log main;
access_log /usr/local/var/log/nginx/access.log;
- 完整的/usr/local/etc/nginx/nginx.conf配置实例:
#user nobody; worker_processes 1; error_log /usr/local/var/log/nginx/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 /usr/local/var/log/nginx/access.log; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server listen 80; server_name localhost; root /usr/local/var/www/localhost/; #charset koi8-r; #access_log logs/host.access.log main; try_files $uri $uri/ /index.php?$args; location / # root html; index index.html index.htm index.php; #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 html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; # # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~ \\.php$ fastcgi_pass unix:/var/systemKits/socket/php-fpm.socket; 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; # # 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 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / # root html; # index index.html index.htm; # # include servers/*;
- 重启php、再重启nginx
brew services restart php brew services restart nginx
- 在nginx的localhost目录中创建/test/index.php
完整路径: /usr/local/var/www/localhost/test/index.php
内容: <?php phpinfo(); - 浏览器中测试:localhost
- 浏览器中测试:http://localhost/test/
- nginx配置其他虚拟站点xx.com的步骤:
1)修改/etc/hosts,添加127.0.0.1 xx.com
2)创建文件/usr/local/var/www/xx.com/index.php,内容随意
3)创建nginx配置文件 /usr/local/etc/nginx/servers/xx.com.conf,内容为:server listen 80; server_name xx.com; root /usr/local/var/www/xx.com/; try_files $uri $uri/ /index.php?$args; access_log /usr/local/var/log/nginx/xx.com.access.log; location / index index.html index.htm index.php; location ~ \\.php$ fastcgi_pass unix:/var/systemKits/socket/php-fpm.socket; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params;
4)重启nginx,即可访问http://xx.com/ - 将mysql的sock位置告诉PHP,
假设mysql使用的socket的文件是/var/systemKits/socket/mysql.sock,
修改/usr/local/etc/php/8.1/php.ini,
找到:
;pdo_mysql.default_socket
修改为:
pdo_mysql.default_socket = /var/systemKits/socket/mysql.sock
找到:
;mysqli.default_socket =
修改为:
;mysqli.default_socket = /var/systemKits/socket/mysql.sock
启用:
extension=mysqli
extension=pdo_mysql
extension=sqlite3
保存,并重启php服务:
brew services restart php - 完成!
汇总
重启
brew services restart nginx
brew services restart php
brew services restart mysql
错误日志:
/usr/local/var/log/nginx/error.log
/usr/local/var/log/php-fpm.log
访问日志
/usr/local/var/log/nginx/access.log
配置虚拟站点 /usr/local/etc/nginx/servers/xx.com.conf
默认www目录 /usr/local/var/www/
相关概念
先稍微理解下一些概念,不用太仔细,快速看完有个印象。
shell有哪些?Zsh和Bash的区别是什么?
shell有哪些?Zsh和Bash的区别是什么? - 简书
nginx配置选项try_files详解
nginx配置选项try_files详解_势无形的博客-CSDN博客_tryfiles
nginx、php-fpm默认配置与性能–TCP socket还是unix domain socket
nginx、php-fpm默认配置与性能–TCP socket还是unix domain socket_綄羙谎唁的博客-CSDN博客
macos 非root用户运行nginx无法使用80端口的处理办法:(本文已经解决了这个问题)
https://www.jb51.net/article/108059.htm
神作!超级详细的PHP-FPM启蒙:
https://www.cnblogs.com/followyou/p/9460058.html
作用
PHP-FPM(PHP FastCGI Process Manager)意:PHP FastCGI 进程管理器,用于管理PHP 进程池的软件,用于接受web服务器的请求。
PHP-FPM提供了更好的PHP进程管理方式,可以有效控制内存和进程、可以平滑重载PHP配置。
(1). 为什么会出现php-fpm
fpm的出现全部因为php-fastcgi出现。为了很好的管理php-fastcgi而实现的一个程序
(2). 什么是php-fastcgi
php-fastcgi 只是一个cgi程序,只会解析php请求,并且返回结果,不会管理(因此才出现的php-fpm)。
(3)为什么不叫php-cgi
其实在php-fastcgi出现之前是有一个php-cgi存在的,只是它的执行效率低下,因此被php-fastcgi取代。
(4)那fastcgi和cgi有什么区别呢?
亲们,这区别就大了,当一个服务web-server(nginx)分发过来请求的时候,通过匹配后缀知道该请求是个动态的php请求,会把这个请求转给php。
在cgi的年代,思想比较保守,总是一个请求过来后,去读取php.ini里的基础配置信息,初始化执行环境,每次都要不停的去创建一个进程,读取配置,初始化环境,返回数据,退出进程,久而久之,启动进程的工作变的乏味无趣特别累。
当php来到了5的时代,大家对这种工作方式特别反感,想偷懒的人就拼命的想,我可不可以让cgi一次启动一个主进程(master),让他只读取一次配置,然后在启动多个工作进程(worker),当一个请求来的时候,通过master传递给worker这样就可以避免重复劳动了。于是就产生了fastcgi。
(5)fastcgi这么好,启动的worker用完怎么办?
当worker不够的时候,master会通过配置里的信息,动态启动worker,等空闲的时候可以收回worker
(6)到现在还是没明白php-fpm 是个什么东西?
就是来管理启动一个master进程和多个worker进程的程序.
PHP-FPM 会创建一个主进程,控制何时以及如何将HTTP请求转发给一个或多个子进程处理。PHP-FPM主进程还控制着什
么时候创建(处理Web应用更多的流量)和销毁(子进程运行时间太久或不再需要了)
PHP子进程。PHP-FPM进程池中的每个进程存在的时间都比单个HTTP请求长,可以处
理10、50、100、500或更多的HTTP请求。
安装
PHP在 5.3.3 之后已经把php-fpm并入到php的核心代码中了。 所以php-fpm不需要单独的下载安装。
要想php支持php-fpm,只需要在编译php源码的时候带上 --enable-fpm 就可以了。
全局配置
在Centos中,PHP-FPM 的主配置文件是 /etc/php7/php-fpm.conf。
指定一段时间内有指定个子进程失效了,PHP-FPM重启:
#在指定的一段时间内,如果失效的PHP-FPM子进程数超过这个值,PHP-FPM主进程将优雅重启。
emergency_restart_threshold = 10
#设定emergency_restart_interval 设置采用的时间跨度。
emergency_restart_interval = 1m
配置进程池
PHP-FPM配置文件其余的内容是一个名为Pool Defintions的区域。这个区域里的配置用户设置每个PHP-FPM进程池。PHP-FPM进程池中是一系列相关的PHP子进程。通常一个PHP应用有自己一个进程池。
Centos在PHP-FPM主配置文件的顶部引入进程池定义文件:
include=/etc/php7/php-fpm.d/*.conf
www.conf 是PHP-FPM进程池的默认配置文件。
user= nobody
#拥有这个 PHP-FPM进程池中子进程的系统用户。要把这个设置的值设用的非根用户的用户名。
group = nobody
#拥有这个 PHP-FPM进程池中子进程的系统用户组。要把这个设置的值设应用的非根用户所属的用户组名。
listen=[::]]:9000
#PHP-FPM进程池监听的IP地址和端口号,让 PHP-FPM只接受 nginx从这里传入的请求。
listen. allowed clients =127.0.0.1
#可以向这个 PHP-FPM进程池发送请求的IP地址(一个或多个)。
pm.max children =51
#这个设置设定任何时间点 PHP-FPM进程池中最多能有多少个进程。这个设置没有绝对正确的值,你应该测试你的PHP应用,确定每个PHP进程需要使用多少内存,然后把这个设置设为设备可用内存能容纳的PHP进程总数。对大多数中小型PHP应用来说,每个PHP进程要使用5~15MB内存(具体用量可能有差异)。 假设我们使用设备为这个PHP-FPM进程池分配了512MB可用内存,那么可以把这个设置设为(512MB总内存)/(每个进程使用10MB) = 51个进程。
...
编辑保存,重启PHP-FPM主进程:
sudo systemctl restart php-fpm.service
PHP-FPM进程池的配置详情参见 http://php.net/manual/install.fpm.configuration.php
参考Company开发环境
测试环境的配置如下:
[www]
user = nobody #进程的发起用户和用户组,用户user是必须设置,group不是 nobody 任意用户
group = nobody
listen = [::]:9000 #监听ip和端口,[::] 代表任意ip
chdir = /app #在程序启动时将会改变到指定的位置(这个是相对路径,相对当前路径或chroot后的“/”目录)
pm = dynamic #选择进程池管理器如何控制子进程的数量 static: 对于子进程的开启数路给定一个锁定的值(pm.max_children) dynamic: 子进程的数目为动态的,它的数目基于下面的指令的值(以下为dynamic适用参数)
pm.max_children = 16 #同一时刻能够存货的最大子进程的数量
pm.start_servers = 4 #在启动时启动的子进程数量
pm.min_spare_servers = 2 #处于空闲"idle"状态的最小子进程,如果空闲进程数量小于这个值,那么相应的子进程会被创建
pm.max_spare_servers = 16 #最大空闲子进程数量,空闲子进程数量超过这个值,那么相应的子进程会被杀掉。
catch_workers_output = Yes #将worker的标准输出和错误输出重定向到主要的错误日志记录中,如果没有设置,根据FastCGI的指定,将会被重定向到/dev/null上
生产环境配置:
转发请求给PHP-FPM
nginx为例:
server
listen 83;
server_name mobile.com;
root /app/mobile/web/;
error_page 500 502 503 504 /50x.html;
location = /50x.html
root /usr/share/nginx/html;
location /
index index.html index.htm index.php;
# Redirect everything that isn't a real file to index.php
try_files $uri $uri/ /index.php$is_args$args;
#把HTTP请求转发给PHP-FPM进程池处理
location ~ .*\\.php include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 192.168.33.30:9000; #监听9000端口
fastcgi_index index.php;
try_files $uri =404;
#include fastcgi.conf;
location ~ /\\.(ht|svn|git)
deny all;
access_log /app/wwwlogs/access.log;
error_log /app/wwwlogs/error.log;
以上是关于macOS monterey 12.6.1安装homebrew + nginx + php + mysql的主要内容,如果未能解决你的问题,请参考以下文章
.NET 5 虽然已安装但在 MacOS (Monterey) 中找不到
MySQL brew 无法在 macOS Monterey 上运行?
ARM macOS Monterey 上的 /usr/local/bin 损坏
通过brew在macOS Monterey上安装PHP后找不到符号sqlite3_enable_load_extension