Ubuntu 搭建文件服务器(Nginx)

1,下载Nginx

2,安装Nginx

3,Nginx指令及脚本使用

4,配置Nginx

1,下载Nginx

①去官网下载对应的Nginx版本

nginx: download

②直接在ubuntu使用指令下载

wget http://nginx.org/download/nginx-1.23.3.tar.gz

2,编译和软件安装

解压

tar zxvf nginx-1.23.3.tar.gz

编译安装

cd nginx-1.23.3

配置Nginx安装位置以及ssl证书

./configure --prefix=/usr/nginx --with-http_ssl_module

编译与安装

make

make install 

3,Nginx的指令,开启,查询和关闭等

①验证:(cd 安装位置)

cd /usr/nginx/sbin

./nginx -t

 如图所示说明配置成功

打开:

sudo ./nginx

 ②查看Nginx服务的当前情况:

ps -ef | grep nginx

 ③关闭Nginx服务:

根据查找到的pid进行关闭;即

普通关闭指令:

kill -15 进程号

强制关闭指令:

kill -9 进程号

根据上图查找到的pid为6211,使用普通关闭方法

kill -15 6211

④重新加载配置文件

cd /usr/nginx/sbin

./nginx -s reload

⑤重启Nginx服务

方法一:

cd /usr/nginx/sbin

./nginx -s reopen

方法二:

kill -1 进程号

⑥Nginx版本查看

cd /usr/nginx/sbin

./nginx -v

4,编写Nginx的系统脚本

在ubuntu里面,在/etc/init.d文件夹里面有许多系统服务的启动和停止脚本。Nginx主要参照它们的脚本搞就好了。

cd /etc/init.d

touch nginx

将下边的代码全部写入nginx里面

#!/bin/sh

### BEGIN INIT INFO
# Provides:     nginx
# Required-Start:
# Required-Stop:
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description: nginx
# Description: nginx server
### END INIT INFO


PROGRAM=/usr/nginx/sbin/nginx
PID=/usr/nginx/logs/nginx.pid


case "$1" in
  start)
     echo "Starting Nginx server"
     $PROGRAM && echo "SUCCESS" #开启Nginx
   ;;
  stop)
     PIDTXT=`cat $PID`
     echo "Stopping Nginx server"
     if [ ! -z "$PIDTXT" ]; then
        kill -15 $PIDTXT
     fi
     echo "SUCCESS"
   ;;
  reload)
     echo "Reload nginx server"
     $PROGRAM -s reload
     echo "SUCCESS"
   ;;
  status)
     if [! -f "$PID"];then
         echo "Nginx is not running..."
      else
         echo "Nginx is running..."
      fi
   ;;
   *)
     echo "Usage: service nginx {start|stop|reload|status}"
     exit 1
   ;;
esac

exit 0

 调整nginx权限:

chmod +x nginx

!!!记得不要直接从外边copy进去,不然会出现用户名不对而导致的错误。

 使用脚本后

开启:

service nginx start

关闭:

service nginx stop

重新加载:

service nginx reload

到这里,nginx安装部署基本完成。

4,配置Nginx

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        # server_name  localhost;
	    server_name _;	

        charset utf-8; # 中文名的文件不乱码
	
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /home/xxxx/Desktop/xxxx/file;  # 文件所放的路径
            index  index.html index.htm;
	        autoindex on;       # 显示目录
	        autoindex_exact_size on;    # 显示文件大小
            autoindex_localtime on;     # 显示文件时间
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #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_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

主要修改root 文件所放的路径,还有文本显示属性之类的

配置完之后输入ubuntu的ip地址得到结果如下:

Ubuntu 搭建文件服务器(Nginx)_第1张图片

你可能感兴趣的:(脚本,ubuntu,服务器,nginx)