环境准备
配置epel仓库源
wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo #如果提示未找到命令 使用yum install wget -y 安装一下wget #安装bash* -y 补全命令提示 #关闭防火墙和开机自启 [root@localhost ~]# systemctl stop firewalld.service [root@localhost ~]# systemctl disable firewalld.service Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. #关闭selinux [root@localhost /]# setenforce 0 #临时关闭 #修改配置文件永久关闭 [root@localhost /]# cat /etc/selinux/config # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled #改成 disabled # SELINUXTYPE= can take one of three values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted [root@localhost /]#
安装nginx
yum install nginx -y #安装nginx #启动nginx开启开机自启 [root@localhost ~]# systemctl start nginx [root@localhost ~]# systemctl enable nginx Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service. #查看nginx状态 [root@localhost ~]# systemctl status nginx ● nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled) Active: active (running) since 三 2023-07-12 08:53:49 CST; 36s ago Main PID: 14813 (nginx) CGroup: /system.slice/nginx.service ├─14813 nginx: master process /usr/sbin/nginx ├─14814 nginx: worker process ├─14815 nginx: worker process ├─14816 nginx: worker process └─14817 nginx: worker process 7月 12 08:53:48 localhost.localdomain systemd[1]: Starting The nginx HTTP and reverse proxy server... 7月 12 08:53:49 localhost.localdomain nginx[14808]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok 7月 12 08:53:49 localhost.localdomain nginx[14808]: nginx: configuration file /etc/nginx/nginx.conf test is successful 7月 12 08:53:49 localhost.localdomain systemd[1]: Started The nginx HTTP and reverse proxy server.
yum安装的nginx配置文件都在/etc/nginx/
nginx配置文件
user nginx; #启动用户 worker_processes auto; #子进程数 error_log /var/log/nginx/error.log; #错误的日志 pid /run/nginx.pid; #进程pid include /usr/share/nginx/modules/*.conf; #加载配置文件 events { worker_connections 1024; #连接数 } 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; #数据类型 include /etc/nginx/conf.d/*.conf; #加载文件 服务 虚拟主机 应用程序 server { listen 80; #端口 listen [::]:80; #ipv6端口 server_name _; #域名 主机名 root /usr/share/nginx/html; #发布目录 程序的目录 error_page 404 /404.html; #如果服务器状态码是404 就会把404.html文件返回给 我们 location = /404.html { } error_page 500 502 503 504 /50x.html;#如果服务器状态码是500 502 503 504 就 会把50x.html文件返回给我们 location = /50x.html { } } }
添加虚拟主机
#不同端口 同主机名添加虚拟主机 #vim 打开/etc/nginx/nginx.conf #在第一个server后面添加下面内容 server { listen 90; #端口号不能冲突 server_name _; #域名,主机名 root /usr/share/nginx/xiaole;#发布目录 } #相同端口不同域名添加虚拟主机 server { listen 80; #端口是80 server_name xiaole.com; #域名,主机名 root /usr/share/nginx/xiaole;#发布目录 } #如需访问需要添加在hosts添加一条解析 #Linux hosts文件在/etc/hosts #windows host文件 在C:\Windows\System32\drivers\etc\hosts