以非root权限安装nginx及运行

本章主要讲如何在无root权限(包含无sudo权限)条件下于centos命令行中安装nginx以及在大于1024的端口(这里用8080)上运行。

1. 安装

两种方式,一是下载预编译好的rpm包安装,二是下载源码后自己编译。

如果是通过下载rpm方式安装,首先要找对应centos版本的rpm包。

通过命令

cat /etc/*release*

的输出可以看到centos系统的版本,在我能访问的某个云主机上显示如下:

 

使用curl -G <URL>命令来浏览网址http://nginx.org/packages/centos/$ver的内容,其中$ver为centos版本值为5或6或7(其实这个也可通过浏览网址http://nginx.org/packages/centos/)查看到,6.x版本对应是6,故浏览http://nginx.org/packages/centos/6,有如下输出:

 

找到对应cpu架构,64位cpu为x86_64,因此浏览http://nginx.org/packages/centos/6/x86_64/,进入RPMS目录(即浏览http://nginx.org/packages/centos/6/x86_64/RPMS/),查看其中的超链接,如:<a href=“nginx-1.8.1-1.el6.ngx.x86_64.rpm”>nginx-1.8.1-1.el6.ngx.x86_64.rpm</a>,选择其中的版本(这里是版本1.8.1),根据超链(href="...")使用wget下载:

wget http://nginx.org/packages/centos/6/x86_64/RPMS/nginx-1.8.1-1.el6.ngx.x86_64.rpm

完成后在当前目录有对应rpm文件,但是不能直接安装改rpm,因为权限限制,因此我们不安装,而是提取出其中的二进制(及其他必要文件),通过以下命令实现:

rpm2cpio ctags-5.8-2.el6.x86_64.rpm |cpio -idvm

此时当前文件夹下多出etc、usr等文件夹,其中usr/sbin/nginx就是nginx的二进制文件,可选择性地将其移动到~/bin/目录下。

至此nginx的二进制文件已有,算是”安装“上了。

2. 配置及运行

nginx根据默认配置会导致读写需要root权限的文件和目录,因此需要提供自定义配置文件。新建一个nginx.conf文件,填入以下内容:

# Usage: nginx -c /path/to/this/nginx.conf

error_log /tmp/error.log;

pid /tmp/nginx.pid;

worker_processes 1;

events {

  worker_connections 1024;

}

 

http {

  # Set an array of temp and cache file options that will otherwise default to

  # restricted locations accessible only to root.

  client_body_temp_path /tmp/client_body;

  fastcgi_temp_path /tmp/fastcgi_temp;

  proxy_temp_path /tmp/proxy_temp;

  scgi_temp_path /tmp/scgi_temp;

  uwsgi_temp_path /tmp/uwsgi_temp;

 

  tcp_nopush on;

  tcp_nodelay on;

  keepalive_timeout 65;

  types_hash_max_size 2048;

 

  #include /etc/nginx/mime.types;

  index index.html index.htm index.php;

 

  log_format   main '$remote_addr - $remote_user [$time_local] $status '

    '"$request" $body_bytes_sent "$http_referer" '

    '"$http_user_agent" "$http_x_forwarded_for"';

 

  default_type application/octet-stream;

 

  server {

    # IPv4.

    listen 8080;

    # IPv6.

    #listen [::]:8080 default ipv6only=on;

 

    root /path/to/html/root;

 

    access_log /tmp/access.log;

    error_log /tmp/error.log;

 

    location / {

      # First attempt to serve request as file, then as directory, then fall

      # back to index.html.

      try_files $uri $uri/ /index.html;

    }

  }

}

 

我们需要告诉nginx我们的html(web应用的根目录)放在哪里,通过修改文件结构中http块下server块中的root实现(即上例文件中的root /path/to/html/root; 行,注意行末有分号),这里最好配置为绝对路径。nginx在软件包中提供了一个很简单的html页面,在解压出中的目录usr/share/html中,可以使用这个html目录测试。配置文件中的大部分值为路径,我们的目的就是要将覆盖默认路径,改到我们有权限读写的路径中(上示例文件中大部分配置到/tmp目录去了)。

通过命令行参数告知nginx我们的配置文件(而非使用默认的配置文件):

 

nginx -c /home/xxxx/nginx.conf #配置文件的绝对路径

 

nginx有可能仍会提示无权限打开/var/log/nginx/error.log,无视之,可使用pgrep -a nginx看到nginx已经运行。通过curl <本机ip或localhost>:8080看到nginx自己做的欢迎页面,我这里的显示如下:

 

至此,nginx在无root权限下成功运行。

 

通过下载源码编译安装方式,一定要记得配置前缀路径,否则会因无权限写入/usr, /etc等默认路径下的文件而失败,编译后的目录结构上面由rpm包解压出的结构类似,配置及运行的过程一样。

posted @ 2016-09-03 13:06  二球悬铃木  阅读(16905)  评论(0编辑  收藏  举报