CentOS 6(64-bit) + Nginx搭建靜態檔案伺服器

來源:互聯網
上載者:User

Nginx搭建靜態檔案伺服器

使用命令開啟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

查看錯誤資訊

Nginx搭建PHP運行環境

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;}

可以得知我們的配置是正確的。

使用PHP上傳檔案

配置"php.ini"檔案

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:

 

注意:

  • 確保表單的 method為 post
  • enctype為 multipart/form-data確保可以接收檔案

建立上傳的PHP指令碼

 

解釋:

  • $target_dir = "images/"表示檔案存放的目錄
  • $target_file表示檔案上傳的路徑
  • $uploadOk=1暫未使用
  • $imageFileType包含了檔案的副檔名
  • 接著就是判斷檔案是否是圖片

檢查檔案是否已存在

// 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

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.