ubuntu nginx html文件,Ubuntu 14.04TLS Nginx搭建静态文件服务器

本文介绍如何使用Nginx配置静态文件服务器及PHP运行环境,包括监听端口、指定根目录、配置文件上传等功能,并解决常见错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Nginx搭建静态文件服务器

[官方教程]https://www.nginx.com/resources/admin-guide/serving-static-content/

打开Nginx的配置文件:/etc/nginx/sites-available/default,找到server,添加以下配置:

server {

# 监听来自所有网络上的80端口的请求

listen 0.0.0.0:8080;

# 这个server的根目录

root /usr/share/nginx/files;

# 下面的东西是需要自行添加的配置

location ~ \.(png|gif|jpg)$ {

root /usr/share/nginx/images; #这个将替换`server->root`配置

expires 1d;

index default.jpg;

}

# 上面就是需要添加的东西了

# 对于满足以 .png/.gif/.jpg 结尾的url请求,

# 将其根目录定义为 /usr/share/nginx/images

# 文件的有效期为一天

}

设置完之后通过命令:

sudo service nginx restart

重启Nginx后生效。

如果遇到启动失败,使用命令:

nginx -t

查看错误信息

Nginx搭建PHP运行环境

PHP运行环境安装一个php5-fpm包即可:

sudo apt-get install php5-fpm

去掉Nginx配置文件里关于php5-fpm的三行(A/B/C,当然也要包含X/Y两行)注释:

# 同样是在server的区块里

location ~ .*\.php$ { # X

# fastcgi_split_path_info ^(.+\.php)(/.+)$;

# # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

# # With php5-cgi alone:

# fastcgi_pass 127.0.0.1:9000;

# # With php5-fpm:

fastcgi_pass unix:/var/run/php5-fpm.sock; # A

fastcgi_index index.php; # B

include fastcgi_params; # C

} # Y

关于A行可以先执行命令:

vim /etc/php5/fpm/pool.d/www.conf

查找到:

listen = /var/run/php5-fpm.sock

可以得知我们的配置是正确的。

使用PHP上传文件

配置"php.ini"文件

sudo vim /etc/php5/fpm/php.ini

设置:

file_uploads = On

创建HTML表单

Select image to upload:

注意:

确保表单的method为post

enctype为multipart/form-data确保可以接收文件

创建上传的PHP脚本

$target_dir = "uploads/";

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

$uploadOk = 1;

$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check if image file is a actual image or fake image

if(isset($_POST["submit"])) {

$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);

if($check !== false) {

echo "File is an image - " . $check["mime"] . ".";

$uploadOk = 1;

} else {

echo "File is not an image.";

$uploadOk = 0;

}

}

?>

解释:

$target_dir = "uploads/" 表示文件存放的目录

$target_file 表示文件上传的路径

$uploadOk=1 暂未使用

$imageFileType 包含了文件的扩展名

接着就是判断文件是否是图片

检查文件是否已存在

// Check if file already exists

if (file_exists($target_file)) {

echo "Sorry, file already exists.";

$uploadOk = 0;

}

限制文件大小

// Check file size

if ($_FILES["fileToUpload"]["size"] > 500000) {

echo "Sorry, your file is too large.";

$uploadOk = 0;

}

限制文件类型

// Allow certain file formats

if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"

&& $imageFileType != "gif" ) {

echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";

$uploadOk = 0;

}

完整的代码

$target_dir = "uploads/";

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

$uploadOk = 1;

$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check if image file is a actual image or fake image

if(isset($_POST["submit"])) {

$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);

if($check !== false) {

echo "File is an image - " . $check["mime"] . ".";

$uploadOk = 1;

} else {

echo "File is not an image.";

$uploadOk = 0;

}

}

// Check if file already exists

if (file_exists($target_file)) {

echo "Sorry, file already exists.";

$uploadOk = 0;

}

// Check file size

if ($_FILES["fileToUpload"]["size"] > 500000) {

echo "Sorry, your file is too large.";

$uploadOk = 0;

}

// Allow certain file formats

if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"

&& $imageFileType != "gif" ) {

echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";

$uploadOk = 0;

}

// Check if $uploadOk is set to 0 by an error

if ($uploadOk == 0) {

echo "Sorry, your file was not uploaded.";

// if everything is ok, try to upload file

} else {

if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {

echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";

} else {

echo "Sorry, there was an error uploading your file.";

}

}

?>

遇到php报500 Server internal error错误怎么办?

在对应的php文件中增加:

ini_set('display_errors', 1);

在.htaccess文件中(如果没有该文件则手动创建一个空文件)添加:

php_flag display_errors 1

遇到php报move_uploaded_file:failed to open stream: Permission denied in /usr/share/nginx/images 怎么办?

在对应的php文件中增加:

echo exec('whoami');

比如输出的是:

www-data

执行以下语句赋予权限(语句中的www-data应该对应whoami的输出值):

sudo chown www-data /usr/share/nginx/images

sudo chmod 0755 /usr/share/nginx/images

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值