1. 程式人生 > 其它 >nginx配置php-fpm虛擬主機站點

nginx配置php-fpm虛擬主機站點

ubuntu下安裝nginx 很簡單 sudo apt-get install nginx

然後安裝php-fpm 我這本地php7.4所以這麼寫

sudo apt search php7.4-fpm

然後好了以後改一下配置 /etc/php/7.4/fpm/pool.d裡面找到 listen

;listen = /run/php/php7.4-fpm.sock
listen = 127.0.0.1:9000

分號註釋上面的 新增開啟下面的

然後把那些php ini需要開的擴充套件都在 /etc/php/7.4/fpm/php.ini裡面給開起來

重啟php-fpm服務

sudo /etc/init.d/php7.4-fpm restart

看看它現在狀態

$ netstat -tln |grep 9000
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN 

發現已經監聽了埠

$ sudo systemctl status php7.4-fpm
● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 
2022-02-18 13:42:21 +08; 8min ago Docs: man:php-fpm7.4(8) Process: 725830 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/7.4/fpm/pool.d/www.conf 74 (c> Main PID: 725826 (php-fpm7.4) Status: "Processes active: 0, idle: 2, Requests: 2, slow: 0, Traffic: 0req/sec
" Tasks: 3 (limit: 18996) Memory: 16.9M CGroup: /system.slice/php7.4-fpm.service ├─725826 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf) ├─725828 php-fpm: pool www └─725829 php-fpm: pool www 2月 18 13:42:21 cit000174nb systemd[1]: Starting The PHP 7.4 FastCGI Process Manager... 2月 18 13:42:21 cit000174nb systemd[1]: Started The PHP 7.4 FastCGI Process Manager.

並且php-fpm的狀態執行良好

然後去做nginx的配置 建立一個站點檔案

/etc/nginx/sites-available/local.ice.com

編輯內容為

#Log Format
  log_format access_exp '$time_iso8601 | $remote_addr | $request | $status | $request_body | $http_referer | $http_user_agent | $http_x_forwarded_for';

server {
  listen 80;

  root /var/www/html/ice/hello/public;

  index index.html index.htm index.php ;
  server_name local.ice.com;

  # log
  access_log /var/log/nginx/local.ice.com.access.log access_exp;
  error_log /var/log/nginx/local.ice.com.error.log;

  location ~ \.php$ {
    root           /var/www/html/ice/hello/public;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
  }
}

然後來到/etc/nginx/sites-enabled

進行設定一個軟連結檔案

sudo ln -s ../sites-available/local.ice.com local.ice.com

然後檢視一下ls -al

$ ls -al
total 16
drwxr-xr-x 2 root root 4096 2月  18 11:28 ./
drwxr-xr-x 8 root root 4096 6月   7  2021 ../
-rw-r--r-- 1 root root 2416 3月  26  2020 default
-rw-rw-rw- 1 root root  677 2月  18 13:35 local.ice.com

然後就 重啟nginx

sudo systemctl restart nginx.service

如果你只想重啟nginx配置可以使用

nginx -s reload

如果遇到這個提示

nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:1
nginx: [alert] kill(728201, 1) failed (1: Operation not permitted)

加sudo就行了sudo nginx -s reload

啟動後看看/var/log/nginx/local.ice.com.error.log 和/var/log/nginx/local.ice.com.access.log

如果有啥錯誤就按照錯誤提示去改就行了