在linux安装nginx

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

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

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: nginxlinux