Nginx的安装部署

一、nginx安装

依赖gcc :     yum -y install gcc gcc-c++ kernel-devel 

pcre-8.44.tar.gz、zlib-1.2.7.tar.gz,nginx依赖于这两个组件,首先安装他俩,

1、为方便直接把pcre-8.44.tar.gz上传到/usr/local/src/

cd /usr/local/src/

安装步骤如下:

   1.1、切换到root用户【su - root】;

   1.2、解压pcre-8.44.tar.gz【tar -zxvf pcre-8.44.tar.gz】;

   1.3、进入到解压后的目录pcre-8.44【cd pcre-8.44】,执行编译命令【./configure --prefix=/usr/local】;

   1.4、执行安装命令【make && make install】;

为方便直接把zlib-1.2.7.tar.gz上传到/usr/local/

cd /usr/local

   1.5、解压zlib-1.2.7.tar.gz【tar -zxvf zlib-1.2.7.tar.gz】;

   1.6、进入到解压后的目录pcre-8.44【cd zlib-1.2.7】,执行编译命令【./configure --prefix=/usr/local】;

   1.7、执行安装命令【make && make install】;

------------------------------------------------------------------------------------------

以上命令执行完成,准备工作就可以了!!!,接下来安装nginx

2、安装nginx

安装步骤如下:

   2.1、解压nginx-1.18.0.tar.gz【tar -zxvf nginx-1.18.0.tar.gz】;

   2.2、进入到解压后的目录nginx-1.18.0【cd nginx-1.18.0】,执行编译命令【./configure --prefix=/usr/local/nginx】;

   2.3、执行安装命令【make && make install】;

-------------------------------------------------------------------------------------------

这个时候nginx就算安装成功了,测试nginx是否安装成功如下:

   3.1、进入nginx启动目录【cd /usr/local/nginx/sbin】;

   3.2、启动nginx【./nginx】;

   3.3、通过浏览器访问【http://ip/】,如果页面上出现nginx欢迎的字样就说明成功了。

二、停止,重启nginx命令

cd /usr/local/nginx/sbin

停止 nginx 命令: ./nginx -s stop 

重启nginx命令:./nginx -s reload

检测nginx配置是否正确:./nginx -t

  1. 简单配置http

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;

    #gzip  on;

    server {

        client_max_body_size 1024m;

        listen       80;

        #server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            resolver_timeout 30s;

            proxy_pass http://192.168.80.180:8001;

            #root   html;

            #index  index.html index.htm;

        }

        access_log /usr/local/nginx/logs/httplogs/proxy-$host.log;

     }

        error_page   500 502 503 504  /50x.html;      

}

三、安装 ssl 模块,缺openssl 

一般情况下都是不存在ssl模块的,接下来进入到你的解压缩后的nginx目录,注意这里不是nginx安装目录,是解压缩后的目录,进入目录后,输入 

cd /usr/local/nginx-1.18.0   这里是解压缩的目录

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

报错

./configure: error: SSL modules require the OpenSSL library.

You can either do not enable the modules, or install the OpenSSL library

into the system, or build the OpenSSL library statically from the source

with nginx by using --with-openssl=<path> option.

安装 openssl

1、Yum安装openssl

yum -y install openssl openssl-devel  

2、CENTOS7,离线源码安装openssl

 openssl官网下载
当前的稳定版是 openssl-fips-2.0.16.tar.gz,下载后上传到服务器的/usr/local/src目录下。
解压压缩包

tar -xzf openssl-fips-2.0.16.tar.gz

然后进入openssl-fips-2.0.16目录,进行安装

./config shared zlib --prefix=/usr/local/openssl
make
make install

装完openssl,继续安装ssl

cd /usr/local/nginx-1.18.0

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module  

然后执行:

make

#切记不要执行make install,否则会重新安装nginx

查看版本,发现TLS已经安装成功

上述在解压目录make后,会在解压目录/usr/local/nginx-1.18.0下生成objs

将安装目录下的nginx文件备份

