一、远程连接

远程连接到linux系统

二、安装相关依赖

1.pcre安装

yum install pcre pcre-devel -y

# 正常显示版本号即安装成功
pcre-config --version
8.32



# 解压安装包
tar zxvf pcre-8.32.tar.gz
# 进入安装目录
cd pcre-8.32

#执行安装

# 配置编译选项
./configure
# 编译
make
# 安装
make install

2.安装其他依赖

yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel

三、安装nginx

#先把压缩包放到文件夹
# 解压安装包
tar zxvf xxx.tar.gz
# 进入安装目录
cd xxx

#执行安装

# 配置编译选项
./configure
# 编译
make
# 安装
make install

四、开放防火墙

#查看开放端口
firewall-cmd --list-all

#设置开放的端口号
firewall-cmd --add-service=http -permanent
sudo firewall-cmd --add-port=80/tcp --permanent

#重启防火墙
firewall-cmd --reload

五、启动nginx

systemctl start nginx

# 2. 设置开机自启(推荐)
systemctl enable nginx

ps aux | grep nginx

六、常用命令

使用nginx命令的前提是进入nginx的目录

首先要确定 Nginx 的安装目录,这是使用命令的前提,常见的安装方式对应目录如下:

安装方式 主目录(prefix) 可执行文件路径 配置文件路径
源码编译安装(推荐) /usr/local/nginx /usr/local/nginx/sbin/nginx /usr/local/nginx/conf/nginx.conf
yum/apt 包管理器安装 /etc/nginx /usr/sbin/nginx /etc/nginx/nginx.conf
Docker 容器 容器内/etc/nginx 容器内/usr/sbin/nginx 容器内/etc/nginx/nginx.conf
#进入可执行文件的目录
[root@VM-0-16-centos nginx]# cd /usr/sbin
#查看版本号
[root@VM-0-16-centos sbin]# ./nginx -v
nginx version: nginx/1.20.1
#停止nginx
[root@VM-0-16-centos sbin]# ./nginx -s stop
#查看nginx进程
[root@VM-0-16-centos sbin]# ps -ef|grep nginx
root     25660 31786  0 18:54 pts/0    00:00:00 grep --color=auto nginx
#启动nginx
[root@VM-0-16-centos sbin]# ./nginx
#重新加载nginx
[root@VM-0-16-centos sbin]# ./nginx -s reload

七、配置文件

配置文件的位置看上面的表格

配置文件的组成有三部分:

1.全局块

处理并发的数量:值越大,并发量越大

worker_process 1;

2.events块

主要影响nginx服务器与用户的网络连接

支持最大的连接数

worker_connections 1024;

3.http块

代理、缓存、日志等都在这部分,是使用最频繁的。

包含全局http块和server块,server块和虚拟主机有密切关系。

server块又包括全局server和location。


http {
    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;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