nginx的安装与配置

本文详细介绍了nginx的安装过程,包括下载、解压、创建系统用户、安装依赖、配置环境变量、设置开机自启等步骤,并重点讲解了配置参数的用途,如用于调试的参数、性能优化参数、事件及网络连接配置,还提到了nginx作为web服务器时的http{}段配置,如虚拟主机、监听端口、默认页面和路径别名等。
摘要由CSDN通过智能技术生成

nginx的安装

提前关闭好防火墙和selinux
下载nginx

[root@localhost ~]# wget https://nginx.org/download/nginx-1.20.2.tar.gz
[root@localhost ~]# wget https://nginx.org/download/nginx-1.22.0.tar.gz

解压

[root@localhost ~]# wget https://nginx.org/download/nginx-1.22.0.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg  nginx-1.20.2.tar.gz
nginx-1.20.2     nginx-1.22.0.tar.gz

创建系统用户nginx

[root@localhost ~]# cd nginx-1.20.2/
[root@localhost nginx-1.20.2]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README
[root@localhost nginx-1.20.2]# 
[root@localhost nginx-1.20.2]# useradd -rMs /sbin/nologin nginx
[root@localhost nginx-1.20.2]# 

安装依赖环境

[root@localhost nginx-1.20.2]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
安装过程略....

创建日志存放目录

[root@localhost nginx-1.20.2]# mkdir -p /var/log/nginx
[root@localhost nginx-1.20.2]# chown -R nginx.nginx /var/log/nginx
[root@localhost nginx-1.20.2]# 

编译安装

[root@localhost nginx-1.20.2]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log

[root@localhost nginx-1.20.2]# make -j 1
安装过程略....
[root@localhost nginx-1.20.2]# make install

查看并开启服务

[root@localhost nginx]# cd /usr/local/nginx
[root@localhost nginx]# ls sbin/
nginx
[root@localhost nginx]# du -sh sbin/nginx 
6.1M	sbin/nginx
[root@localhost nginx]# /usr/local/nginx/sbin/nginx 
[root@localhost nginx]# ss -antl
State  Recv-Q Send-Q  Local Address:Port   Peer Address:Port Process 
LISTEN 0      128           0.0.0.0:80          0.0.0.0:*            
LISTEN 0      128           0.0.0.0:22          0.0.0.0:*            
LISTEN 0      128              [::]:22             [::]:*            

访问网站
在这里插入图片描述

nginx安装后配置

配置环境变量

[root@localhost nginx]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost nginx]# source /etc/profile.d/nginx.sh 
[root@localhost nginx]# cd
[root@localhost ~]# which nginx 
/usr/local/nginx/sbin/nginx
[root@localhost ~]# 

配置开机自启

[root@localhost ~]# vim /usr/lib/systemd/system/nginx.service 
[Unit]
Description=nginx server daemon
After=network.target 

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# nginx -s stop
[root@localhost ~]# systemctl status nginx.service 
● nginx.service - nginx server daemon
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; >
   Active: inactive (dead)
[root@localhost ~]# systemctl start nginx.service 
[root@localhost ~]# systemctl status nginx.service 

[root@localhost ~]# systemctl enable nginx.service 
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@localhost ~]# systemctl status nginx.service 
● nginx.service - nginx server daemon
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; v>
   Active: active (running) since Mon 2022-10-10 22:09:20 CST; 23s a>
 Main PID: 57854 (nginx)
    Tasks: 2 (limit: 23500)
   Memory: 2.2M
   CGroup: /system.slice/nginx.service

-c //指定配置文件的路径

[root@localhost ~]# cp /usr/local/nginx/conf/nginx.conf /opt/
[root@localhost ~]# cp /usr/local/nginx/conf/mime.types /opt/
[root@localhost ~]# vim /opt/nginx.conf 
server {
        listen       8080;			//改8080
        server_name  localhost;
[root@localhost ~]# nginx -c /opt/nginx.conf 
[root@localhost ~]# ss -antl
State  Recv-Q Send-Q  Local Address:Port   Peer Address:Port Process 
LISTEN 0      128           0.0.0.0:8080        0.0.0.0:*            
LISTEN 0      128           0.0.0.0:80          0.0.0.0:*            
LISTEN 0      128           0.0.0.0:22          0.0.0.0:*            
LISTEN 0      128              [::]:22             [::]:*            
[root@localhost ~]# 


用于调试、定位问题的配置参数

daemon {on|off};    //是否以守
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值