使用命令打开Nginx配置文件:
sudo vim /etc/nginx/conf.d/default.conf
将配置改为:
server { ...... ...... # 下面的东西是需要自行添加的配置 location ~ \.(png|gif|jpg|jpeg)$ { 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
查看错误信息
PHP运行环境安装一个 php-fpm包即可:
sudo yum install php-fpm
执行命令打开对应的配置文件:
sudo vim /etc/nginx/conf.d/default.conf
将server_name改为:
server_name localhost;
将第一个默认的 localtion改为:
location / { try_files $uri $uri=404;}
将 404 改为:
error_page 404 /404.html;
执行命令:
vim /etc/php-fpm.d/www.conf
查找并记住 listen内容(以下127.0.0.1:9000是我本机的设置):
listen = 127.0.0.1:9000
去掉Nginx配置文件里的PHP的配置改为如下:
# 同样是在server的区块里location ~ \.php$ { try_files $uri = 404; fastcgi_pass 127.0.0.1:9000; # 就是上面查找到的127.0.0.1:9000这个内容 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params;}
可以得知我们的配置是正确的。
sudo vim /etc/php.ini
如果不知道php.ini文件在哪里,请执行命令:
php -i | grep "Loaded Configuration File"
设置:
file_uploads = On
重启PHP服务:
sudo service php-fpm restart
在 /usr/share/nginx中创建HTML表单 upload.php:
<!DOCTYPE html><html><body><form action="upload.php" method="post" enctype="multipart/form-data"> Select image to upload: <input type="file" name="fileToUpload" id="fileToUpload"> <input type="submit" value="Upload Image" name="submit"></form></body></html>
注意:
<?php $target_dir = "images/"; $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 existsif (file_exists($target_file)) { echo "Sorry, file already exists."; $uploadOk = 0;}
限制文件大小
// Check file sizeif ($_FILES["fileToUpload"]["size"] > 500000) { echo "Sorry, your file is too large."; $uploadOk = 0;}
限制文件类型
// Allow certain file formatsif($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"&& $imageFileType != "gif" ) { echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; $uploadOk = 0;}
完整的代码
500000) { echo "Sorry, your file is too large."; $uploadOk = 0;}// Allow certain file formatsif($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 errorif ($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/imagessudo chmod 0755 /usr/share/nginx/images
[参考文章] https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-centos-7