目錄
- nginx降權啟動
- 確認普通用戶無法開啟nginx
- 創建普通用戶:
- 測試是否可以啟動nginx:
- 創建必需的相關文件
- 安裝 PHP 7.4,配合 Nginx
- 安裝 PHP 和 PHP FPM 軟件包:
- 修改Nginx配置文件
- 測試:
- 總結
nginx降權啟動
確認普通用戶無法開啟nginx

創建普通用戶:
root@ubuntu:~# useradd -d /home/test -m test root@ubuntu:~# passwd test New password: Retype new password: passwd: password updated successfully root@ubuntu:~#
切換到test用戶:

測試是否可以啟動nginx:

啟動失敗
創建必需的相關文件
$ mkdir nginx $ cd nginx $ mkdir conf logs www sbin
使用root用戶copy配置文件中網頁支持類型文件
root@ubuntu:/www/env/nginx/conf# cp /www/env/nginx/conf/mime.types /home/test/nginx/conf/
使用root用戶拷貝nginx配置文件
root@ubuntu:~# cp /www/env/nginx/conf/nginx.conf /home/test/nginx/conf/

設置權限
root@ubuntu:~# chown -R test:test /www/env/nginx/ # 將當前前目錄下的所有文件與子目錄的擁有者皆設為 test,群體的使用者 test:
修改配置文件
worker_processes 4;
worker_rlimit_nofile 65535;
error_log /home/test/nginx/logs/error.log;
user test test;
pid /home/test/nginx/logs/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
http {
include /home/test/nginx/conf/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8080;
server_name localhost;
root /home/test/nginx/www;
location / {
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
access_log /home/test/nginx/logs/access.log;
}
全路徑啟動nginx -c參數使用指定的配置文件而不是conf目錄下的nginx.conf
/www/env/nginx/sbin/nginx -c /home/test/nginx/conf/nginx.conf &> /dev/null
安裝 PHP 7.4,配合 Nginx
安裝 PHP 和 PHP FPM 軟件包:
apt install php-fpm
檢查服務狀態,運行:
systemctl status php7.4-fpm
修改權限
chmod 777 /run/php/php7.2-fpm.sock
配置php-fpm
修改配置監聽9000端口來處理nginx的請求(這種方法一般在windows上使用),打開 /etc/php/7.2/fpm/pool.d/www.conf 文件找到如下位置注釋第一行添加第二行
;listen = /run/php/php7.2-fpm.sock listen = 127.0.0.1:9000
修改Nginx配置文件
找到下面這部分代碼取消注釋,修改配置
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.conf;
}
測試:
在/www/env/nginx/html下創建index.php文件:
root@ubuntu:/www/env/nginx/html# cat index.php <?php phpinfo() ?>
打開瀏覽器:







