一、安装相关依赖
首先安装PCRE pcre-devel 和Zlib,因为配置nginx的时候会需要这两个东西
PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:
yum install -y pcre pcre-devel
zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
yum install -y zlib zlib-devel
虽然我安装的时候安装着两个儿就可以了,但是如果有需要,根据error提示可能还需要GCC和OpenSSL
yum install gcc-c++
yum install -y openssl openssl-devel
二、安装
现在我们开始安装nginx, 获取地址 直接搜nginx 进入页面 复制最新下载地址
wget -c http://nginx.org/download/nginx-1.17.4.tar.gz
解压并进入nginx目录
tar -xzvf nginx-1.17.4.tar.gz
cd nginx-1.17.4
使用nginx的默认配置
./configure --prefix=/usr/local/nginx
--prefix=/usr/local/nginx:指定安装目录
编译安装
make
make install
进入/usr/local/nginx/sbin目录,可以看到有一个可执行文件nginx,直接./nginx执行就OK了。
运行起来之后访问服务器ip,可以看到nginx的欢迎页面
命令:
./nginx 启动
./nginx -s stop 快速停止
./nginx -s quit 优雅关闭,在退出前完成已经接受的链接请求
./nginx -s reload 重新加载配置
简单配置:
nginx.conf 进行编辑,注:listen 端口号如80 和代理端口号如8080 不要一样
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/local/my/dist;
index index.html index.htm;
}
location /my{
proxy_pass http://127.0.0.1:8080/;
}
location /myapk {
alias /usr/local/my/apk/;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
三、安装成系统服务
在如下位置创建服务脚本nginx.service
vi /usr/lib/systemd/system/nginx.service
服务脚本内容如下(注意路径要对应,这里的路径是/usr/local/nginx/sbin):
[Unit]
Description=nginx - web server
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 -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true[Install]
WantedBy=multi-user.target
重新加载系统服务
systemctl daemon-reload
启动服务
systemctl start nginx.service
开机启动
systemctl enable nginx.service