编译安装Nginx

一 简介:

    Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。

    官网地址:http://nginx.org/

二 编译安装Nginx

   详细编译注释:===>>>点击进入


2.1.1更改阿里yum源

[root@centos7 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

2.1.2 安装依赖工具

#建议安装pcre, zlib,前者为了重写rewrite,后者为了gzip压缩

[root@centos7 ~]# yum -y install pcre pcre-devel openssl openssl-devel gcc gcc-c++ autoconf automake zlib zlib-devel libxml2 libxml2-dev libxslt-devel gd-devel perl-devel perl-ExtUtils-Embed GeoIP GeoIP-devel GeoIP-data make  GeoIP-devel GeoIP-update

2.1.3 创建Nginx运行用户和组及目录

[root@centos7 ~]# groupadd nginx
[root@centos7 ~]# useradd nginx -g nginx -s /sbin/nologin -M

[root@centos7 ~]# mkdir /tmp/nginx/client -p 

[root@centos7 ~]# mkdir /var/tmp/nginx/proxy -p

2.1.4 下载Nginx1.15.2并编译安装

[root@centos7 tools]# wget http://nginx.org/download/nginx-1.15.2.tar.gz
#解压
[root@centos7 tools]# tar xf nginx-1.15.2.tar.gz 
[root@centos7 tools]# cd nginx-1.15.2/ 
#--help可查看编译帮助
[root@centos7 nginx-1.15.2]# ./configure --help

#配置
[root@centos7 nginx-1.15.2]# ./configure --prefix=/usr/local/nginx-1.15.2 --user=nginx --group=nginx --lock-path=/usr/local/nginx-1.15.2/lock --with-http_ssl_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_perl_module --with-mail --with-mail_ssl_module --with-pcre --http-client-body-temp-path=/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=/tmp/nginx/scgi  --with-stream
#查看返回值
[root@centos7 nginx-1.15.2]# echo $?
0
#编译安装
[root@centos7 nginx-1.15.2]# make -j 2

[root@centos7 nginx-1.15.2]# echo $?
0

[root@centos7 nginx-1.15.2]# make install

[root@centos7 nginx-1.15.2]# echo $?
0

三 检查并验证启动

3.1.1 查看安装目录

[root@centos7 nginx-1.15.2]# cd /usr/local/nginx-1.15.2/
[root@centos7 nginx-1.15.2]# tree 
.
├── conf
│   ├── fastcgi.conf
│   ├── fastcgi.conf.default
│   ├── fastcgi_params
│   ├── fastcgi_params.default
│   ├── koi-utf
│   ├── koi-win
│   ├── mime.types
│   ├── mime.types.default
│   ├── nginx.conf
│   ├── nginx.conf.default
│   ├── scgi_params
│   ├── scgi_params.default
│   ├── uwsgi_params
│   ├── uwsgi_params.default
│   └── win-utf
├── html
│   ├── 50x.html
│   └── index.html
├── logs
└── sbin
    └── nginx

4 directories, 18 files

3.1.2 软连接

[root@centos7 nginx-1.15.2]# ln -sv /usr/local/nginx-1.15.2/ /usr/local/nginx
"/usr/local/nginx" -> "/usr/local/nginx-1.15.2/"
[root@centos7 nginx-1.15.2]# ln -sv /usr/local/nginx/sbin/nginx /usr/bin/nginx
"/usr/bin/nginx" -> "/usr/local/nginx/sbin/nginx"

3.1.3 检查语法并启动

[root@centos7 ~]# nginx -t
nginx: the configuration file /usr/local/nginx-1.15.2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.15.2/conf/nginx.conf test is successful
[root@centos7 ~]# nginx 
[root@centos7 ~]# netstat -lnutp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      66669/nginx: master 

四 编写启动脚本

[root@centos7 system]# cat /usr/lib/systemd/system/nginx.service 
[Unit]
Description=nginx
After=network.target
 
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /usr/local/nginx/logs/nginx.pid
TimeoutStopSec=5
KillMode=mixed
 
[Install]
WantedBy=multi-user.target

测试

[root@centos7 system]# systemctl start nginx
[root@centos7 system]# netstat -lnutp | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      67513/ng
[root@centos7 system]# systemctl stop nginx
[root@centos7 system]# netstat -lnutp | grep 80
[root@centos7 system]# systemctl restart nginx
[root@centos7 system]# netstat  -lnutp | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      67545/ng
[root@centos7 system]# systemctl status nginx
● nginx.service - nginx
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disab
   Active: active (running) since 五 2018-08-10 14:05:21 CST; 13s ago
  Process: 67543 ExecStart=/usr/local/nginx/sbin/nginx -g daemon on; master_process on; 
  Process: 67540 ExecStartPre=/usr/local/nginx/sbin/nginx -t -q -g daemon on; master_pro
 Main PID: 67545 (nginx)
    Tasks: 2
   CGroup: /system.slice/nginx.service
           ├─67545 nginx: master process /usr/local/nginx/sbin/nginx -g daemon on; maste
           └─67546 nginx: worker process

8月 10 14:05:21 centos7.2 systemd[1]: Starting nginx...
8月 10 14:05:21 centos7.2 systemd[1]: Failed to read PID from file /usr/local/nginx/logs
8月 10 14:05:21 centos7.2 systemd[1]: Started nginx.

 

posted @ 2018-08-09 16:36  闫世成  阅读(322)  评论(0编辑  收藏  举报