linux安装nginx

nginx的官方网站:http://www.nginx.org/

  1. Mainline version: 主线版,即开发版
  2. Stable version: 最新稳定版,生产环境上建议使用的版本
  3. Legacy versions: 遗留的老版本的稳定版

一、编译安装
1.安装编译环境

[root@localhost ~]#yum -y install gcc gcc-c++


2.安装pcre软件包(使nginx支持http rewrite模块)

[root@localhost ~]#yum install -y pcre pcre-devel


3.安装openssl-devel(使nginx支持ssl)

[root@localhost ~]#yum install -y openssl openssl-devel


4.安装zlib

[root@localhost ~]#yum install -y zlib zlib-devel


5.创建用户nginx

[root@localhost ~]#useradd nginx

[root@localhost ~]#passwd nginx

可以忽略。
6.nginx安装
把对应的安装包放在/usr/local/下面,列如:nginx-1.18.0.tar.gz

[root@localhost ~]#cd /usr/local/

[root@localhost ~]# tar xzf nginx-1.18.0.tar.gz

[root@localhost ~]# cd /usr/local/nginx-1.18.0/

[root@localhost nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream

[root@localhost nginx-1.18.0]# make && make install


# 查看 nginx 安装的模块

[root@localhost ~]# cd /root
[root@localhost ~]#/usr/local/nginx/sbin/nginx -V


二、nginx配置


Nginx配置文件为/etc/nginx/nginx.conf,nginx.conf的组成:nginx.conf一共由三部分组成,分别为:全局块、events块、http块。在http块中又包含http全局块、多个server块。每个server块中又包含server全局块以及多个location块。在统一配置块中嵌套的配置快,各个之间不存在次序关系。

# 全局参数设置
user nginx;            #设置nginx使用的用户
worker_processes  4;  #设置nginx启动进程的数量,一般设置成与逻辑cpu数量相同 
error_log  logs/error.log;  #指定错误日志 
worker_rlimit_nofile 1024;  #设置一个nginx进程能打开的最大文件数 
pid        /var/run/nginx.pid; 
events { 
    worker_connections  1024; #设置一个进程的最大并发连接数 
}
# http 服务相关设置 
http { 
    include      mime.types; 
    default_type  application/octet-stream; 
    log_format  main  'remote_addr - remote_user [time_local] "request" '
                      'status body_bytes_sent "$http_referer" '
                      '"http_user_agent" "http_x_forwarded_for"'; 
    access_log  /var/log/nginx/access.log  main;    #设置访问日志的位置和格式 
    sendfile          on; #是否调用sendfile函数输出文件,一般设置为on,若nginx是用来进行磁盘IO负载应用时,可以设置为off,降低系统负载 
    gzip              on;      #是否开启gzip压缩,将注释去掉开启 
    keepalive_timeout  65;     #设置长连接的超时时间
# 虚拟服务器的相关设置 
    server { 
        listen      80;        #设置监听的端口 
        server_name  localhost;        #设置绑定的主机名、域名或ip地址 
        charset koi8-r;        # 设置编码字符 
        location / { 
            root  /var/www/nginx;           #设置服务器默认网站的根目录位置,需要手动创建
            index  index.html index.htm;    #设置默认打开的文档 
            } 
        error_page  500 502 503 504  /50x.html; #设置错误信息返回页面 
        location = /50x.html { 
            root  html;        #这里的绝对位置是/usr/local/nginx/html
        } 
    } 
 }


检测nginx配置文件是否正确

[root@localhost ~]#mkdir -p /usr/local/nginx/logs

[root@localhost ~]# mkdir -p /var/www/nginx

[root@localhost ~]# mkdir -p /tmp/nginx

[root@localhost ~]# /usr/local/nginx/sbin/nginx -t

[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

[root@localhost ~]#chown -R nginx:nginx /usr/local

[root@localhost ~]#chown -R nginx:nginx  /etc/nginx

[root@localhost ~]#chown -R nginx:nginx  /var/log/nginx

[root@localhost ~]#chown -R nginx:nginx  /tmp/nginx

 


三、nginx服务启停及常用命令


nginx服务启动

[root@localhost ~]# /usr/local/nginx/sbin/nginx


 

nginx -c /path/nginx.conf        # 以特定目录下的配置文件启动nginx:
nginx -s reload                  # 修改配置后重新加载生效
nginx -s stop                    # 快速停止nginx
nginx -s quit                    # 完整有序的停止nginx
nginx -t                         # 测试当前配置文件是否正确
nginx -t -c /path/to/nginx.conf  # 测试特定的nginx配置文件是否正确


注意:
nginx -s reload 命令加载修改后的配置文件,命令下达后发生如下事件
1. Nginx的master进程检查配置文件的正确性,若是错误则返回错误信息,nginx继续采用原配置文件进行工作(因为worker未受到影响)
2. Nginx启动新的worker进程,采用新的配置文件
3. Nginx将新的请求分配新的worker进程
4. Nginx等待以前的worker进程的全部请求已经都返回后,关闭相关worker进程
5. 重复上面过程,知道全部旧的worker进程都被关闭掉

把nginx添加到服务

vim /etc/init.d/nginx

 添加下面脚本内容 

#!/bin/bash
#chkconfig: - 99 20
#description:Nginx Service Control Script
COM="/usr/local/nginx/sbin/nginx"
PID="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
  $COM
;;
 
stop)
  kill -s QUIT $(cat $PID)
;;
 
restart)
  $0 stop
  $0 start
;;
 
reload)
  kill -s HUP $(cat $PID)
;;
 
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
 
esac
exit 0

 授权并添加服务

chmod +x /etc/init.d/nginx
chkconfig --add nginx

 常用命令

# 开机配置
systemctl enable nginx # 开机自动启动
systemctl disable nginx # 关闭开机自动启动

# 启动Nginx
systemctl start nginx # 启动Nginx成功后,可以直接访问主机IP,此时会展示Nginx默认页面

# 停止Nginx
systemctl stop nginx

# 重启Nginx
systemctl restart nginx

# 重新加载Nginx
systemctl reload nginx

# 查看 Nginx 运行状态
systemctl status nginx

# 查看Nginx进程
ps -ef | grep nginx

# 杀死Nginx进程
kill -9 pid # 根据上面查看到的Nginx进程号,杀死Nginx进程,-9 表示强制结束进程

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Linux安装Nginx有几种方法。一种方法是通过包管理器进行安装。您可以使用命令"yum install nginx"来安装最新的稳定版本,默认情况下安装的是Nginx 1.20.2版本。另外一种方法是通过源码编译安装Nginx。这需要下载Nginx安装包并解压缩,然后进行依赖安装和配置。具体步骤如下: 1. 下载Nginx安装包,并解压缩。 2. 安装Nginx的依赖包。 3. 进入解压缩后的Nginx目录,执行"./configure"命令进行配置。 4. 执行"make"命令进行编译。 5. 执行"make install"命令进行安装。 6. 修改Nginx的配置文件"nginx.conf",可以设置用户和用户组等参数。 7. 启动Nginx,可以使用命令"nginx"。 8. 停止或重启Nginx,可以使用命令"nginx -s stop"和"nginx -s reload"。 9. 设置Nginx开机自启动,可以将Nginx添加到系统的启动项中。 10. 配置防火墙,确保80端口开放以允许Nginx的HTTP访问。 根据您的需求和喜好,您可以选择适合您的安装方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Linux安装nginx详细步骤](https://blog.csdn.net/adaizzz/article/details/126669430)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [linux 系统下四种nginx安装方法](https://blog.csdn.net/shallow72/article/details/123878716)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

罗大胖丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值