Linux下 安装Nginx完整教程及常见错误解决方案

Linux Nginx 1.5 安装教程

1.nginx默认占用80端口,so 安装的时候看你的服务器80端口是否被别的application程序占用了。

为什么nginx默认是80端口呢,因为浏览器访问的时候不加端口号也是80端口,这样输入域名或者ip就可以直接访问到服务器80所指向的服务了。

检查80端口是否被暂用

命令netstat -ntulp |grep 80

如果没有可以跳过此步骤直接进入第2步

可以看到以下有三条信息,分别是80 8005 8009端口,以上命令是模糊查询,查询出80开头的所有进程,我们看到这三个进程端口的PID都是6963,

其实这个是我服务器里面的tomcat服务,我们都知道tomcat配置文件里面有三个端口配置。

[root@iZ94j7ahvuvZ bin]# netstat -ntulp |grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6963/java
tcp 0 0 127.0.0.1:8005 0.0.0.0:* LISTEN 6963/java
tcp 0 0 0.0.0.0:8009 0.0.0.0:* LISTEN 6963/java
好了 我们先关闭端口80端口吧,就是杀死进程PID,因为这三个进程是依赖关系,所使用的进程PID也是一样,所以杀死80端口PID 6963也等于关闭8005 8009端口。

命令如下 kill -9 PID是强制杀死进程/服务

kill -9 6963

2.安装nginx

给nginx配置安装目录,就是nginx存放的目录

我一般安装软件都是安装在/usr/local下面的

mkdir /usr/local/nginx

进入nginx目录

cd /usr/local/nginx

使用wget命令下载nginx资源包

 wget http://nginx.org/download/nginx-1.5.9.tar.gz

解压

 tar -zxvf nginx-1.5.9.tar.gz

执行 ./configure命令

cd nginx-1.5.9 
./configure

执行./configure可能会存在一下错误,如果出现请执行一下命令

错误1

/configure: error: the HTTP rewrite module requires the PCRE library.

解决方法

安装pcre-devel解决问题

yum -y install pcre-devel

错误2

./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library.   You can either disable the module by using
--without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
--with-http_ssl_module --with-openssl=<path> options.

解决办法:

yum -y install openssl openssl-devel

错误3

./configure: error: C compiler cc is not found

原因:没有安装C++
解决方法:
yum -y install gcc gcc-c++ autoconf automake make

3.编译

make 编译 (make的过程是把各种语言写的源码文件,变成可执行文件和各种库文件)

cd /usr/local/nginx/nginx-1.5.9
make

4.make install安装

make install 安装 (make install是把这些编译出来的可执行文件和库文件复制到合适的地方)

make install

5.启动nginx服务

cd /usr/local/nginx/sbin
./nginx

如图:
启动后默认80 端口,浏览器打开如图所示

6.看nginx服务是否启动

[root@iZ94j7ahvuvZ sbin]# ps -ef|grep nginx
root      7017     1  0 11:07 ?        00:00:00 nginx: master process ./nginx

我们看到服务已经起来了,输入ip即可访问我们nginx目录下面的html文件夹下面的index.html文件

=========================

nginx -s reload  :修改配置后重新加载生效
nginx -s reopen  :重新打开日志文件
nginx -t -c /path/to/nginx.conf 测试nginx配置文件是否正确

关闭nginx:
nginx -s stop :快速停止nginx
quit :完整有序的停止nginx

其他的停止nginx 方式:

ps -ef | grep nginx

kill -QUIT 主进程号 :从容停止Nginx
kill -TERM 主进程号 :快速停止Nginx
pkill -9 nginx :强制停止Nginx

启动nginx:

nginx -c /path/to/nginx.conf

平滑重启nginx:
kill -HUP 主进程号

如果报错:

./configure: error: the HTTP gzip module requires the zlib library.

执行这句话

yum install -y zlib-devel

可能会发生错误:nginx: [emerg] the “ssl” parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:37

解决方法
1.原编译目录下执行

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

2.覆盖原有的nginx 执行文件

cp ./objs/nginx /usr/local/nginx/sbin/nginx 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值