CentOS安装nginx

一、部署服务器环境
      nginx:轻量级、高性能的HTTP及反向代理服务器,占用内存少,并发能力强,相比老牌的apache作为web服务器,性能更加卓越。

//使用yum命令 安装nginx
#nginx yum install nginx -y   

 
CentOS 安装nginx yum install nginx 时报错:No package nginx available.

解决办法:

先安装epel:
#yum install epel-release

然后再安装nginx
#nginx yum install nginx-y 

安装成功,查看安装的目录
CentOS安装nginx

// 安装完毕,启动nginx
#nginx

CentOS安装nginx

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

根据Nginx配置文件查看配置的端口(本文中使用的是80端口),然后根据端口查看端口占用情况

# netstat -ntlp|grep 80

kill掉相关占用的进程。

重新启动nginx

就可以在浏览器中输入你的服务器ip地址80端口,可以看到nginx启动成功的页面了

CentOS安装nginx

 

默认页面文件在下
/usr/share/nginx/html

配置文件
/etc/nginx/nginx.conf

 

配置服务器访问路径
     nginx服务器默认的服务目录,即/usr/share/nginx/html目录,可以在/etc/nginx/nginx.conf配置文件中配置你自己的项目目录,并让nginx正确访问。

     修改/etc/nginx/nginx.conf文件配置:

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        # 修改root默认目录  /usr/share/nginx/html;
        #root         /website;

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

        location / {
            # 修改nginx在path为'/’下的访问目录  
            root   /website;
            index  index.html index.htm;
        }

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

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

 

然后重启 Nginx
nginx -s reload
 重启后,现在我们应该已经可以使用我们的静态服务器了,现在新建一个静态文件,查看服务是否运行正常

 

新建你的项目目录,创建一个index.html
    现在就开始创建你的项目目录,即/website

// 创建你的项目目录
mkdir    /website

// 并在/website目录下创建一个html文件让nginx访问
touch index.html
复制代码
// index.html代码为
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>hello world</title>
</head>
<body>
Hello world!
</body>
</html>