Nginx安装

本文详细介绍了在Ubuntu和CentOS环境下安装Nginx的过程,包括编译环境的准备、依赖库的安装、Nginx的下载、编译、配置及启动步骤。通过本文,读者可以了解如何从源码安装Nginx,并进行基本的配置。

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

编译环境准备

ubuntu
apt-get install build-essential
apt-get install libtool
CentOS
#安装make
yum -y install gcc automake autoconf libtool make
#安装g++
yum install gcc gcc-c++
#安装PCRE库
yum install -y pcre pcre-devel
#安装zlib库
yum install -y zlib zlib-devel
##也可一步完成
yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel

开始安装

 cd /usr/local/nginx
 wget http://nginx.org/download/nginx-1.4.2.tar.gz
 tar -zxvf nginx-1.4.2.tar.gz
 cd nginx-1.4.2
 ./configure --prefix=/usr/local/nginx/nginx-1.4.2 \
 --conf-path=/usr/local/nginx/nginx.conf \
 --pid-path=/usr/local/nginx/nginx.pid
make
make install
安装成功后 /usr/local/nginx 目录下如下
[root@localhost nginx]# ls
fastcgi.conf          fastcgi_params          koi-utf  mime.types          nginx-1.4.2  nginx.conf.default  scgi_params.default  uwsgi_params.default
fastcgi.conf.default  fastcgi_params.default  koi-win  mime.types.default  nginx.conf   scgi_params         uwsgi_params         win-utf
启动Nginx

确保系统的 80 端口没被其他程序占用,运行/usr/local/nginx/nginx-1.4.2/sbin/nginx命令来启动 Nginx

cd /usr/local/nginx/nginx-1.4.2/sbin
./nginx -c /usr/local/nginx/conf/nginx.conf
#也可以直接启动
./nginx
查看是否启动完成
查看进程
[root@localhost sbin]# ps -ef|grep nginx                    
root       1343      1  0 20:09 ?        00:00:00 nginx: master process ./nginx
nobody     1344   1343  0 20:09 ?        00:00:00 nginx: worker process
root       1346   1325  0 20:09 pts/0    00:00:00 grep --color=auto nginx
本机访问
[root@localhost sbin]# curl localhost
<!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 安装方法 #### Ubuntu 和 Debian 系统 对于基于 Debian 的 Linux 发行版,可以使用 APT 包管理器来安装 Nginx。 ```bash sudo apt update sudo apt install nginx ``` 这会自动下载并安装最新稳定版本的 Nginx 及其依赖项[^1]。 #### CentOS, RHEL 或 Fedora 系统 在 Red Hat 类似发行版中,YUM 是默认的包管理系统。可以通过 YUM 来安装 Nginx: ```bash sudo yum install epel-release sudo yum install nginx ``` 上述命令先启用了 EPEL 仓库,因为官方源可能不包含最新的 Nginx 版本;接着通过 `yum` 下载并安装 Nginx[^4]。 #### 麒麟操作系统 (Kylin OS) 针对麒麟操作系统的环境,有特定的方式来进行 Nginx 的离线安装。首先需要获取到 Nginx 的 RPM 文件,之后再执行本地安装过程: ```bash yum -y install --downloadonly --downloaddir=/home/nginx_install nginx # 获取离线安装包 rpm -ivh /home/nginx_install/nginx*.rpm # 执行RPM安装 ``` 完成安装后,可通过如下方式启动服务以及验证是否正常工作: ```bash systemctl start nginx # 启动Nginx服务 curl http://localhost # 测试访问 ``` 另外,在银河麒麟系统中也可以直接进入解压后的目录并通过脚本来启动 Nginx 实例: ```bash cd nginx/sbin ./nginx # 使用自带脚本启动Nginx ``` 此时可以在浏览器里输入服务器 IP 地址来确认 Web 页面能否被正确展示出来[^3]。 #### macOS 环境下 Homebrew 方式 如果是在 Mac 上,则推荐利用 Homebrew 工具简化整个流程: ```bash brew install nginx # 利用Homebrew安装Nginx ``` 以上就是在多种主流平台上部署 Nginx 的基本指导说明。每种平台都有各自的特点和最佳实践方案可供选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值