CentOS6 (64-bit) + Nginx build static file server
Run the following command to open the Nginx configuration file:
Sudo vim/etc/nginx/conf. d/default. conf
Change the configuration:
Server {...... # the following configuration items need to be added by yourself. location ~ \. (Png | gif | jpg | jpeg) $ {root/usr/share/nginx/images; # This will replace the 'server-> root' configuration # expires 1d; index default.jpg ;} # The above is what needs to be added # to meet the requirements. png /. gif /. url request at the end of jpg, # define its root directory as/usr/share/nginx/images # The file is valid for one day (you can cancel the comment if necessary )............}
After setting, run the following command:
Sudo service nginx restart restarts Nginx and takes effect.
If the startup fails, run the following command:
Nginx-t
View error information
Build a PHP runtime environment using Nginx
Install a PHP-fpm package in the php runtime environment:
Sudo yum install php-fpm
Run the following command to open the corresponding configuration file:
Sudo vim/etc/nginx/conf. d/default. conf
Change server_name:
Server_name localhost;
Change the first default localtion:
location / { try_files $uri $uri=404;}
Change 404:
error_page 404 /404.html;
Run the following command:
Vim/etc/php-fpm.d/www. conf
Find and remember the listen content (the following 127.0.0.1: 9000 is my local settings ):
Listen = 127.0.0.1: 9000
Change the PHP configuration in the Nginx configuration file to the following:
# Location ~ in the server block ~ \. Php $ {try_files $ uri = 404; fastcgi_pass 127.0.0.1: 9000; # The content fastcgi_index index found above. php; fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name; include fastcgi_params ;}
We can see that our configuration is correct.
Use PHP to upload a file and configure the "php. ini" file
Sudo vim/etc/php. ini
If you do not know where the php. ini file is, run the following command:
Php-I | grep "Loaded Configuration File"
Settings:
File_uploads = On
Restart the PHP service:
Sudo service php-fpm restart
Create an HTML form upload. php in/usr/share/nginx:
Note:
- Make sure the Form method is post
- Enctype is multipart/form-data to ensure files can be received
Create an uploaded PHP script
Explanation:
- $ Target_dir = "images/" indicates the directory where the file is stored.
- $ Target_file indicates the file upload path.
- $ UploadOk = 1 temporarily unavailable
- $ ImageFileType contains the file extension
- The next step is to determine whether the file is an image.
Check whether the file already exists
// Check if file already existsif (file_exists($target_file)) { echo "Sorry, file already exists."; $uploadOk = 0;}
Limit file size
// Check file sizeif ($_FILES["fileToUpload"]["size"] > 500000) { echo "Sorry, your file is too large."; $uploadOk = 0;}
Restrict file types
// Allow certain file formatsif($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"&& $imageFileType != "gif" ) { echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; $uploadOk = 0;}
Complete Code
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."; }}?>
What should I do if a 500 Server internal error is reported in php?
Add the following to the corresponding php file:
Ini_set ('display _ errors ', 1 );
In the. htaccess file (if the file does not exist, manually create an empty file) Add:
Php_flag display_errors 1
What if move_uploaded_file: failed to open stream: Permission denied in/usr/share/nginx/images is reported in php?
Add the following to the corresponding php file:
Echo exec ('whoam ');
For example, the output is:
Www-data
Execute the following statement to grant permissions (www-data in the statement should correspond to the output value of whoami ):
Sudo chown www-data/usr/share/nginx/imagessudo chmod 0755/usr/share/nginx/images
[Reference] https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-centos-7