0%

Centos安装nginx与简单配置

Centos安装nginx记录如下:

为了追加 nginx 的 yum 仓库,需要创建一个文件 /etc/yum.repos.d/nginx.repo,并将下面的其中一个内容复制进去:

CentOS:

1
2
3
4
5
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

下面直接执行如下指令即可自动安装好Nginx:

1
yum install nginx -y

安装完成,下面直接就可以启动Nginx了:

1
/etc/init.d/nginx start

现在Nginx已经启动了,直接访问服务器就能看到Nginx欢迎页面了的。

如果还无法访问,则需配置一下Linux防火墙。

1
2
3
4
5
iptables -I INPUT 5 -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT

service iptables save

service iptables restart

Nginx的命令以及配置文件位置:

1
2
3
4
5
/etc/init.d/nginx start # 启动Nginx服务

/etc/init.d/nginx stop # 停止Nginx服务

/etc/nginx/nginx.conf # Nginx配置文件位置

至此,Nginx已经全部安装完成。

接下来我们看看nginx的基本配置,vi /etc/nginx/nginx.conf 看到最后一行

1
include /etc/nginx/conf.d/*.conf;

表示包含/etc/nginx/conf.d/下的所有conf文件
然后我们cd到/etc/nginx/conf.d/目录下ls一下看看

1
default.conf example_ssl.conf

有两个conf文件 一个默认的和一个example,我们用vi打开default.conf看看

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
server {
listen 80;
server_name localhost;

#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

我们可以看到服务监听的端口是80 ,server_name 是localhost,网站目录root为/usr/share/nginx/html,默认访问文件index index.html index.htm以及错误访问文件50x.html路径的配置。
你可以修改这些配置,比如修改监听端口、server_name、网站目录等。或者新建一个conf文件按照这个配置书写即可。
记得修改以后重启一下nginx服务。
OK 到这里nginx安装以及基本配置就完成了。

 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!