1.安装nginx

yum -y  install nginx

2.如果这一步报错,是因为因为本地​yum源​中没有我们想要的nginx,那么我们就需要创建一个/etc/yum.repos.d/nginx.repo的文件,新增一个yum源。

vi /etc/yum.repos.d/nginx.repo
#写入以下内容
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

3.配置文件nginx.conf

vi /etc/nginx/nginx.conf   #编辑配置文件
cat /etc/nginx/nginx.conf #查看nginx.conf内容
#直接复制以下内容
user nginx;
worker_processes 8;
worker_cpu_affinity auto;
worker_rlimit_nofile 65535;
pid /var/run/nginx.pid;
events {
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
log_format mains $server_name "|" $remote_addr "|" [$time_iso8601] "|" "$request" "|"
$status "|" $body_bytes_sent "|" "$http_referer" "|"
"$http_user_agent" "|" $upstream_addr "|" $request_time "|" $upstream_response_time|;

client_max_body_size 100M;
client_body_buffer_size 1024k;
client_header_buffer_size 32k;
sendfile on;
gzip on;
tcp_nopush on;
keepalive_timeout 300;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_ignore_client_abort on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_disable "MSIE [1-6]\.";
gzip_vary off;
include /etc/nginx/conf.d/http/*.conf;

}

4.检查一下有没有配置好

[root@localhost ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost ~]# nginx -s reload

5.启动nginx

systemctl start nginx  #启动
systemctl enable nginx #默认开机自启
systemctl status nginx #查看启动状态
nginx -t #检查配置是否正确
nginx -s reload #重新加载配置
ps -ef|grep nginx #查看进程

6.创建一个include,并写上配置

[root@localhost ~]# mkdir -p /etc/nginx/conf.d/http
[root@localhost ~]# cd /etc/nginx/conf.d/http
[root@localhost http]# vi guangwang.conf
#写入以下配置
server {
listen 80;
location / {
root /opt/static ;
index index.html;
}
}
[root@localhost ~]# cd /etc/nginx/conf.d/http
[root@localhost http]# ll
total 4
-rw-r--r--. 1 root root 98 Apr 12 12:31 guangwang.conf
[root@localhost http]# cat guangwang.conf
server {
listen 80;
location / {
root /opt/static ;
index index.html;
}
}
#然后配置一下静态页面存放路径
[root@localhost ~]# mkdir -p /opt/static
[root@localhost http]# cd /opt/static
[root@localhost static]# ll
total 4
-rw-r--r--. 1 root root 29 Apr 12 13:21 index.html
[root@localhost static]# vi index.html

7.检查一下有没有报错

nginx -t
nginx -s reload

然后复制本机ip地址到浏览器,端口号默认是80