鲲鹏麒麟安装Nginx

本文详细描述了如何在离线环境下麒麟操作系统鲲鹏服务器上下载Nginx源码,配置mime.types和nginx.conf,以及创建install_nginx目录和相关脚本,以手动编译并安装Nginx,最后验证安装并设置防火墙规则以供外网访问。
摘要由CSDN通过智能技术生成

因项目需求需要在安装麒麟操作系统的鲲鹏服务器上安装Nginx,服务器没有可用的yum源也没有连接互联网,因此需要下载nginx源码进行实时编译。以下提及的完整shell脚本(点击下载)

下载源码

可在Nginx官网下载,下载地址:

https://nginx.org/en/download.html

准备目录

install_nginx/

     /thirdtools

        nginx-1.22.1.tar.gz

     /thirdtools-conf

        mime.types

        nginx.conf

        nginx.service

     install_nginx.sh

     uninstall_nginx.sh

安装

mime.types内容如下:


types {
    text/html                                        html htm shtml;
    text/css                                         css;
    text/xml                                         xml;
    image/gif                                        gif;
    image/jpeg                                       jpeg jpg;
    application/javascript                           js;
    application/atom+xml                             atom;
    application/rss+xml                              rss;

    text/mathml                                      mml;
    text/plain                                       txt;
    text/vnd.sun.j2me.app-descriptor                 jad;
    text/vnd.wap.wml                                 wml;
    text/x-component                                 htc;

    image/png                                        png;
    image/svg+xml                                    svg svgz;
    image/tiff                                       tif tiff;
    image/vnd.wap.wbmp                               wbmp;
    image/webp                                       webp;
    image/x-icon                                     ico;
    image/x-jng                                      jng;
    image/x-ms-bmp                                   bmp;

    font/woff                                        woff;
    font/woff2                                       woff2;

    application/java-archive                         jar war ear;
    application/json                                 json;
    application/mac-binhex40                         hqx;
    application/msword                               doc;
    application/pdf                                  pdf;
    application/postscript                           ps eps ai;
    application/rtf                                  rtf;
    application/vnd.apple.mpegurl                    m3u8;
    application/vnd.google-earth.kml+xml             kml;
    application/vnd.google-earth.kmz                 kmz;
    application/vnd.ms-excel                         xls;
    application/vnd.ms-fontobject                    eot;
    application/vnd.ms-powerpoint                    ppt;
    application/vnd.oasis.opendocument.graphics      odg;
    application/vnd.oasis.opendocument.presentation  odp;
    application/vnd.oasis.opendocument.spreadsheet   ods;
    application/vnd.oasis.opendocument.text          odt;
    application/vnd.openxmlformats-officedocument.presentationml.presentation
                                                     pptx;
    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
                                                     xlsx;
    application/vnd.openxmlformats-officedocument.wordprocessingml.document
                                                     docx;
    application/vnd.wap.wmlc                         wmlc;
    application/x-7z-compressed                      7z;
    application/x-cocoa                              cco;
    application/x-java-archive-diff                  jardiff;
    application/x-java-jnlp-file                     jnlp;
    application/x-makeself                           run;
    application/x-perl                               pl pm;
    application/x-pilot                              prc pdb;
    application/x-rar-compressed                     rar;
    application/x-redhat-package-manager             rpm;
    application/x-sea                                sea;
    application/x-shockwave-flash                    swf;
    application/x-stuffit                            sit;
    application/x-tcl                                tcl tk;
    application/x-x509-ca-cert                       der pem crt;
    application/x-xpinstall                          xpi;
    application/xhtml+xml                            xhtml;
    application/xspf+xml                             xspf;
    application/zip                                  zip;

    application/octet-stream                         bin exe dll;
    application/octet-stream                         deb;
    application/octet-stream                         dmg;
    application/octet-stream                         iso img;
    application/octet-stream                         msi msp msm;

    audio/midi                                       mid midi kar;
    audio/mpeg                                       mp3;
    audio/ogg                                        ogg;
    audio/x-m4a                                      m4a;
    audio/x-realaudio                                ra;

    video/3gpp                                       3gpp 3gp;
    video/mp2t                                       ts;
    video/mp4                                        mp4;
    video/mpeg                                       mpeg mpg;
    video/quicktime                                  mov;
    video/webm                                       webm;
    video/x-flv                                      flv;
    video/x-m4v                                      m4v;
    video/x-mng                                      mng;
    video/x-ms-asf                                   asx asf;
    video/x-ms-wmv                                   wmv;
    video/x-msvideo                                  avi;
}

nginx.conf内容如下


#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_tokens off;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        
        location /demo {
            proxy_pass http://127.0.0.1:8600/demo;
            proxy_http_version 1.1;
            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }

        #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       8080;
        server_name  localhost;

        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;
    #    }
    #}

}

nginx.service内容如下:

[Unit]
Description=nginx
After=network.target
  
[Service]
Type=forking
ExecStart=/data/public/nginx/sbin/nginx
ExecReload=/data/public/nginx/sbin/nginx -s reload
ExecStop=/data/public/nginx/sbin/nginx -s quit
PrivateTmp=true
  
[Install]
WantedBy=multi-user.target

install_nginx.sh内容如下:

#!/bin/sh

# 获取当前路径,如:/data/public/install 
basepath=$(cd `dirname $0`; pwd)

# 加载配置项
source ./config
path=$(echo $path | sed 's/\r//')
pathpublic=$(echo $pathpublic | sed 's/\r//')
installpath=$pathpublic
nginx_dir=$installpath/nginx

if [ ! -d "$nginx_dir" ]; then
    mkdir -p $nginx_dir
fi

nginx_file=$(find `dirname ./` -name *nginx*.tar.gz)