cd /usr/local/nginx/sbin

cp nginx nginx.http.bak.20220518

cp /usr/local/nginx-1.18.0/objs/nginx  /usr/local/nginx/sbin

cd /usr/local/nginx/sbin

./nginx –V 注意大写V,小写的只有简单版本号

nginx version: nginx/1.18.0

built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)

built with OpenSSL 1.0.2k-fips  26 Jan 2017

TLS SNI support enabled

configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

四、测试环境,在nginx创建开发版本证书

https://blog.csdn.net/PRIMEFJT/article/details/79123011

# cd /usr/local/nginx/conf

[root@localhost conf]# openssl genrsa -des3 -out server.key 1024

Generating RSA private key, 1024 bit long modulus

..................................++++++

.............................++++++

e is 65537 (0x10001)

Enter pass phrase for server.key: 1qaz2wsx

Verifying - Enter pass phrase for server.key: 1qaz2wsx

[root@localhost conf]# openssl req -new -key server.key -out server.csr

Enter pass phrase for server.key: 1qaz2wsx

You are about to be asked to enter information that will be incorporated

into your certificate request.

What you are about to enter is what is called a Distinguished Name or a DN.

There are quite a few fields but you can leave some blank

For some fields there will be a default value,

If you enter '.', the field will be left blank.

-----

Country Name (2 letter code) [XX]:CN

State or Province Name (full name) []:Beijing

Locality Name (eg, city) [Default City]:Beijing

Organization Name (eg, company) [Default Company Ltd]:zhonghe

Organizational Unit Name (eg, section) []:IT Dept

Common Name (eg, your name or your server's hostname) []:kjlink.com

Email Address []:直接回车

Please enter the following 'extra' attributes

to be sent with your certificate request

A challenge password []:直接回车

An optional company name []:直接回车

在加载SSL支持的Nginx并使用上述私钥时除去必须的口令:

$ cp server.key server.key.org

# openssl rsa -in server.key.org -out server.key

Enter pass phrase for server.key.org: 1qaz2wsx

writing RSA key

$ openssl rsa -in server.key.org -out server.key

最后标记证书使用上述私钥和CSR:

$ openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt

Signature ok

subject=/C=CN/ST=Beijing/L=Beijing/O=zhonghe/OU=IT Dept/CN=kjlink.com

Getting Private key

### 安装和配置Nginx服务器 #### 下载并解压Nginx 为了在Windows操作系统上安装Nginx,首先需要下载适用于Windows的Nginx版本。可以从官方网站获取最新稳定版压缩包,并将其解压至期望的位置。 #### 编辑配置文件`nginx.conf` 进入Nginx目录下的`conf`文件夹,找到名为`nginx.conf`的配置文件[^1]。此文件包含了启动Nginx所需的各种设置选项。可以根据具体的应用场景调整参数,比如监听端口、静态资源路径等。 对于希望作为图片服务器的情况,在该配置文件内定义好存储图像的具体位置以及对应的URL访问方式[^2];而对于想要实现反向代理功能,则需利用`proxy_pass`指令来设定目标服务器地址[^3]。 ```nginx location / { proxy_pass http://example.com; } ``` 上述代码片段展示了如何使用`proxy_pass`将请求转发给另一台服务器。 #### 命令行操作指南 完成以上准备工作之后,可以通过命令提示符执行相应指令来进行服务管理: - 查看已安装Nginx版本信息:`nginx -v` 或者 `nginx -V`(带更多编译选项) - 启动Nginx服务:`start nginx` - 平滑重启加载新配置而不停机:`nginx -s reload` 当对配置进行了更改后,记得运行`nginx -t`检验语法正确性再做热更新以确保不会因为错误而导致整个站点不可用。 #### 测试部署成果 最后一步就是验证一切是否正常工作了。按照之前所设好的映射关系放置一些测试素材(如图片),然后尝试通过浏览器或其他HTTP客户端工具去访问这些资源,确认能够顺利展示出来即可认为初步搭建成功。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值