环境
SystemOS:CentOS 7
相关插件下载(可选)
安装gcc
安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装
yum -y install gcc
pcre、pcre-devel安装
pcre是一个perl库,包括perl兼容的正则表达式库,nginx的http模块使用pcre来解析正则表达式,所以需要安装pcre库
yum install -y pcre pcre-devel
zlib安装
zlib库提供了很多种压缩和解压缩方式nginx使用zlib对http包的内容进行gzip,所以需要安装
yum install -y zlib zlib-devel
安装openssl
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库
yum install -y openssl openssl-devel
安装Nginx
下载安装包(自行查询当下最新/稳定版本,地址:nginx.org/en/download…)
| |
| wget http://nginx.org/download/nginx-1.20.0.tar.gz |
解压
| |
| tar -zxvf nginx-1.20.0.tar.gz |
配置
- 使用默认配置(推荐,默认配置在/usr/local/nginx目录下)
./configure
| |
| ./configure \ |
| --prefix=/usr/local/nginx \ |
| --conf-path=/usr/local/nginx/conf/nginx.conf \ |
| --pid-path=/usr/local/nginx/conf/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 |
编译安装(这一步才会去真正常执行文件Nginx安装)
| # 在nginx解压后文件目录内执行 |
| make && make install |
| # Nginx默认安装在/usr/local/nginx,安装完后有需要的话可以把解压文件删了 |
启动/停止/重启
| cd /usr/local/nginx/sbin/ |
| |
| ./nginx |
| |
| ./nginx -s stop |
| |
| ./nginx -s quit |
| |
| ./nginx -s reload |
设置开机自动启动(如果设置为服务并设置服务开机启动后,需要把这个取消,否则会产生冲突)
| vi /etc/rc.local |
| |
| /usr/local/nginx/sbin/nginx |
| |
| chmod 755 rc.local |
配置Nginx为服务&设置开机启动(需要把上面设置的开机启动取消)
创建nginx.service文件
| cd /lib/systemd/system/ |
| vim nginx.service |
输入以下内容(如果Nginx安装路径不是默认路径,需要自己手动修改路径,默认为 /usr/local/nginx
)
| [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 |
| |
| Type=forking |
| 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 |
| PrivateTmp=true |
| |
| [Install] |
| WantedBy=multi-user.target |
设置执行权限
chmod -r 755 ./nginx.service
执行
| systemctl start nginx.service 启动nginx |
| |
| systemctl stop nginx.service 结束nginx |
| |
| systemctl restart nginx.service 重启nginx |
加入开机自动启动
| |
| systemctl enable nginx.service |
| |
| systemctl daemon-reload |
加入系统变量
修改/etc/profile文件 vim /etc/profile
加入下面两行
| export NGINX_HOME=/usr/local/nginx |
| export PATH=${NGINX_HOME}/sbin:${PATH} |
3. 生效
source /etc/profile
4. 验证
nginx -v
nginx -s 命令行参数
| 验证配置是否正确: nginx -t |
| 查看Nginx的详细的版本号:nginx -V |
| 查看Nginx的简洁版本号:nginx -v |
| 启动Nginx:start nginx |
| 快速停止或关闭Nginx:nginx -s stop |
| 正常停止或关闭Nginx:nginx -s quit |
| 配置文件修改重装载命令:nginx -s reload |
配置nginx.conf
相关问题
- 启动时报80端口被占用: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
- 解决方案:
- 安装net-tool包:
yum install net-tools
- 查询进程:
ps aux|grep nginx