nginx安装

1.前置安装 sudo yum install yum-utils
2.在服务器上根目录创建一个/etc/yum.repos.d/nginx.repo文件,编辑如下:

[nginx-stable]    name=nginx stable repo    baseurl=http://nginx.org/packages/centos/$releasever/$basearch/    gpgcheck=1    enabled=1    gpgkey=https://nginx.org/keys/nginx_signing.key    module_hotfixes=true    [nginx-mainline]    name=nginx mainline repo    baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/    gpgcheck=1    enabled=0    gpgkey=https://nginx.org/keys/nginx_signing.key    module_hotfixes=true

3.

sudo yum install nginx

这样我们就完成了nginx的安装,这三步官网要求的,没有为什么;
以这样的方式我们安装nginx的配置在/etc/nginx/default.conf中配置

2.nginx配置

我们刚生成的nginx默认配置如下

user  nginx; //默认用户worker_processes  1;//cpu内核error_log  /var/log/nginx/error.log warn;//打印错误日志路径pid        /var/run/nginx.pid;//nginx运行启动后就会生成这个标示文件,记录 nginx主进程的 ID 号。events {//设置最大连接数    worker_connections  1024;}http {    include       /etc/nginx/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系统调用来传输文件。  sendfile系统调用在两个文件描述符之间直接传递数据(完全在内核中操作),从而避免了数据在内核缓冲区和用户缓冲区之间的拷贝,操作效率很高,被称之为零拷贝    keepalive_timeout  65;//默认连接时间   include /etc/nginx/conf.d/*.conf;//默认配置都包含在.conf文件中}

上面的最后一行我们也清楚了,那么我们看一看默认配置的详细注解:

server {    listen       80;//默认监听80端口    server_name  localhost;    access_log  /var/log/nginx/host.access.log  main;//打印日志位置    location / {        root   /usr/share/nginx/html;//配置访问文件夹        index  index.html index.htm;//配置访问路径    }}

部署

下载xshell和winscp,xshell是连接服务器的一种工具,winscp是向服务器上传文件,不过我遇到了一个问题;
首先我自己在阿里云服务器购买了一台服务器,然后按照上面的操作进行安装和搭建,但是我改了nginx上的配置文件并没有生效,由于我也是第一次接触nginx对这块不是很了解,所以我直接用localhost访问(我也真是一个nt,既然上传到服务器当然是用服务器访问了),但是还是一直停留在welcome to nginx!这就很邪门了,然后在网上各种查资料原来不是配置的问题,是我们在配置文件中写的端口指向80,但是阿里云需要设置安全组,设置完之后在网址上输入服务器地址才能看到我们更改之后的index.html,也算是历经挫折了,但是我把congf中的root指向指向了root文件夹下的index.html

 location / {        root   /root;//配置访问文件夹        index  index.html index.htm;//配置访问路径    }

再打开网页显示403这是什么鬼,没事我有百度,接着查百度才发现是权限问题,只需要更改配置中的user改成user nginx;然后重新启动一下nginx(只要改了配置文件都要重新启动),这样就可以正常的显示文件了;

常用的命令

systemctl start nginx //启动nginxsystemctl status nginx.service  //查看nginx的状态systemctl disable firewalld.service  //关闭防火墙ps -ef |grep nginx//查看进程history  !(序号)//使用命令的历史记录systemctl restart nginx.service//重启nginx