一 Nginx安装环境准备
nginx是C语言开发,建议在linux上运行,本教程使用Centos7作为安装环境。
- gcc
安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:
yum install gcc-c++
- PCRE
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。
yum install -y pcre pcre-devel
注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。
- zlib
zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。
yum install -y zlib zlib-devel
- openssl
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。
yum install -y openssl openssl-devel
二 编译安装
- 在线安装以上4个环境
- 上传nginx安装包到/opt/software/nginx
- 解压 tar -zxvf 安装包名
- 在/usr/local下创建文件夹nginx (下面会指定引用)
- 在/var下创建/temp/nginx文件夹 (下面会指定引用,注意是建了两个文件夹)
- 在nginx的解压目录执行配置 (在目录下直接粘贴这段代码)
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
- 等上面执行完了,继续在这个文件夹目录下执行
make - make install
- 关闭防火墙
停止firewall
systemctl stop firewalld.service
禁止firewall开机启动
systemctl disable firewalld.service
三 启动nginx
cd /usr/local/nginx/sbin/
./nginx
在浏览器输入自己的ip查看
例如:我的ip: http://192.168.220.110/
nginx默认端口是80
出现welcome to nginx 说明启动成功
四 nginx常用命令
- 停止nginx
cd /usr/local/nginx/sbin
./nginx -s quit
- 重启nginx
有两种方法
1 先关闭再重启
./nginx -s quit
./nginx
2 直接重启
./nginx -s reload