Linux安装Nginx

99 阅读4分钟

环境

SystemOS:CentOS 7

相关插件下载(可选)

  1. 安装gcc 安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装
yum -y install gcc
  1. pcre、pcre-devel安装 pcre是一个perl库,包括perl兼容的正则表达式库,nginx的http模块使用pcre来解析正则表达式,所以需要安装pcre库
yum install -y pcre pcre-devel
  1. zlib安装 zlib库提供了很多种压缩和解压缩方式nginx使用zlib对http包的内容进行gzip,所以需要安装
yum install -y zlib zlib-devel
  1. 安装openssl

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。 nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库

yum install -y openssl openssl-devel

安装Nginx

  1. 下载安装包(自行查询当下最新/稳定版本,地址:nginx.org/en/download…
# 如果没有安装wget 可以执行 yum install wget 安装
wget http://nginx.org/download/nginx-1.20.0.tar.gz  
  1. 解压
# 实际看自己下的哪个版本
tar -zxvf nginx-1.20.0.tar.gz
  1. 配置
# 先进入解压后的Nginx文件夹
cd nginx-1.20.0
  • 使用默认配置(推荐,默认配置在/usr/local/nginx目录下)
./configure
  • 自定义配置(只是配置(配置安装路径),不会安装)
# 注意:将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录
./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
  1. 编译安装(这一步才会去真正常执行文件Nginx安装)
# 在nginx解压后文件目录内执行
make && make install 
# Nginx默认安装在/usr/local/nginx,安装完后有需要的话可以把解压文件删了
  1. 启动/停止/重启
cd /usr/local/nginx/sbin/
# 启动Nginx
./nginx
#  此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程
./nginx -s stop
# 此方式停止步骤是待nginx进程处理任务完毕进行停止
./nginx -s quit
# 在Nginx启动状态下,重启Nginx
./nginx -s reload
  1. 设置开机自动启动(如果设置为服务并设置服务开机启动后,需要把这个取消,否则会产生冲突)
vi /etc/rc.local
# 增加一行
/usr/local/nginx/sbin/nginx
# 设置执行权限
chmod 755 rc.local

配置Nginx为服务&设置开机启动(需要把上面设置的开机启动取消)

  1. 创建nginx.service文件
cd /lib/systemd/system/
vim nginx.service
  1. 输入以下内容(如果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
  1. 设置执行权限
chmod -r 755 ./nginx.service
  1. 执行
systemctl start nginx.service    启动nginx

systemctl stop nginx.service    结束nginx

systemctl restart nginx.service    重启nginx
  1. 加入开机自动启动
# 设置开机启动
systemctl enable nginx.service
# 这句我也不知道啥意思。。有的教程有写有的没有
systemctl daemon-reload
  1. 加入系统变量
    1. 修改/etc/profile文件 vim /etc/profile
    2. 加入下面两行
export NGINX_HOME=/usr/local/nginx
export PATH=${NGINX_HOME}/sbin:${PATH}
  3. 生效
source /etc/profile
  4. 验证
nginx -v
  1. 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)
    • 解决方案:
      1. 安装net-tool包:yum install net-tools
      2. 查询进程:ps aux|grep nginx