nginx優化-使用普通用戶啓動nginx(監牢模式)

使用普通用戶啓動nginx(監牢模式)

nginx運行時,主進程一般是由root管理的

[root@web01 ~]# ps aux|grep nginx
root       1340  0.0  0.8 572132  1488 ?        Ss   02:41   0:00 nginx: master process /application/nginx/sbin/nginx
www        1341  0.0  5.9 580624 10160 ?        S    02:41   0:00 nginx: worker process        
www        1342  0.0  5.9 580624 10180 ?        S    02:41   0:00 nginx: worker process        
www        1343  0.0  1.1 572132  1980 ?        S    02:41   0:00 nginx: cache manager process 
www        1344  0.0  1.0 572132  1856 ?        S    02:41   0:00 nginx: cache loader process  
root       1352  0.0  0.4 103332   844 pts/0    S+   02:41   0:00 grep --color=auto nginx


部署環境

[root@web01 tools]# cat /etc/redhat-release 
CentOS release 6.9 (Final)
[root@web01 scripts]# uname -r
2.6.32-696.el6.x86_64
[root@web01 tools]# uname -m
x86_64
[root@web01 tools]# uname -a
Linux moban 2.6.32-696.el6.x86_64 #1 SMP Tue Mar 21 19:29:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[root@web01 ~]# getenforce 
Disabled
[root@web01 ~]# /etc/init.d/iptables status
iptables: Firewall is not running.
[root@web01 ~]# ls /application/
nginx  nginx-1.14  php  php-5.5.32


 

在普通用戶家目錄中 創建nginx程序目錄

useradd  cjh
su - cjh
mkdir -p nginx
mkdir -p nginx/{conf,html,logs}

 

添加生成配置文件和數據文件信息

cp /application/nginx/conf/nginx.conf ~/nginx/conf/
cp /application/nginx/conf/mime.types ~/nginx/conf/
ll ~/nginx/conf/

 

編寫nginx配置文件

強調說明:對於普通用戶啓動的一些網絡服務,不能使用知名端口號(1-1024)

vim ~/nginx/conf/nginx.conf

worker_processes  2;
worker_cpu_affinity 0101 1010;
worker_rlimit_nofile 65535;
error_log  /home/cjh/nginx/logs/error.log;
#user cjh cjh;   #普通用戶不能使用此參數配置
pid        /home/cjh/nginx/logs/nginx.pid;
 
events {
    use epoll;
    worker_connections  1024;
}
 
http {
    include      mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
 
    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  /home/cjh/nginx/logs/access.log  main;
 
    server {
        listen       8080;
        server_name  www.123.org;
        root   /home/cjh/nginx/html;
        index  index.html index.htm;
    }
}


nginx -p   指定程序路徑-只有在檢查語法時可以使用

   /application/nginx/sbin/nginx -t -p ~/nginx/

[cjh@web01 ~]$ /application/nginx/sbin/nginx -t -p ~/nginx/
nginx: the configuration file /home/cjh/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /home/cjh/nginx/conf/nginx.conf test is successful


nginx -c   指定配置文件路徑-可以指定配置文件進行(檢查語法 可以啓動nginx)

 

監牢模式 檢查配置文件

   /application/nginx/sbin/nginx -t -p ~/nginx/

[cjh@web01 ~]$ /application/nginx/sbin/nginx -t -p ~/nginx/
nginx: the configuration file /home/cjh/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /home/cjh/nginx/conf/nginx.conf test is successful

 

   /application/nginx/sbin/nginx -t -c ~/nginx/conf/nginx.conf

[cjh@web01 ~]$ /application/nginx/sbin/nginx -t -c ~/nginx/conf/nginx.conf 
nginx: [alert] could not open error log file: open() "/application/nginx-1.14/logs/error.log" failed (13: Permission denied)
2018/10/14 03:05:46 [warn] 1690#0: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /home/cjh/nginx/conf/nginx.conf:5
#注意下面兩行即可
nginx: the configuration file /home/cjh/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /home/cjh/nginx/conf/nginx.conf test is successful


  

監牢模式 啓動nginx

   /application/nginx/sbin/nginx -c ~/nginx/conf/nginx.conf

[cjh@web01 ~]$ /application/nginx/sbin/nginx -c ~/nginx/conf/nginx.conf 
nginx: [alert] could not open error log file: open() "/application/nginx-1.14/logs/error.log" failed (13: Permission denied)
2018/10/14 03:07:18 [warn] 1709#0: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /home/cjh/nginx/conf/nginx.conf:5


 

檢查監牢模式的nginx

   ps aux|grep nginx

[cjh@web01 ~]$ ps aux|grep nginx
root       1340  0.0  0.9 572132  1624 ?        Ss   02:41   0:00 nginx: master process /application/nginx/sbin/nginx
www        1341  0.0  6.0 580624 10236 ?        S    02:41   0:00 nginx: worker process        
www        1342  0.0  6.0 580624 10244 ?        S    02:41   0:00 nginx: worker process        
www        1343  0.0  1.1 572132  1980 ?        S    02:41   0:00 nginx: cache manager process 
cjh        1710  0.0  0.6  44744  1088 ?        Ss   03:07   0:00 nginx: master process /application/nginx/sbin/nginx -c /home/cjh/nginx/conf/nginx.conf
cjh        1711  0.0  0.9  45176  1540 ?        S    03:07   0:00 nginx: worker process                                           
cjh        1712  0.0  0.8  45176  1532 ?        S    03:07   0:00 nginx: worker process                                           
cjh        1714  0.0  0.4 103332   840 pts/0    S+   03:07   0:00 grep --color=auto nginx


 

創建首頁文件

   echo "cjh mail [email protected]" > ~/nginx/html/index.html

瀏覽器訪問測試

image.png


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章