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

在linux安装nginx

1:将下载好的nginx-1.21.6.tar.gz拉到linux系在这里插入代码片统目录下的opt/下(目录更加自己想放的位置定义)

2:通过命令在当前目录解压nginx-1.21.6.tar.gz,解压成功后将文件夹名字改为redis

tar -zxvf nginx-1.21.6.tar.gz`在这里插入代码片`

4:执行(为了避免执行配置文件报错)

yum -y install gcc gcc-c++ autoconf automake make

5:执行(为了避免执行配置文件报错)

yum -y install openssl openssl-devel

6:配置基本信息

#配置configure --prefix 代表安装的路径,--with-http_ssl_module 安装ssl,--with-http_stub_status_module查看nginx的客户端状态
./configure --prefix=/opt/nginx --with-http_ssl_module --with-http_stub_status_module

7:进行编译安装

#编译安装nginx
make & make install 

8:在nginx目录下进入sbin目录下,执行启动命令

./nginx

9:在nginx文件下,编写重启脚本,保存为sh文件

#/bin/bash
cd sbin
./nginx -s reload

10:在nginx文件下,编写启动脚本,保存为sh文件

#/bin/bash
cd sbin
./nginx

如果启动提示没有logs目录下的error.log和access.log文件,就需要在nginx下创建logs目录和error.log与access.log文件

11:在nginx文件下,编写停止脚本,保存为sh文件

#/bin/bash
cd sbin
./nginx -s stop

12:修改nginx里conf下的nginx.cnf文件,配置ssl证书,启动https


#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  域名;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        root   /opt/app/dist;         
        location / {
             try_files $uri $uri/ @router;
             index  index.html;
         }
        location @router{
            rewrite ^.*$ /index.html last;
         }

        #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 /api {
            proxy_pass   http://127.0.0.1:8080/luhe/sys;
        }
	
	location /lxminiapp {
            proxy_pass   http://127.0.0.1:8080/luhe/sys;
        }


        # 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;
    #    }
    #}
    
    server {
        listen       443 ssl;
        server_name  域名;

        ssl_certificate      /opt/app/nginx/nginx/ssl/www.gdhdx.com.pem;
        ssl_certificate_key  /opt/app/nginx/nginx/ssl/www.gdhdx.com.key;
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
        
        root   /opt/app/dist;         
        location / {
             try_files $uri $uri/ @router;
             index  index.html;
         }
        location @router{
            rewrite ^.*$ /index.html last;
         }

        location /api {
            proxy_pass   http://127.0.0.1:8080/luhe/sys;
        }
	
	location /lxminiapp {
            proxy_pass   http://127.0.0.1:8080/luhe/sys;
        }
    }

}

13:设置开机自启配置文件

创建/etc/init.d/nginx文件,复制如下内容(到文件中,修改nginx=“/usr/local/nginx/sbin/nginx” 指向你的nginx启动文件路径,NGINX_CONF_FILE=“/usr/local/nginx/conf/nginx.conf” 指向你的配置文件路径

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description:  NGINX is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

#nginx启动文件路径
nginx="/opt/nginx/sbin/nginx"  

prog=$(basename $nginx)

#nginx.conf配置文件路径
NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -z "`grep $user /etc/passwd`" ]; then
       useradd -M -s /bin/nologin $user
   fi
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
restart() {
    configtest || return $?
    stop
    sleep 1
    start
}
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
force_reload() {
    restart
}
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
    status $prog
}
rh_status_q() {
    rh_status >/dev/null 2>&1
}
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

14:保存成功后给nginx文件赋予操作权限:

chmod a+x /etc/init.d/nginx

15:使用chkconfig命令: 将其加入管理列表

chkconfig --add /etc/init.d/nginx 

16:设置开机启动

chkconfig nginx on

service nginx start //启动nginx
service nginx stop //停止nginx

相关文章:

  • 记事本做网站背景/百度广告竞价
  • 外贸网站建设便宜/百度网页排名怎么提升
  • 学做奶油的网站/福州seo
  • 重庆铜梁网站建设报价/现在有什么技能培训班
  • 不同网站建设特点/济南百度推广公司电话
  • 为什么做的网站有的有弹窗有的没有/北京外贸网站优化
  • 《Buildozer打包实战指南》第三节 安装Buildozer打包所需的依赖文件
  • Retrofit源码分析实践(二)【Retrofit 免费的api测试工具引入】
  • Cadence PCB仿真使用Allegro PCB SI仿真参数单位配置与说明图文教程
  • 《和声学教程》学习笔记(四):属七和弦及其转位
  • Kubernetes:如何实现跨集群节点均匀调度分布Pod(Pod拓扑分布约束)
  • 3DMAX砖墙神器WallBuilder:快速生成常见砖墙插件教程
  • 郭天祥十天入门单片机学习笔记
  • 计算机网络必会:面向连接,无连接,可靠与不可靠
  • vue实现购物车思想
  • 用故事来传递商业价值
  • PTA 1095 解码PAT准考证 (C语言实现)
  • 二十九、Kubernetes中CronJob(CJ)详解