linux下安装nginx

本文详细介绍Nginx服务的安装过程,包括创建服务进程用户、下载与解压软件包、配置与编译安装等步骤,并提供了解决常见错误的方法。

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

1、添加nginx服务进程用户
# groupadd -r nginx
# useradd -r -g nginx nginx
2、下载

记得挑选自己喜欢的版本
http://nginx.org/download
可以在远程主机直接下载

# wget  http://nginx.org/download/nginx-1.9.9.tar.gz

也可以用该地址下载之后自己上传到远程主机

3、解压
# tar -zxvf nginx-1.9.9.tar.gz
4、部署

先进入到解压完成的nginx目录下

# cd nginx-1.9.9/

然后安装,最简单的安装是这样,没有添加第三方模块,不过一般都不够用
/usr/local/nginx是安装路径,可以根据需要自己修改

./configure --prefix=/usr/local/nginx

后面配置的时候会发现需要的好多模块都没有。
索性就把所有模块都安装了吧

# ./configure  --prefix=/usr/local/nginx  --sbin-path=/usr/sbin/nginx  --conf-path=/etc/nginx/nginx.conf  --error-log-path=/var/log/nginx/error.log  --http-log-path=/var/log/nginx/access.log  --pid-path=/var/run/nginx.pid  --lock-path=/var/run/nginx.lock  --http-client-body-temp-path=/var/tmp/nginx/client  --http-proxy-temp-path=/var/tmp/nginx/proxy  --http-fastcgi-temp-path=/var/tmp/nginx/fcgi  --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi  --http-scgi-temp-path=/var/tmp/nginx/scgi  --user=nginx  --group=nginx  --with-pcre  --with-http_v2_module  --with-http_ssl_module  --with-http_realip_module  --with-http_addition_module  --with-http_sub_module  --with-http_dav_module  --with-http_flv_module  --with-http_mp4_module  --with-http_gunzip_module  --with-http_gzip_static_module  --with-http_random_index_module  --with-http_secure_link_module  --with-http_stub_status_module  --with-http_auth_request_module  --with-mail  --with-mail_ssl_module  --with-file-aio  --with-ipv6  --with-http_v2_module  --with-threads  --with-stream  --with-stream_ssl_module 

如果出现如下错误

checking for C compiler ... not found

需要执行

# yum -y install gcc gcc-c++ autoconf automake make

再次安装
可能会出现下一个错误

./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=<path> option.

如果出现了,就执行这个

# yum -y install openssl openssl-devel

后面如果是这样就是执行成功了

creating objs/Makefile

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"
5、编译
# make

结尾如下

objs/ngx_modules.o \
-ldl -lpthread -lcrypt -lpcre -lz \
-Wl,-E
sed -e "s|%%PREFIX%%|/usr/local/nginx|" \
        -e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \
        -e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" \
        -e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \
        < man/nginx.8 > objs/nginx.8
make[1]: Leaving directory `/usr/local/nginx/nginx-1.9.9'
6、安装
# make install

结尾是这样的话应该就是完成了

test -d '/usr/local/nginx/logs' \
        || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/logs' \
        || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' \
        || cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' \
        || mkdir -p '/usr/local/nginx/logs'
make[1]: Leaving directory `/usr/local/nginx/nginx-1.9.9'
7、启动

参数 -c 指定了配置文件的路径,如果不加的话就是使用默认的配置文件

# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

查看是否启动

# ps -ef | grep nginx

然后在浏览器输入IP 默认端口是80
看到这样的 就是成功了
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

木小同

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

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

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

打赏作者

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

抵扣说明:

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

余额充值