当前位置: 首页 > news >正文

Nginx文件服务器搭建

1、安装依赖、关闭防火墙
[root@localhost ~]# yum install wget gcc gcc-c++ pcre pcre-devel openssl openssl-devel zlib zlib-devel
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld

2、创建nginx启动用户
注意:会在/home目录生成对应www的用户目录
[root@localhost ~]# /usr/sbin/groupadd -f www ---建立用户组 www
[root@localhost ~]# /usr/sbin/useradd -g www www ---添加用户 www

创建文件存放目录
[root@localhost ~]# mkdir /pages

3、下载nginx
[root@localhost ~]# cd /usr/local/src
[root@localhost ~]# wget http://nginx.org/download/nginx-1.14.0.tar.gz

4、解压并编译安装
[root@localhost ~]# tar zxf nginx-1.14.0.tar.gz
[root@localhost ~]# cd nginx-1.14.0
[root@localhost ~]# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module
[root@localhost ~]# make
[root@localhost ~]# make install

5、设置nginx软连接
[root@localhost ~]# ln -sv /usr/local/nginx/sbin/nginx /usr/local/sbin/

6、配置nginx服务并设置开机自启
设置service启动的方式
[root@localhost ~]# vim /etc/init.d/nginx

#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFOPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid
if [ -s /bin/ss ]; thenStatBin=/bin/ss
elseStatBin=/bin/netstat
ficase "$1" instart)echo -n "Starting $NAME... "if $StatBin -tnpl | grep -q nginx;thenecho "$NAME (pid `pidof $NAME`) already running."exit 1fi$NGINX_BIN -c $CONFIGFILEif [ "$?" != 0 ] ; thenecho " failed"exit 1elseecho " done"fi;;stop)echo -n "Stoping $NAME... "if ! $StatBin -tnpl | grep -q nginx; thenecho "$NAME is not running."exit 1fi$NGINX_BIN -s stopif [ "$?" != 0 ] ; thenecho " failed. Use force-quit"exit 1elseecho " done"fi;;status)if $StatBin -tnpl | grep -q nginx; thenPID=`pidof nginx`echo "$NAME (pid $PID) is running..."elseecho "$NAME is stopped."exit 0fi;;force-quit|kill)echo -n "Terminating $NAME... "if ! $StatBin -tnpl | grep -q nginx; thenecho "$NAME is is stopped."exit 1fikill `pidof $NAME`if [ "$?" != 0 ] ; thenecho " failed"exit 1elseecho " done"fi;;restart)$0 stopsleep 1$0 start;;reload)echo -n "Reload service $NAME... "if $StatBin -tnpl | grep -q nginx; then$NGINX_BIN -s reloadecho " done"elseecho "$NAME is not running, can't reload."exit 1fi;;configtest)echo -n "Test $NAME configure files... "$NGINX_BIN -t;;*)echo "Usage: $0 {start|stop|restart|reload|status|configtest|force-quit|kill}"exit 1;;esac

 [root@localhost ~]# chmod +x /etc/init.d/nginx
设置开机自启动
[root@localhost ~]# chkconfig --add nginx
[root@localhost ~]# chkconfig nginx on

 7、启动nginx
[root@localhost ~]# /etc/init.d/nginx start 启动
[root@localhost ~]# service nginx start 启动
[root@localhost ~]# service nginx stop 停止
[root@localhost ~]# service nginx restart 重启
[root@localhost ~]# service nginx status 查看nginx状态

8、访问
访问http://IP即可看到nginx欢迎页,默认欢迎页面在/usr/local/nginx/html目录下

9、修改nginx.conf配置文件
将下面的全部复制到nginx.conf
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

#user  nobody;
worker_processes  1;events {worker_connections  1024;
}http {include       mime.types;default_type  application/octet-stream;sendfile        on;keepalive_timeout  65;server {listen       80;server_name  localhost;charset utf-8;location / {root   html;index  index.html indexcharset utf-8;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}location /pages {alias /pages;autoindex on;               # 开启索引功能,以便可以看到文件列表autoindex_exact_size off;   # 显示文件大小autoindex_localtime on;     # 显示文件时间}}}

修改完配置后,保存退出,重启Nginx:service nginx restart

11、创建文件测下
[root@localhost ~]# cd /pages/
[root@localhost ~]# touch 1.txt
访问http://IP/pages

 


http://www.mrgr.cn/news/24516.html

相关文章:

  • 51. 数组中的逆序对
  • 2024最新盘点,主流生产报工软件有哪些?
  • 基于CICD的Nginx灰度发布与节点自动上下线管理
  • 平板电脑开发软件思路——客户现场编译—SAAS本地化及未来之窗行业应用跨平台架构
  • flink中chainWith() 的详解
  • CSS3 var() 函数:解锁动态样式与高效维护的密钥
  • 一分钟发布月考成绩,教师助力工具!
  • 【SQL】百题计划:SQL对于空值的比较判断。
  • 光学变焦和数字变倍的区别,看完这篇文你就明白了!!!
  • Redis面试题整理
  • oneclick 命令:快速筛选控制变量的利器
  • 计算机视觉学习路线
  • 海康IPC摄像头通过国标28181方式接入带域名的视频监控接入平台,视频通道无法上传到视频监控平台,导致无法获取视频资源的问题解决
  • PyTorch深度学习快速入门【小土堆】
  • Redis学习Day3——项目工程开发`
  • 信息安全管理工程师
  • 什么是三轴齿轮箱
  • mysql 之 information_schema
  • 动态数字时钟屏保 提升桌面美化 电脑屏幕屏保软件
  • 上海徐汇区开启大模型备案奖励申报