首页 > 技术文章 > 编译安装nginx

Mr-Ding 2018-08-19 20:41 原文

安装nginx依赖包

yum -y install gcc gcc-c++ pcre-devel zlib-devel openssl-devel

建立一个工具目录来固定存放安装的各种软件

mkdir -p /home/dm/tools

进入/home/dm/tools目录下载并解压nginx安装包

1 wget http://nginx.org/download/nginx-1.8.1.tar.gz
2 ls -l
3 tar xf nginx-1.8.1.tar.gz 
4 cd nginx-1.8.1/

创建www用户

useradd www -s /sbin/nologin -M

 编译安装nginx

1 ./configure --user=www --group=www --prefix=/application/nginx-1.8.1 --with-http_stub_status_module --with-http_ssl_module
2 make && make install

将nginx的安装路径通过软连接的方式更改为/application/nginx,方便人员使用。

  安装时指定版本号路径是为了便于查看区分当前使用的nginx版本,也方便以后升级。

  当nginx软件升级编译成带新版本号的版本后,删除原来的软连接,再重新建立新的到/application/nginx/ 的软连接就好。

ln -s /application/nginx-1.8.1 /application/nginx

检查软连接目录状态

1 root@nginx nginx-1.8.1]# ll /application/|grep nginx
2 lrwxrwxrwx  1 root root  24 8月  19 19:06 nginx -> /application/nginx-1.8.1
3 drwxr-xr-x 11 root root 151 8月  19 19:06 nginx-1.8.1

4 [root@nginx nginx-1.8.1]# ls -l /application/nginx 5 lrwxrwxrwx 1 root root 24 8月 19 19:06 /application//nginx -> /application/nginx-1.8.1 6 [root@nginx nginx-1.8.1]#

启动并检查安装结果

1 检查配置文件语法:
2 /application/nginx/sbin/nginx -t
3 
4 启动nginx:
5 /application/nginx/sbin/nginx 

查看nginx服务是否启动成功

1 [root@nginx nginx-1.8.1]# lsof -i :80
2 COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
3 nginx   3663 root    6u  IPv4  23418      0t0  TCP *:http (LISTEN)
4 nginx   3665  www    6u  IPv4  23418      0t0  TCP *:http (LISTEN)
5 
6 [root@nginx nginx-1.8.1]# netstat -lnt|grep 80
7 tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN 

防火墙开启80端口

centos6开启方法:
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
/etc/init.d/iptables save
service iptables restart

centos7开启方法:

firewall-cmd --zone=public --add-port=80/tcp --permanent 
firewall-cmd --reload

测试:

 1 [root@nginx nginx-1.8.1]# curl -I 192.168.200.102
 2 HTTP/1.1 200 OK
 3 Server: nginx/1.8.1
 4 Date: Sun, 19 Aug 2018 12:20:40 GMT
 5 Content-Type: text/html
 6 Content-Length: 612
 7 Last-Modified: Sun, 19 Aug 2018 11:06:05 GMT
 8 Connection: keep-alive
 9 ETag: "5b794f1d-264"
10 Accept-Ranges: bytes

说明:

1、在编译安装nginx时,可以使用./configure --help 查看相关参数帮助,下面是编译时指定的参数及简单说明:

1 --prefix=PATH                      set installation prefix          #设置安装路径
2 --user=USER                        set non-privileged user for        #设置用户权限
3 --group=GROUP                      set non-privileged group for        #进程用户组权限
4 --with-http_stub_status_module     enable ngx_http_stub_status_module    #激活状态信息
5 --with-http_ssl_module             enable ngx_http_ssl_module         #激活ssl功能

 

2、nginx启动命令说明:

1 /application/nginx/sbin/nginx -h
2 
3 -V    显示版本信息和配置选项并退出;
4 -t    检测nginx配置文件是否正确;
5 -s    后面可以跟stop, quit, reopen, reload,用来停止和重启nginx服务;

 

推荐阅读