本博结构
一、 介绍更改网站根目录之前的基本环境
二、 介绍更改步骤
- 更改配置文件(更改后重新加载)
- 403 Forbidden错误和File not found错误
- 如何解决错误
以下为正文,以及,不愿意看基本环境的直接跳到 二
看步骤和问题
安装完LNMP之后,网站的根目录是/usr/share/nginx/html
这个用起来非常不方便, for instance : 直接输入cd
命令,打开的是~
,要打开网站根目录,则需要输入一长串 /usr/share/nginx/html
不爽,时间就是生命啊,所以博主将网站根目录改为 ~
,具体的绝对路径就是 /root
(可能有不一样的,根据自己的实际情况来)。
接下来就介绍到底如何更改。
一、基础环境
##1. 服务器环境
- CentOS 6.7
- Nginx/1.0.15
##2. Nginx初始配置文件
我的nginx配置文件位置 /etc/nginx/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost; #以后要绑定域名的时候改这里
index index.php index.html index.htm;
location / {
root /urs/share/nginx/hmtl; #这里是文件的根目录
index index.php 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 html; #这里是出50*错之后的展示页面位置
}
location ~ \.php$ {
root /usr/share/nginx/html; #这里也是文件的根目录
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
其实nginx配置文件的基本款挺易懂的,还是认真看看比较好,否则该错误就是一头雾水了。
可以看到当前服务器的根目录是 /usr/share/nginx/html
,用起来挺不方便的:比如你输入cd
命令,打开的是~
而不是/usr/share/nginx/html
。 所以我们想把根目录定位到~
,在我的服务器上~
是 /root
。
要注意,更改完配置之后,需要重载nginx的配置文件(用这种方法不用停服)
service nginx reload
二、 更改方法
##1. 更改Nginx配置文件
将我的网站根目录改为~
也就是/root
。
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
index index.php index.html index.htm;
location / {
root /root; #这里改了
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
# root html;
}
location ~ \.php$ {
root /root; #这里改了
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
记得重载配置!service nginx reload
##2. 可能会出现的问题
改完之后可能会出现如下两个问题:
- 403 Forbidden
- File not found
这两个问题都源于 /root
(更改后的网站根目录)的权限。
##3. 问题解答
需要给根目录加上可写权限
###3.1 未更改前的文件权限
3.2 更改权限
chmod a+w .
3.3 更改后的权限
##4. 成功 !