• macOS monterey 12.6.1安装homebrew + nginx + php + mysql


    效果图

    主要步骤

    1. 安装homebrew
    2. 使用brew安装nginx+php+mysql
    3. 整合nginx、php
    4. 告诉PHP,mysql在哪

    详细步骤

    1. 参考“Homebrew国内如何自动安装(国内地址)(Mac & Linux)”安装brew,
      (因为有些国内IP无法访问安装homebrew,所以以下是国内的资源,贼快)
      命令:
      /bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
    2. 先使用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配置文件中指向这里,
      具体操作:

      1. sudo mkdir -p /var/systemKits/socket/
      2. sudo chmod -R 0777 /var/systemKits/socket/

      再找出mysql配置文件并修改:

      sudo vi /usr/local/etc/my.cnf

      并添加以下代码,保存:

      1. [mysqld]
      2. socket=/var/systemKits/socket/mysql.sock
      3. [client]
      4. socket=/var/systemKits/socket/mysql.sock
      5. [mysql]
      6. socket=/var/systemKits/socket/mysql.sock

      my.cnf最终效果:

      1. # Default Homebrew MySQL server config
      2. [mysqld]
      3. # Only allow connections from localhost
      4. bind-address = 127.0.0.1
      5. mysqlx-bind-address = 127.0.0.1
      6. socket=/var/systemKits/socket/mysql.sock
      7. [client]
      8. socket=/var/systemKits/socket/mysql.sock
      9. [mysql]
      10. socket=/var/systemKits/socket/mysql.sock


      再重启mysql服务:

      brew services restart mysql


      如图:

    3. 开始设置Mysql的root密码,命令行输入
      mysql_secure_installation
      回车,开始设置root密码。
      会先问你要不要什么额外的密码组件,我这里直接no回车,再设置密码








       
    4. 安装php、nginx
      1. brew install php
      2. brew install nginx
    5. 服务开启start、关闭stop、重启restart:
      1. brew services restart nginx
      2. brew services restart php
      3. brew services restart mysql
    6. 查看php、nginx版本:
      nginx -v
      php -v

       
    7. 先备份好nginx和php的配置文件,“后悔药”准备好。
      1)备份php配置文件夹 /usr/local/etc/php/
      2)备份nginx配置文件夹 /usr/local/etc/nginx/
       
    8. 快速了解PHP-FPM
      PHP-FPM详解 - walkingSun - 博客园[TOC] # 作用PHP-FPM(PHP FastCGI Process Manager)意:PHP FastCGI 进程管理器,用于管理PHP 进程池的软件,用于接受web服务器的请求。PHPhttps://www.cnblogs.com/followyou/p/9460058.html
      超级好文!内容已经被我无耻的复制到本文底部,以防丢失,若侵权请告知,谢大神!
       
    9. 为php设置效率最高的非分布式的PHP-FPM进程池监听方式
      先根据自己的爱好方式,提前为php-fpm要用的socket文件准备好可读写的目录,
      例如:
      1. sudo mkdir -p /var/systemKits/socket/
      2. sudo chmod -R 0777 /var/systemKits/socket/
      编辑 /usr/local/etc/php/8.1/php-fpm.d/www.conf ,修改listen监听方式,
      listen = 127.0.0.1:9000
      修改为
       
      listen = /var/systemKits/socket/php-fpm.socket
      路径可以随意...

      另外,设置运行php的用户名&用户组,从
      1. ;user = _www
      2. ;group = _www
      按自己的情况,修改为
      1. user = mac
      2. group = wheel
      再设置以上的socket文件的权限,从
      1. ;listen.owner = _www
      2. ;listen.group = _www
      3. ;listen.mode = 0660
      按自己的情况,修改为
      1. listen.owner = mac
      2. listen.group = wheel
      3. listen.mode = 0777
      重启PHP:
       
      brew services restart php


      完整代码:
      1. ; Start a new pool named 'www'.
      2. ; the variable $pool can be used in any directive and will be replaced by the
      3. ; pool name ('www' here)
      4. [www]
      5. ; Per pool prefix
      6. ; It only applies on the following directives:
      7. ; - 'access.log'
      8. ; - 'slowlog'
      9. ; - 'listen' (unixsocket)
      10. ; - 'chroot'
      11. ; - 'chdir'
      12. ; - 'php_values'
      13. ; - 'php_admin_values'
      14. ; When not set, the global prefix (or /usr/local/Cellar/php/8.2.1_1) applies instead.
      15. ; Note: This directive can also be relative to the global prefix.
      16. ; Default Value: none
      17. ;prefix = /path/to/pools/$pool
      18. ; Unix user/group of processes
      19. ; Note: The user is mandatory. If the group is not set, the default user's group
      20. ; will be used.
      21. ;user = _www
      22. ;group = _www
      23. user = mac
      24. group = wheel
      25. ; The address on which to accept FastCGI requests.
      26. ; Valid syntaxes are:
      27. ; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific IPv4 address on
      28. ; a specific port;
      29. ; '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
      30. ; a specific port;
      31. ; 'port' - to listen on a TCP socket to all addresses
      32. ; (IPv6 and IPv4-mapped) on a specific port;
      33. ; '/path/to/unix/socket' - to listen on a unix socket.
      34. ; Note: This value is mandatory.
      35. ;listen = 127.0.0.1:9000
      36. listen = /var/systemKits/socket/php-fpm.socket
      37. ; Set listen(2) backlog.
      38. ; Default Value: 511 (-1 on Linux, FreeBSD and OpenBSD)
      39. ;listen.backlog = 511
      40. ; Set permissions for unix socket, if one is used. In Linux, read/write
      41. ; permissions must be set in order to allow connections from a web server. Many
      42. ; BSD-derived systems allow connections regardless of permissions. The owner
      43. ; and group can be specified either by name or by their numeric IDs.
      44. ; Default Values: user and group are set as the running user
      45. ; mode is set to 0660
      46. ;listen.owner = _www
      47. ;listen.group = _www
      48. ;listen.mode = 0660
      49. listen.owner = mac
      50. listen.group = wheel
      51. listen.mode = 0777
      52. ; When POSIX Access Control Lists are supported you can set them using
      53. ; these options, value is a comma separated list of user/group names.
      54. ; When set, listen.owner and listen.group are ignored
      55. ;listen.acl_users =
      56. ;listen.acl_groups =
      57. ; List of addresses (IPv4/IPv6) of FastCGI clients which are allowed to connect.
      58. ; Equivalent to the FCGI_WEB_SERVER_ADDRS environment variable in the original
      59. ; PHP FCGI (5.2.2+). Makes sense only with a tcp listening socket. Each address
      60. ; must be separated by a comma. If this value is left blank, connections will be
      61. ; accepted from any ip address.
      62. ; Default Value: any
      63. ;listen.allowed_clients = 127.0.0.1
      64. ; Set the associated the route table (FIB). FreeBSD only
      65. ; Default Value: -1
      66. ;listen.setfib = 1
      67. ; Specify the nice(2) priority to apply to the pool processes (only if set)
      68. ; The value can vary from -19 (highest priority) to 20 (lower priority)
      69. ; Note: - It will only work if the FPM master process is launched as root
      70. ; - The pool processes will inherit the master process priority
      71. ; unless it specified otherwise
      72. ; Default Value: no set
      73. ; process.priority = -19
      74. ; Set the process dumpable flag (PR_SET_DUMPABLE prctl for Linux or
      75. ; PROC_TRACE_CTL procctl for FreeBSD) even if the process user
      76. ; or group is different than the master process user. It allows to create process
      77. ; core dump and ptrace the process for the pool user.
      78. ; Default Value: no
      79. ; process.dumpable = yes
      80. ; Choose how the process manager will control the number of child processes.
      81. ; Possible Values:
      82. ; static - a fixed number (pm.max_children) of child processes;
      83. ; dynamic - the number of child processes are set dynamically based on the
      84. ; following directives. With this process management, there will be
      85. ; always at least 1 children.
      86. ; pm.max_children - the maximum number of children that can
      87. ; be alive at the same time.
      88. ; pm.start_servers - the number of children created on startup.
      89. ; pm.min_spare_servers - the minimum number of children in 'idle'
      90. ; state (waiting to process). If the number
      91. ; of 'idle' processes is less than this
      92. ; number then some children will be created.
      93. ; pm.max_spare_servers - the maximum number of children in 'idle'
      94. ; state (waiting to process). If the number
      95. ; of 'idle' processes is greater than this
      96. ; number then some children will be killed.
      97. ; pm.max_spawn_rate - the maximum number of rate to spawn child
      98. ; processes at once.
      99. ; ondemand - no children are created at startup. Children will be forked when
      100. ; new requests will connect. The following parameter are used:
      101. ; pm.max_children - the maximum number of children that
      102. ; can be alive at the same time.
      103. ; pm.process_idle_timeout - The number of seconds after which
      104. ; an idle process will be killed.
      105. ; Note: This value is mandatory.
      106. pm = dynamic
      107. ; The number of child processes to be created when pm is set to 'static' and the
      108. ; maximum number of child processes when pm is set to 'dynamic' or 'ondemand'.
      109. ; This value sets the limit on the number of simultaneous requests that will be
      110. ; served. Equivalent to the ApacheMaxClients directive with mpm_prefork.
      111. ; Equivalent to the PHP_FCGI_CHILDREN environment variable in the original PHP
      112. ; CGI. The below defaults are based on a server without much resources. Don't
      113. ; forget to tweak pm.* to fit your needs.
      114. ; Note: Used when pm is set to 'static', 'dynamic' or 'ondemand'
      115. ; Note: This value is mandatory.
      116. pm.max_children = 5
      117. ; The number of child processes created on startup.
      118. ; Note: Used only when pm is set to 'dynamic'
      119. ; Default Value: (min_spare_servers + max_spare_servers) / 2
      120. pm.start_servers = 2
      121. ; The desired minimum number of idle server processes.
      122. ; Note: Used only when pm is set to 'dynamic'
      123. ; Note: Mandatory when pm is set to 'dynamic'
      124. pm.min_spare_servers = 1
      125. ; The desired maximum number of idle server processes.
      126. ; Note: Used only when pm is set to 'dynamic'
      127. ; Note: Mandatory when pm is set to 'dynamic'
      128. pm.max_spare_servers = 3
      129. ; The number of rate to spawn child processes at once.
      130. ; Note: Used only when pm is set to 'dynamic'
      131. ; Note: Mandatory when pm is set to 'dynamic'
      132. ; Default Value: 32
      133. ;pm.max_spawn_rate = 32
      134. ; The number of seconds after which an idle process will be killed.
      135. ; Note: Used only when pm is set to 'ondemand'
      136. ; Default Value: 10s
      137. ;pm.process_idle_timeout = 10s;
      138. ; The number of requests each child process should execute before respawning.
      139. ; This can be useful to work around memory leaks in 3rd party libraries. For
      140. ; endless request processing specify '0'. Equivalent to PHP_FCGI_MAX_REQUESTS.
      141. ; Default Value: 0
      142. ;pm.max_requests = 500
      143. ; The URI to view the FPM status page. If this value is not set, no URI will be
      144. ; recognized as a status page. It shows the following information:
      145. ; pool - the name of the pool;
      146. ; process manager - static, dynamic or ondemand;
      147. ; start time - the date and time FPM has started;
      148. ; start since - number of seconds since FPM has started;
      149. ; accepted conn - the number of request accepted by the pool;
      150. ; listen queue - the number of request in the queue of pending
      151. ; connections (see backlog in listen(2));
      152. ; max listen queue - the maximum number of requests in the queue
      153. ; of pending connections since FPM has started;
      154. ; listen queue len - the size of the socket queue of pending connections;
      155. ; idle processes - the number of idle processes;
      156. ; active processes - the number of active processes;
      157. ; total processes - the number of idle + active processes;
      158. ; max active processes - the maximum number of active processes since FPM
      159. ; has started;
      160. ; max children reached - number of times, the process limit has been reached,
      161. ; when pm tries to start more children (works only for
      162. ; pm 'dynamic' and 'ondemand');
      163. ; Value are updated in real time.
      164. ; Example output:
      165. ; pool: www
      166. ; process manager: static
      167. ; start time: 01/Jul/2011:17:53:49 +0200
      168. ; start since: 62636
      169. ; accepted conn: 190460
      170. ; listen queue: 0
      171. ; max listen queue: 1
      172. ; listen queue len: 42
      173. ; idle processes: 4
      174. ; active processes: 11
      175. ; total processes: 15
      176. ; max active processes: 12
      177. ; max children reached: 0
      178. ;
      179. ; By default the status page output is formatted as text/plain. Passing either
      180. ; 'html', 'xml' or 'json' in the query string will return the corresponding
      181. ; output syntax. Example:
      182. ; http://www.foo.bar/status
      183. ; http://www.foo.bar/status?json
      184. ; http://www.foo.bar/status?html
      185. ; http://www.foo.bar/status?xml
      186. ;
      187. ; By default the status page only outputs short status. Passing 'full' in the
      188. ; query string will also return status for each pool process.
      189. ; Example:
      190. ; http://www.foo.bar/status?full
      191. ; http://www.foo.bar/status?json&full
      192. ; http://www.foo.bar/status?html&full
      193. ; http://www.foo.bar/status?xml&full
      194. ; The Full status returns for each process:
      195. ; pid - the PID of the process;
      196. ; state - the state of the process (Idle, Running, ...);
      197. ; start time - the date and time the process has started;
      198. ; start since - the number of seconds since the process has started;
      199. ; requests - the number of requests the process has served;
      200. ; request duration - the duration in µs of the requests;
      201. ; request method - the request method (GET, POST, ...);
      202. ; request URI - the request URI with the query string;
      203. ; content length - the content length of the request (only with POST);
      204. ; user - the user (PHP_AUTH_USER) (or '-' if not set);
      205. ; script - the main script called (or '-' if not set);
      206. ; last request cpu - the %cpu the last request consumed
      207. ; it's always 0 if the process is not in Idle state
      208. ; because CPU calculation is done when the request
      209. ; processing has terminated;
      210. ; last request memory - the max amount of memory the last request consumed
      211. ; it's always 0 if the process is not in Idle state
      212. ; because memory calculation is done when the request
      213. ; processing has terminated;
      214. ; If the process is in Idle state, then informations are related to the
      215. ; last request the process has served. Otherwise informations are related to
      216. ; the current request being served.
      217. ; Example output:
      218. ; ************************
      219. ; pid: 31330
      220. ; state: Running
      221. ; start time: 01/Jul/2011:17:53:49 +0200
      222. ; start since: 63087
      223. ; requests: 12808
      224. ; request duration: 1250261
      225. ; request method: GET
      226. ; request URI: /test_mem.php?N=10000
      227. ; content length: 0
      228. ; user: -
      229. ; script: /home/fat/web/docs/php/test_mem.php
      230. ; last request cpu: 0.00
      231. ; last request memory: 0
      232. ;
      233. ; Note: There is a real-time FPM status monitoring sample web page available
      234. ; It's available in: /usr/local/Cellar/php/8.2.1_1/share/php/fpm/status.html
      235. ;
      236. ; Note: The value must start with a leading slash (/). The value can be
      237. ; anything, but it may not be a good idea to use the .php extension or it
      238. ; may conflict with a real PHP file.
      239. ; Default Value: not set
      240. ;pm.status_path = /status
      241. ; The address on which to accept FastCGI status request. This creates a new
      242. ; invisible pool that can handle requests independently. This is useful
      243. ; if the main pool is busy with long running requests because it is still possible
      244. ; to get the status before finishing the long running requests.
      245. ;
      246. ; Valid syntaxes are:
      247. ; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific IPv4 address on
      248. ; a specific port;
      249. ; '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
      250. ; a specific port;
      251. ; 'port' - to listen on a TCP socket to all addresses
      252. ; (IPv6 and IPv4-mapped) on a specific port;
      253. ; '/path/to/unix/socket' - to listen on a unix socket.
      254. ; Default Value: value of the listen option
      255. ;pm.status_listen = 127.0.0.1:9001
      256. ; The ping URI to call the monitoring page of FPM. If this value is not set, no
      257. ; URI will be recognized as a ping page. This could be used to test from outside
      258. ; that FPM is alive and responding, or to
      259. ; - create a graph of FPM availability (rrd or such);
      260. ; - remove a server from a group if it is not responding (load balancing);
      261. ; - trigger alerts for the operating team (24/7).
      262. ; Note: The value must start with a leading slash (/). The value can be
      263. ; anything, but it may not be a good idea to use the .php extension or it
      264. ; may conflict with a real PHP file.
      265. ; Default Value: not set
      266. ;ping.path = /ping
      267. ; This directive may be used to customize the response of a ping request. The
      268. ; response is formatted as text/plain with a 200 response code.
      269. ; Default Value: pong
      270. ;ping.response = pong
      271. ; The access log file
      272. ; Default: not set
      273. ;access.log = log/$pool.access.log
      274. ; The access log format.
      275. ; The following syntax is allowed
      276. ; %%: the '%' character
      277. ; %C: %CPU used by the request
      278. ; it can accept the following format:
      279. ; - %{user}C for user CPU only
      280. ; - %{system}C for system CPU only
      281. ; - %{total}C for user + system CPU (default)
      282. ; %d: time taken to serve the request
      283. ; it can accept the following format:
      284. ; - %{seconds}d (default)
      285. ; - %{milliseconds}d
      286. ; - %{milli}d
      287. ; - %{microseconds}d
      288. ; - %{micro}d
      289. ; %e: an environment variable (same as $_ENV or $_SERVER)
      290. ; it must be associated with embraces to specify the name of the env
      291. ; variable. Some examples:
      292. ; - server specifics like: %{REQUEST_METHOD}e or %{SERVER_PROTOCOL}e
      293. ; - HTTP headers like: %{HTTP_HOST}e or %{HTTP_USER_AGENT}e
      294. ; %f: script filename
      295. ; %l: content-length of the request (for POST request only)
      296. ; %m: request method
      297. ; %M: peak of memory allocated by PHP
      298. ; it can accept the following format:
      299. ; - %{bytes}M (default)
      300. ; - %{kilobytes}M
      301. ; - %{kilo}M
      302. ; - %{megabytes}M
      303. ; - %{mega}M
      304. ; %n: pool name
      305. ; %o: output header
      306. ; it must be associated with embraces to specify the name of the header:
      307. ; - %{Content-Type}o
      308. ; - %{X-Powered-By}o
      309. ; - %{Transfert-Encoding}o
      310. ; - ....
      311. ; %p: PID of the child that serviced the request
      312. ; %P: PID of the parent of the child that serviced the request
      313. ; %q: the query string
      314. ; %Q: the '?' character if query string exists
      315. ; %r: the request URI (without the query string, see %q and %Q)
      316. ; %R: remote IP address
      317. ; %s: status (response code)
      318. ; %t: server time the request was received
      319. ; it can accept a strftime(3) format:
      320. ; %d/%b/%Y:%H:%M:%S %z (default)
      321. ; The strftime(3) format must be encapsulated in a %{}t tag
      322. ; e.g. for a ISO8601 formatted timestring, use: %{%Y-%m-%dT%H:%M:%S%z}t
      323. ; %T: time the log has been written (the request has finished)
      324. ; it can accept a strftime(3) format:
      325. ; %d/%b/%Y:%H:%M:%S %z (default)
      326. ; The strftime(3) format must be encapsulated in a %{}t tag
      327. ; e.g. for a ISO8601 formatted timestring, use: %{%Y-%m-%dT%H:%M:%S%z}t
      328. ; %u: remote user
      329. ;
      330. ; Default: "%R - %u %t \"%m %r\" %s"
      331. ;access.format = "%R - %u %t \"%m %r%Q%q\" %s %f %{milli}d %{kilo}M %C%%"
      332. ; A list of request_uri values which should be filtered from the access log.
      333. ;
      334. ; As a security precuation, this setting will be ignored if:
      335. ; - the request method is not GET or HEAD; or
      336. ; - there is a request body; or
      337. ; - there are query parameters; or
      338. ; - the response code is outwith the successful range of 200 to 299
      339. ;
      340. ; Note: The paths are matched against the output of the access.format tag "%r".
      341. ; On common configurations, this may look more like SCRIPT_NAME than the
      342. ; expected pre-rewrite URI.
      343. ;
      344. ; Default Value: not set
      345. ;access.suppress_path[] = /ping
      346. ;access.suppress_path[] = /health_check.php
      347. ; The log file for slow requests
      348. ; Default Value: not set
      349. ; Note: slowlog is mandatory if request_slowlog_timeout is set
      350. ;slowlog = log/$pool.log.slow
      351. ; The timeout for serving a single request after which a PHP backtrace will be
      352. ; dumped to the 'slowlog' file. A value of '0s' means 'off'.
      353. ; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
      354. ; Default Value: 0
      355. ;request_slowlog_timeout = 0
      356. ; Depth of slow log stack trace.
      357. ; Default Value: 20
      358. ;request_slowlog_trace_depth = 20
      359. ; The timeout for serving a single request after which the worker process will
      360. ; be killed. This option should be used when the 'max_execution_time' ini option
      361. ; does not stop script execution for some reason. A value of '0' means 'off'.
      362. ; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
      363. ; Default Value: 0
      364. ;request_terminate_timeout = 0
      365. ; The timeout set by 'request_terminate_timeout' ini option is not engaged after
      366. ; application calls 'fastcgi_finish_request' or when application has finished and
      367. ; shutdown functions are being called (registered via register_shutdown_function).
      368. ; This option will enable timeout limit to be applied unconditionally
      369. ; even in such cases.
      370. ; Default Value: no
      371. ;request_terminate_timeout_track_finished = no
      372. ; Set open file descriptor rlimit.
      373. ; Default Value: system defined value
      374. ;rlimit_files = 1024
      375. ; Set max core size rlimit.
      376. ; Possible Values: 'unlimited' or an integer greater or equal to 0
      377. ; Default Value: system defined value
      378. ;rlimit_core = 0
      379. ; Chroot to this directory at the start. This value must be defined as an
      380. ; absolute path. When this value is not set, chroot is not used.
      381. ; Note: you can prefix with '$prefix' to chroot to the pool prefix or one
      382. ; of its subdirectories. If the pool prefix is not set, the global prefix
      383. ; will be used instead.
      384. ; Note: chrooting is a great security feature and should be used whenever
      385. ; possible. However, all PHP paths will be relative to the chroot
      386. ; (error_log, sessions.save_path, ...).
      387. ; Default Value: not set
      388. ;chroot =
      389. ; Chdir to this directory at the start.
      390. ; Note: relative path can be used.
      391. ; Default Value: current directory or / when chroot
      392. ;chdir = /var/www
      393. ; Redirect worker stdout and stderr into main error log. If not set, stdout and
      394. ; stderr will be redirected to /dev/null according to FastCGI specs.
      395. ; Note: on highloaded environment, this can cause some delay in the page
      396. ; process time (several ms).
      397. ; Default Value: no
      398. ;catch_workers_output = yes
      399. ; Decorate worker output with prefix and suffix containing information about
      400. ; the child that writes to the log and if stdout or stderr is used as well as
      401. ; log level and time. This options is used only if catch_workers_output is yes.
      402. ; Settings to "no" will output data as written to the stdout or stderr.
      403. ; Default value: yes
      404. ;decorate_workers_output = no
      405. ; Clear environment in FPM workers
      406. ; Prevents arbitrary environment variables from reaching FPM worker processes
      407. ; by clearing the environment in workers before env vars specified in this
      408. ; pool configuration are added.
      409. ; Setting to "no" will make all environment variables available to PHP code
      410. ; via getenv(), $_ENV and $_SERVER.
      411. ; Default Value: yes
      412. ;clear_env = no
      413. ; Limits the extensions of the main script FPM will allow to parse. This can
      414. ; prevent configuration mistakes on the web server side. You should only limit
      415. ; FPM to .php extensions to prevent malicious users to use other extensions to
      416. ; execute php code.
      417. ; Note: set an empty value to allow all extensions.
      418. ; Default Value: .php
      419. ;security.limit_extensions = .php .php3 .php4 .php5 .php7
      420. ; Pass environment variables like LD_LIBRARY_PATH. All $VARIABLEs are taken from
      421. ; the current environment.
      422. ; Default Value: clean env
      423. ;env[HOSTNAME] = $HOSTNAME
      424. ;env[PATH] = /usr/local/bin:/usr/bin:/bin
      425. ;env[TMP] = /tmp
      426. ;env[TMPDIR] = /tmp
      427. ;env[TEMP] = /tmp
      428. ; Additional php.ini defines, specific to this pool of workers. These settings
      429. ; overwrite the values previously defined in the php.ini. The directives are the
      430. ; same as the PHP SAPI:
      431. ; php_value/php_flag - you can set classic ini defines which can
      432. ; be overwritten from PHP call 'ini_set'.
      433. ; php_admin_value/php_admin_flag - these directives won't be overwritten by
      434. ; PHP call 'ini_set'
      435. ; For php_*flag, valid values are on, off, 1, 0, true, false, yes or no.
      436. ; Defining 'extension' will load the corresponding shared extension from
      437. ; extension_dir. Defining 'disable_functions' or 'disable_classes' will not
      438. ; overwrite previously defined php.ini values, but will append the new value
      439. ; instead.
      440. ; Note: path INI options can be relative and will be expanded with the prefix
      441. ; (pool, global or /usr/local/Cellar/php/8.2.1_1)
      442. ; Default Value: nothing is defined by default except the values in php.ini and
      443. ; specified at startup with the -d argument
      444. ;php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f www@my.domain.com
      445. ;php_flag[display_errors] = off
      446. ;php_admin_value[error_log] = /var/log/fpm-php.www.log
      447. ;php_admin_flag[log_errors] = on
      448. ;php_admin_value[memory_limit] = 32M
    10.  【配置Nginx】nginx的默认localhost目录:

      /usr/local/var/www

      为了不与其他的自定义域名冲突,将localhost指向其子目录,修改/usr/local/etc/nginx/nginx.conf,在
      server_name localhost;
      的后面增加一行:
      server_name localhost;
      root /usr/local/var/www/localhost/;

      然后手动创建好此文件夹:
      mkdir -p /usr/local/var/www/localhost/
      没有权限的话,前面加sudo与空格,再创建

      假设需要另加一个本地网站xx.com,创建好目录:
      mkdir -p /usr/local/var/www/xx.com/

       
    11. 开始“整合nginx + php”:
      修改/usr/local/etc/nginx/nginx.conf
      1)将nginx的localhost的默认监听端口从8080改为80  (大概在第36行)
      原来
      listen       8080;
      改为
      listen       80;
      2)在nginx的localhost的默认入口文件列表中,添加index.php
      原来
      index  index.html index.htm;
      改为
      index  index.html index.htm index.php;
      看情况自行修改优先顺序,左边为最优先

      3)为nginx的localhost的PHP解析工作方式,设置为socket方式(默认是TCP方式)
      原来
      1. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
      2. #
      3. #location ~ \.php$ {
      4. # root html;
      5. # fastcgi_pass 127.0.0.1:9000;
      6. # fastcgi_index index.php;
      7. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
      8. # include fastcgi_params;
      9. #}

      改为
      1. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
      2. location ~ \.php$ {
      3. fastcgi_pass unix:/var/systemKits/socket/php-fpm.socket;
      4. fastcgi_index index.php;
      5. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      6. include fastcgi_params;
      7. }
      4)使用try_files告诉nginx主动去找index.php去解析,
      在“server {”的里面设置:
      try_files $uri $uri/ /index.php?$args;
      5)启用nginx的错误日志,找到以下行并去除注释 + 删掉最后的main,
      #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;


       
    12. 完整的/usr/local/etc/nginx/nginx.conf配置实例:
      1. #user nobody;
      2. worker_processes 1;
      3. error_log /usr/local/var/log/nginx/error.log;
      4. #error_log logs/error.log notice;
      5. #error_log logs/error.log info;
      6. #pid logs/nginx.pid;
      7. events {
      8. worker_connections 1024;
      9. }
      10. http {
      11. include mime.types;
      12. default_type application/octet-stream;
      13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
      14. # '$status $body_bytes_sent "$http_referer" '
      15. # '"$http_user_agent" "$http_x_forwarded_for"';
      16. access_log /usr/local/var/log/nginx/access.log;
      17. sendfile on;
      18. #tcp_nopush on;
      19. #keepalive_timeout 0;
      20. keepalive_timeout 65;
      21. #gzip on;
      22. server {
      23. listen 80;
      24. server_name localhost;
      25. root /usr/local/var/www/localhost/;
      26. #charset koi8-r;
      27. #access_log logs/host.access.log main;
      28. try_files $uri $uri/ /index.php?$args;
      29. location / {
      30. # root html;
      31. index index.html index.htm index.php;
      32. }
      33. #error_page 404 /404.html;
      34. # redirect server error pages to the static page /50x.html
      35. #
      36. error_page 500 502 503 504 /50x.html;
      37. location = /50x.html {
      38. root html;
      39. }
      40. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
      41. #
      42. #location ~ \.php$ {
      43. # proxy_pass http://127.0.0.1;
      44. #}
      45. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
      46. #
      47. #location ~ \.php$ {
      48. # root html;
      49. # fastcgi_pass 127.0.0.1:9000;
      50. # fastcgi_index index.php;
      51. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
      52. # include fastcgi_params;
      53. #}
      54. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
      55. location ~ \.php$ {
      56. fastcgi_pass unix:/var/systemKits/socket/php-fpm.socket;
      57. fastcgi_index index.php;
      58. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      59. include fastcgi_params;
      60. }
      61. # deny access to .htaccess files, if Apache's document root
      62. # concurs with nginx's one
      63. #
      64. #location ~ /\.ht {
      65. # deny all;
      66. #}
      67. }
      68. # another virtual host using mix of IP-, name-, and port-based configuration
      69. #
      70. #server {
      71. # listen 8000;
      72. # listen somename:8080;
      73. # server_name somename alias another.alias;
      74. # location / {
      75. # root html;
      76. # index index.html index.htm;
      77. # }
      78. #}
      79. # HTTPS server
      80. #
      81. #server {
      82. # listen 443 ssl;
      83. # server_name localhost;
      84. # ssl_certificate cert.pem;
      85. # ssl_certificate_key cert.key;
      86. # ssl_session_cache shared:SSL:1m;
      87. # ssl_session_timeout 5m;
      88. # ssl_ciphers HIGH:!aNULL:!MD5;
      89. # ssl_prefer_server_ciphers on;
      90. # location / {
      91. # root html;
      92. # index index.html index.htm;
      93. # }
      94. #}
      95. include servers/*;
      96. }

       
    13. 重启php、再重启nginx
      1. brew services restart php
      2. brew services restart nginx

       
    14. 在nginx的localhost目录中创建/test/index.php
      完整路径: /usr/local/var/www/localhost/test/index.php
      内容:
    15. 浏览器中测试:localhost
    16. 浏览器中测试:http://localhost/test/

       
    17. 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,内容为:
      1. server {
      2. listen 80;
      3. server_name xx.com;
      4. root /usr/local/var/www/xx.com/;
      5. try_files $uri $uri/ /index.php?$args;
      6. access_log /usr/local/var/log/nginx/xx.com.access.log;
      7. location / {
      8. index index.html index.htm index.php;
      9. }
      10. location ~ \.php$ {
      11. fastcgi_pass unix:/var/systemKits/socket/php-fpm.socket;
      12. fastcgi_index index.php;
      13. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      14. include fastcgi_params;
      15. }
      16. }

      4)重启nginx,即可访问http://xx.com/
    18. 将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
    19. 完成!

    汇总

    重启
    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

    1. 作用
    2. PHP-FPM(PHP FastCGI Process Manager)意:PHP FastCGI 进程管理器,用于管理PHP 进程池的软件,用于接受web服务器的请求。
    3. PHP-FPM提供了更好的PHP进程管理方式,可以有效控制内存和进程、可以平滑重载PHP配置。
    4. (1). 为什么会出现php-fpm
    5. fpm的出现全部因为php-fastcgi出现。为了很好的管理php-fastcgi而实现的一个程序
    6. (2). 什么是php-fastcgi
    7. php-fastcgi 只是一个cgi程序,只会解析php请求,并且返回结果,不会管理(因此才出现的php-fpm)。
    8. (3)为什么不叫php-cgi
    9. 其实在php-fastcgi出现之前是有一个php-cgi存在的,只是它的执行效率低下,因此被php-fastcgi取代。
    10. (4)那fastcgi和cgi有什么区别呢?
    11. 亲们,这区别就大了,当一个服务web-server(nginx)分发过来请求的时候,通过匹配后缀知道该请求是个动态的php请求,会把这个请求转给php。
    12. 在cgi的年代,思想比较保守,总是一个请求过来后,去读取php.ini里的基础配置信息,初始化执行环境,每次都要不停的去创建一个进程,读取配置,初始化环境,返回数据,退出进程,久而久之,启动进程的工作变的乏味无趣特别累。
    13. 当php来到了5的时代,大家对这种工作方式特别反感,想偷懒的人就拼命的想,我可不可以让cgi一次启动一个主进程(master),让他只读取一次配置,然后在启动多个工作进程(worker),当一个请求来的时候,通过master传递给worker这样就可以避免重复劳动了。于是就产生了fastcgi。
    14. (5)fastcgi这么好,启动的worker用完怎么办?
    15. 当worker不够的时候,master会通过配置里的信息,动态启动worker,等空闲的时候可以收回worker
    16. (6)到现在还是没明白php-fpm 是个什么东西?
    17. 就是来管理启动一个master进程和多个worker进程的程序.
    18. PHP-FPM 会创建一个主进程,控制何时以及如何将HTTP请求转发给一个或多个子进程处理。PHP-FPM主进程还控制着什
    19. 么时候创建(处理Web应用更多的流量)和销毁(子进程运行时间太久或不再需要了)
    20. PHP子进程。PHP-FPM进程池中的每个进程存在的时间都比单个HTTP请求长,可以处
    21. 理10、50、100、500或更多的HTTP请求。
    22. 安装
    23. PHP在 5.3.3 之后已经把php-fpm并入到php的核心代码中了。 所以php-fpm不需要单独的下载安装。
    24. 要想php支持php-fpm,只需要在编译php源码的时候带上 --enable-fpm 就可以了。
    25. 全局配置
    26. 在Centos中,PHP-FPM 的主配置文件是 /etc/php7/php-fpm.conf。
    27. 指定一段时间内有指定个子进程失效了,PHP-FPM重启:
    28. #在指定的一段时间内,如果失效的PHP-FPM子进程数超过这个值,PHP-FPM主进程将优雅重启。
    29. emergency_restart_threshold = 10
    30. #设定emergency_restart_interval 设置采用的时间跨度。
    31. emergency_restart_interval = 1m
    32. 配置进程池
    33. PHP-FPM配置文件其余的内容是一个名为Pool Defintions的区域。这个区域里的配置用户设置每个PHP-FPM进程池。PHP-FPM进程池中是一系列相关的PHP子进程。通常一个PHP应用有自己一个进程池。
    34. Centos在PHP-FPM主配置文件的顶部引入进程池定义文件:
    35. include=/etc/php7/php-fpm.d/*.conf
    36. www.conf 是PHP-FPM进程池的默认配置文件。
    37. user= nobody
    38. #拥有这个 PHP-FPM进程池中子进程的系统用户。要把这个设置的值设用的非根用户的用户名。
    39. group = nobody
    40. #拥有这个 PHP-FPM进程池中子进程的系统用户组。要把这个设置的值设应用的非根用户所属的用户组名。
    41. listen=[::]]:9000
    42. #PHP-FPM进程池监听的IP地址和端口号,让 PHP-FPM只接受 nginx从这里传入的请求。
    43. listen. allowed clients =127.0.0.1
    44. #可以向这个 PHP-FPM进程池发送请求的IP地址(一个或多个)。
    45. pm.max children =51
    46. #这个设置设定任何时间点 PHP-FPM进程池中最多能有多少个进程。这个设置没有绝对正确的值,你应该测试你的PHP应用,确定每个PHP进程需要使用多少内存,然后把这个设置设为设备可用内存能容纳的PHP进程总数。对大多数中小型PHP应用来说,每个PHP进程要使用5~15MB内存(具体用量可能有差异)。 假设我们使用设备为这个PHP-FPM进程池分配了512MB可用内存,那么可以把这个设置设为(512MB总内存)/(每个进程使用10MB) = 51个进程。
    47. ...
    48. 编辑保存,重启PHP-FPM主进程:
    49. sudo systemctl restart php-fpm.service
    50. PHP-FPM进程池的配置详情参见 http://php.net/manual/install.fpm.configuration.php
    51. 参考Company开发环境
    52. 测试环境的配置如下:
    53. [www]
    54. user = nobody #进程的发起用户和用户组,用户user是必须设置,group不是 nobody 任意用户
    55. group = nobody
    56. listen = [::]:9000 #监听ip和端口,[::] 代表任意ip
    57. chdir = /app #在程序启动时将会改变到指定的位置(这个是相对路径,相对当前路径或chroot后的“/”目录) 
    58. pm = dynamic #选择进程池管理器如何控制子进程的数量 static:  对于子进程的开启数路给定一个锁定的值(pm.max_children) dynamic:  子进程的数目为动态的,它的数目基于下面的指令的值(以下为dynamic适用参数)
    59. pm.max_children = 16 #同一时刻能够存货的最大子进程的数量
    60. pm.start_servers = 4 #在启动时启动的子进程数量
    61. pm.min_spare_servers = 2 #处于空闲"idle"状态的最小子进程,如果空闲进程数量小于这个值,那么相应的子进程会被创建
    62. pm.max_spare_servers = 16 #最大空闲子进程数量,空闲子进程数量超过这个值,那么相应的子进程会被杀掉。
    63. catch_workers_output = Yes #将worker的标准输出和错误输出重定向到主要的错误日志记录中,如果没有设置,根据FastCGI的指定,将会被重定向到/dev/null上
    64. 生产环境配置:
    65. 转发请求给PHP-FPM
    66. nginx为例:
    67. server {
    68. listen 83;
    69. server_name mobile.com;
    70. root /app/mobile/web/;
    71. error_page 500 502 503 504 /50x.html;
    72. location = /50x.html {
    73. root /usr/share/nginx/html;
    74. }
    75. location / {
    76. index index.html index.htm index.php;
    77. # Redirect everything that isn't a real file to index.php
    78. try_files $uri $uri/ /index.php$is_args$args;
    79. }
    80. #把HTTP请求转发给PHP-FPM进程池处理
    81. location ~ .*\.php include fastcgi_params;
    82. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    83. fastcgi_pass 192.168.33.30:9000; #监听9000端口
    84. fastcgi_index index.php;
    85. try_files $uri =404;
    86. #include fastcgi.conf;
    87. }
    88. location ~ /\.(ht|svn|git) {
    89. deny all;
    90. }
    91. access_log /app/wwwlogs/access.log;
    92. error_log /app/wwwlogs/error.log;
    93. }

  • 相关阅读:
    「TCP 重要机制」滑动窗口 & 粘包问题 & 异常情况处理
    复盘:智能座舱系列文六- 它的3种交互方式之显式交互(语音以及显示)
    【Java项目】我是如何实现抢红包功能的?
    两种基于时间窗口的限流器的简单实现
    一分钟带你了解C语言中数据在内存中的存储详解!
    Linux下安装和使用MySQL的详细教程
    PDFView4NET 2022.09.02 Crack
    面试题精讲:你所不知道的Lambda表达式和常用的函数式接口
    Cisco(十一)—STP
    小程序支付详解
  • 原文地址:https://blog.csdn.net/qq285744011/article/details/127955548