源码安装Nginx服务器

2017.5.25 0 条评论 1.23k 次阅读 0 人点赞

Nginx是一款轻量级的Web服务器,在连接高并发的情况下,Nginx是Apache服务器不错的替代品,本文安装Nginx替代Apache服务器。

1 安装环境

操作系统:CentOS Linux release 7.3.1611 (Core)
Kernel版本:3.10.0-514.16.1.el7.x86_64

2、安装Nginx依赖的库文件

http_rewrite_module模块(默认安装)需要pcre库,http_gzip_module模块(默认安装)需要zlib库,http_ssl_module模块(默认不安装)需要OpenSSL库:

[root@www ~]# yum install -y gcc make pcre-devel zlib-devel

3、安装最新稳定版的Nginx软件

首先下载Nginx源码:

[root@www ~]# mkdir -p /u01/src /u01/app
[root@www ~]# cd /u01/src
[root@www src]# wget http://nginx.org/download/nginx-1.12.0.tar.gz
[root@www src]# tar -zxf nginx-1.12.0.tar.gz
[root@www src]# cd nginx-1.12.0/

(可选)修改源码隐藏Nginx软件名及版本号,就叫Jianxiangqiao Web Server,简称JWS吧,版本1.0.1:

[root@www nginx-1.12.0]# sed -i '{
s!#define NGINX_VERSION      "1.12.0"!#define NGINX_VERSION      "1.0.1"!g
s!#define NGINX_VER          "nginx/" NGINX_VERSION!#define NGINX_VER          "JWS/" NGINX_VERSION!g
s!#define NGINX_VAR          "NGINX"!#define NGINX_VAR          "JWS"!g
}' src/core/nginx.h
[root@www nginx-1.12.0]# sed -i 's/ngx_http_server_string\[\] = "Server: nginx" CRLF;/ngx_http_server_string\[\] = "Server: JWS" CRLF;/g' src/http/ngx_http_header_filter_module.c

编译安装:

[root@www nginx-1.12.0]# ./configure --user=nginx --group=nginx --prefix=/u01/app/nginx-1.12.0
[root@www nginx-1.12.0]# make
[root@www nginx-1.12.0]# make install
[root@www nginx-1.12.0]# cd /u01/app
[root@www app]# ln -s nginx-1.12.0 nginx

4、启动并检查Nginx安装结果

首先创建nginx用户:

[root@www app]# useradd -s /sbin/nologin -M nginx

检查配置文件并启动Nginx:

[root@www app]# /u01/app/nginx/sbin/nginx -t
nginx: the configuration file /u01/app/nginx-1.12.0/conf/nginx.conf syntax is ok
nginx: configuration file /u01/app/nginx-1.12.0/conf/nginx.conf test is successful
//如果报“nginx: [emerg] getpwnam("nginx") failed”错误,检查是否执行了前面创建nginx用户的命令
[root@www app]# /u01/app/nginx/sbin/nginx

检查Nginx监听状态:

[root@www app]# lsof -i :80
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   7119  root    6u  IPv4  35036      0t0  TCP *:http (LISTEN)
nginx   7120 nginx    6u  IPv4  35036      0t0  TCP *:http (LISTEN)
[root@www app]# netstat -lnptu|grep :80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7119/nginx: master  

检查网页是否能正常打开:

[root@www app]# curl 127.0.0.1

或者浏览器访问:

Nginx

Nginx

5、配置Nginx启动项

配置nginx.service:

[root@www app]# vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStartPre=/u01/app/nginx/sbin/nginx -t
ExecStart=/u01/app/nginx/sbin/nginx
ExecReload=/u01/app/nginx/sbin/nginx -s reload
ExecStop=/u01/app/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

设置Nginx开机启动并测试:

[root@www app]# systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@www app]# /u01/app/nginx/sbin/nginx -s stop
[root@www app]# systemctl start nginx
[root@www app]# systemctl status nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2017-05-22 21:00:45 CST; 5s ago
     Docs: http://nginx.org/en/docs/
  Process: 12089 ExecStart=/u01/app/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 12086 ExecStartPre=/u01/app/nginx/sbin/nginx -t (code=exited, status=0/SUCCESS)
 Main PID: 12090 (nginx)
   CGroup: /system.slice/nginx.service
           ├─12090 nginx: master process /u01/app/nginx/sbin/nginx
           └─12091 nginx: worker process

May 22 21:00:45 www systemd[1]: Starting nginx - high performance web server...
May 22 21:00:45 www nginx[12086]: nginx: the configuration file /u01/app/nginx-1.12.0/conf/nginx.conf syntax is ok
May 22 21:00:45 www nginx[12086]: nginx: configuration file /u01/app/nginx-1.12.0/conf/nginx.conf test is successful
May 22 21:00:45 www systemd[1]: Started nginx - high performance web server.
Hint: Some lines were ellipsized, use -l to show in full.

Nginx服务器安装完成,暂时使用之前安装的MySQL数据库,下一步安装PHP。
参考资料:
http://nginx.org/en/docs/configure.html
https://www.freedesktop.org/software/systemd/man/systemd.service.html
————————————————
本文永久地址http://www.jianxiangqiao.com/nginx-installation-src/,转载请注明出处。

健翔桥的奇迹

IT工程师/摄影历史/跑步骑行