一、介绍
1.1、什么是nginx降权启动
降权启动:即nginx的启动与管理使用非root用户来启动与管理,这样就防止每次修改配置重启时都需要用root用户来重启了。
注意:普通用户只能只用1024以上的端口,不可以直接使用80或者443端口(前面可以用代理来解决这一问题)。
二、实践
操作说明:使用www用户来管理与启动nginx。
2.1、如果是新装的系统需要做基础优化
2.2、首先要编译安装nginx
前面已经写过编译安装nginx了,如需编译请参考:编译安装Nginx
2.3、配置Nginx降权启动
1.创建普通用户(这里用www用户)
# useradd www #创建用户
# echo "www_password" |passwd --stdint www #设置密码
# su - www #切换至普通用户
2.使用www用户创建nginx程序目录
$ mkdir nginx/{conf,logs,www,sbin} #www用户创建降权后的nginx目录
# cp /usr/local/nginx/conf/mime.types /home/www/nginx/conf #另开一个窗口,使用root用户copy配置文件中网页支持类型文件
# cp /usr/local/nginx/conf/nginx.conf /home/www/nginx/conf #另开一个窗口,使用root用户拷贝nginx配置文件
# ln -s /home/www/nginx/sbin/nginx /usr/local/nginx/sbin/nginx #建软连接
# chown -R www.www /home/www/nginx #设置权限
3.修改nginx配置文件
$ cat /home/www/nginx/conf/nginx.conf
worker_processes auto;
worker_rlimit_nofile 65535;
error_log /home/www/nginx/logs/error.log;
pid /home/www/nginx/logs/nginx.pid;
events {
use epoll;
worker_connections 10240;
}
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"';
server {
listen 8080;
server_name localhost;
access_log /home/www/nginx/logs/web_blog_access.log main;
root /home/www/nginx/www;
location / {
index index.php index.html index.htm;
}
location ~.*\.(php|php5)?$ {
root /home/www/nginx/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.html;
include fastcgi.conf;
}
}
}
4.使用指定配置文件的方式启动nginx
$ /home/www/nginx/sbin/nginx -c /home/www/nginx/conf/nginx.conf -t #检查语法
$ /home/www/nginx/sbin/nginx -c /home/www/nginx/conf/nginx.conf #启动
三、解决非80对外访问的问题
刚刚说的使用非root用户启动nginx不可以使用80和443端口,可以通过前面加上负载均衡或者七牛云的CDN(支持端口映射)来实现对外的访问。