nginx 编译安装

158 阅读1分钟

安装shell脚本

#!/bin/bash
#第1步:下载nginx源码包
yum install wget -y
wget http://nginx.org/download/nginx-1.19.7.tar.gz
#第2步:安装依赖关系包
yum install gcc pcre-devel openssl-devel -y
#第3步:解压
tar xf nginx-1.19.7.tar.gz
#第4步:进入解压后的目录
cd /nginx/nginx-1.19.7
#第5步:配置
./configure --prefix=/usr/local/nginx --with-http_realip_module --with-http_ssl_module --with-pcre --with-http_stub_status_module
#第6步:编译然后再安装
yum install make -y
make && make install
#开机启动
#/usr/local/nginx/sbin/nginx
#echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.local
#chmod +x /etc/rc.d/rc.local
#添加环境变量
PATH=$PATH:/usr/local/nginx/sbin
echo "PATH=$PATH:/usr/local/nginx/sbin" >>/etc/bashrc
#添加防火墙规则
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload​

将Nginx设置为linux下的服务

1.进入目录/etc/init.d/   ,vim nginx 2.编辑nginx

#!/bin/sh
#chkconfig: 2345  85 15   
# 2345 表示在2345模式下,开机自启动
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
 
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
 
 
#source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
  [ '${NETWORKING}' = "no" ] && exit 0
  [ -x $nginxd ] || exit 0
 
 
# Start nginx daemons functions.
 start() {
    if [ -e $nginx_pid ];then
      echo "nginx already running...."
      exit 1
    fi
      echo -n $"Starting $prog: "
      daemon $nginxd -c ${nginx_config}
      RETVAL=$?
     echo
      [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
    return $RETVAL
}
# Stop nginx daemons functions.
stop() {
        echo -n $"Stopping $prog: "
        killproc $nginxd
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
    echo -n $"Reloading $prog: "
     #kill -HUP `cat ${nginx_pid}`
     killproc $nginxd -HUP
     RETVAL=$?
     echo
}
# See how we were called.
  case "$1" in
     start)
         start
         ;;
     stop)
         stop
         ;;
     reload)
         reload
         ;;
     restart)
         stop
         start
         ;;
     status)
         status $prog
         RETVAL=$?
         ;;
     *)
        echo $"Usage: $prog {start|stop|restart|reload|status|help}"
        exit 1
  esac
  exit $RETVAL

3.给脚本添加执行权限

chkconfig --add nginx
chkconfig nginx on

文件服务器功能配置

配置nginx.conf

#修改用户为root

user  root;
    
http{    
    autoindex on;# 显示目录
    autoindex_exact_size on;# 显示文件大小
    autoindex_localtime on;# 显示文件时间

	server {
		listen       80 default_server;
		listen       [::]:80 default_server;
		#root         /usr/share/nginx/html;
		
		location /files {
		    alias         /data/;
		    #alias指定的目录是准确的,即location匹配访问的path目录下的文件直接是在alias目录下查找的;
            #root指定的目录是location匹配访问的path目录的上一级目录,这个path目录一定要是真实存在root指定目录下的;
		}
	}
}