if [ ! -e "${nginx_file}" ];then
    echo "Not find nginx install file."
    exit 0
fi
tar zxvf $nginx_file -C $nginx_dir

if [ $? -ne 0 ];then
    exit 0
fi

wj_file=$(ls $nginx_dir/ -l| awk '/^d/{print $NF}')
wj_new_file=${nginx_dir}/${wj_file}

# 修改banner
sed -i 's/^static u_char ngx_http_server_string.*/static u_char ngx_http_server_string[] = "Server: unknown" CRLF;/g' $wj_new_file/src/http/ngx_http_header_filter_module.c
sed -i 's/^static u_char ngx_http_server_full_string.*/static u_char ngx_http_server_full_string[] = "Server: unknown" CRLF;/g' $wj_new_file/src/http/ngx_http_header_filter_module.c
sed -i 's/^static u_char ngx_http_server_build_string.*/static u_char ngx_http_server_build_string[] = "Server: unknown" CRLF;/g' $wj_new_file/src/http/ngx_http_header_filter_module.c
sed -i 's:^"<hr><center>" NGINX_VER "</center>" CRLF:"<hr><center>WebServer</center>" CRLF:g' $wj_new_file/src/http/ngx_http_special_response.c
sed -i 's:^"<hr><center>" NGINX_VER_BUILD "</center>" CRLF:"<hr><center>WebServer</center>" CRLF:g' $wj_new_file/src/http/ngx_http_special_response.c
sed -i 's:^"<hr><center>nginx</center>" CRLF:"<hr><center>WebServer</center>" CRLF:g' $wj_new_file/src/http/ngx_http_special_response.c
sed -i 's:^<p><em>Faithfully yours, nginx.</em></p>:<p><em>Faithfully yours.</em></p>:g' $wj_new_file/html/50x.html
# 删除首页
rm -rf $wj_new_file/html/index.html

cd $wj_new_file

./configure --prefix=$nginx_dir --with-http_stub_status_module --with-http_gzip_static_module --with-http_ssl_module
make -j4 && make install

$nginx_dir/sbin/nginx -t

# 拷贝文件
cp $basepath/thirdtools-conf/nginx.conf $nginx_dir/conf/nginx.conf
processor=`cat /proc/cpuinfo | grep "processor" | wc -l`
#sed -i "s/^w.*;$/worker_processes ${processor};/g" $nginx_dir/conf/nginx.conf

echo "拷贝服务配置文件"
echo "wj_file=${wj_file}"
echo "wj_new_file=${wj_new_file}"

cd $basepath
cp ./thirdtools-conf/nginx.service /lib/systemd/system/
# 修改配置
sed -i 's:ExecStart=.*:ExecStart='$nginx_dir'/sbin/nginx:g' /lib/systemd/system/nginx.service
sed -i 's:ExecReload=.*:ExecReload='$nginx_dir'/sbin/nginx -s reload:g' /lib/systemd/system/nginx.service
sed -i 's:ExecStop=.*:ExecStop='$nginx_dir'/sbin/nginx -s quit:g' /lib/systemd/system/nginx.service

echo "重新加载配置"
systemctl daemon-reload
sleep 1
echo "启用Nginx服务"
systemctl enable nginx.service
sleep 1
echo "启动Nginx服务"
systemctl start nginx.service
sleep 1
echo "启用Nginx服务"
systemctl enable nginx.service

uninstall_nginx.sh内容如下:

#!/bin/sh

# 获取当前路径,如:/data/public/install 
basepath=$(cd `dirname $0`; pwd)

# 加载配置项
source ./config
path=$(echo $path | sed 's/\r//')
pathpublic=$(echo $pathpublic | sed 's/\r//')
installpath=$pathpublic
nginx_dir=$installpath/nginx

systemctl stop nginx.service
sleep 1
systemctl disable nginx.service
sleep 1

# 删除文件
rm -rf $nginx_dir
rm -rf /lib/systemd/system/nginx.service
rm -rf /usr/lib/systemd/system/nginx.service
rm -rf /etc/systemd/system/nginx.service

for file in `find / -name nginx.service`
do
    if [[ $file =~ $basepath ]]; then
        echo 'install file skip '$file
    else 
        rm -rf $file
        echo 'delete file '$file
    fi
done

exit

exit

config配置文件内容如下:

path=/data
pathpublic=/data/public

将上面这些文件上传到鲲鹏服务器。然后进入到install_nginx目录,给文件授权

chmod 777 ./*.sh

然后执行./install_nginx.sh,出现如下内容说明安装成功

……
make[1]: Leaving directory '/data/public/nginx/nginx-1.22.1'
nginx: the configuration file /data/public/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /data/public/nginx/conf/nginx.conf test is successful
拷贝服务配置文件
wj_file=nginx-1.22.1
wj_new_file=/data/public/nginx/nginx-1.22.1
重新加载配置
启用Nginx服务
启动Nginx服务
启用Nginx服务

验证

此时访问curl http://localhost得如下内容说明已经安装成功

[root@test install_nginx]# curl http://localhost
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>WebServer</center>
</body>
</html>

如果需要外网访问,可以使用如下命令

firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload

配置SSL

    server {
        listen       8081 ssl;
        server_name test.com;

        ssl_certificate cert/test.com.pem;
        ssl_certificate_key cert/test.com.key;
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers on;
    }

配置404/500页面

server节点中增加如下配置

        error_page  404              /404.html;
        location = /404.html {
            root   html;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

404.html页面放到html目录下,内容如下:

<!DOCTYPE html>
<html>
<head>
<title>Error-404</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>404 Not Found</h1>
</body>
</html>

去掉Nginx相关的标识,增强系统的安全性

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

angushine

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值