nginx安装部署

本文详细介绍了如何手动安装nginx 1.18.0,包括下载安装包、解压、编译以及安装依赖库pcre、openssl和zlib。在编译过程中可能遇到的问题,如缺少依赖、配置错误等,也提供了解决方案,例如安装gcc-c++环境和修改端口绑定。最后,通过curl检查nginx服务器是否正常运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

nginx安装部署

1. 首先下载安装包(此次安装使用的是1.18版本)

nginx-1.18.0.tar.gz

2. 解压缩nginx

tar -zxvf nginx-1.18.0.tar.gz

3. 进入nginx-1.18目录进行编译

cd nginx-1.18
./configure
此时报错:./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre= option.
(需要安装依赖的pcre、openssl、zlib库)

4. 安装pcre、openssl、zlib库(有yum的可以直接使用yum安装)

yum -y install pcre
yum -y install openssl
yum -y install zlib
(无法使用yum的情况,需要下载安装包)
pcre-8.44.tar.gz
tar zxvf pcre-8.44.tar.gz
tar zxvf openssl-1.1.1l.tar.gz
tar zxvf zlib-1.2.11.tar.gz

5. 编译nginx安装软件(制定安装目录和依赖包位置)

cd nginx-1.18
./configure --prefix=/nginx/nginx --with-openssl=/nginx/openssl-1.1.1l --with-pcre=/nginx/pcre-8.44 --with-zlib=/nginx/zlib-1.2.11
make && make install
*报错:configure: error: Invalid C++ compiler or C++ compiler flags
这是因为系统缺失 gcc-c++ 库,需要使用root安装gcc-c++环境:
yum -y install gcc-c++
yum install -y gcc
完成后再次执行make就可以了

6.启动nginx

cd /nginx/nginx/sbin
./nginx
报错:nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)
以非root权限启动时,会出现 nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied) 错误,将 /usr/local/nginx/conf/nginx.conf 文件中的80端口改为1024以上(8080)

7. 测试nginx网页访问是否正常

curl http://ip:8080

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>

### 安装和配置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、付费专栏及课程。

余额充值