CentOS7编译安装Nginx

0

第一次用 centos7 编译安装配置 nginx、php 环境,和我以前使用 Ubuntu apt包管理器开箱即用不同的是,需要自己编译、解决依赖、配置启动/重载/重启服务和文件权限。

1 安装配置过程

下载安装包,stabel version

1
wget -C http://nginx.org/download/nginx-1.14.0.tar.gz

解压

1
tar -zxvf nginx-1.14.0.tar.gz

编译安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cd nginx-1.14.0
# 编译
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
#指定运行权限的用户
# --user=nginx
#指定运行的权限用户组
# --group=nginx
#指定安装路径
# --prefix=/usr/local/nginx
#支持nginx状态查询
# --with-http_stub_status_module
#开启ssl支持
# --with-http_ssl_module
#开启GZIP功能
# --with-http_gzip_static_module
# 安装
make && make install

配置启动/重载/重启服务

centos7 使用 systemctl 这个指令来配置 service,如果使用 apt 或 yum 安装 nginx 的话,会自动在 /lib/systemd/system 生成一个 nginx.service,而编译安装需要手动去添加这个service。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[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
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
1
2
3
4
5
6
7
systemctl enable nginx.service #开机运行nginx
systemctl disable nginx.service #取消开机运行nginx
systemctl start nginx.service #启动nginx
systemctl stop nginx.service #停止nginx
systemctl restart nginx.service #重启nginx
systemctl reload nginx.service #重新加载nginx配置文件
# service 这个指令也可以使用,不过在centos7 service功能转移到systemctl上

nginx 配置

1
2
3
4
5
6
7
8
# 将php结尾的 uri 交给 php-fpm 去响应
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

快捷命令

由于是编译安装,所有可执行指令都在安装目录下,可以用 ln -s 到 /usr/local/bin 目录下。

或者在 .bashrc 或我使用的 .zshrc 下加个alias

1
alias nginx="/usr/local/nginx/sbin/nginx"

upstream、handler模块待了解。

picture

引用自 http://tengine.taobao.org/book/_images/chapter-2-1.PNG