寫教學的最大目的是教會未來的自己

Nginx 安裝、設定教學

這篇教學儲了nginx的安裝設定之外 還包括 apache 的關閉等等….

使用環境適 Ubuntu

參考資料

http://mental.we8log.com/mental/entry/240/my_weblog

https://www.digitalocean.com/community/articles/how-to-configure-single-and-multiple-wordpress-site-settings-with-nginx

另外感謝 Licson Lee 狄信祐 在臉書上的教導

 

先轉成root

#sudo su

安裝篇

先把需要的通通安裝….似乎不需要那麼多 XD,管她的 沒漏裝就好

apt-get install nginx php5-cgi php5-cli php5-fpm php-doc

 

如果之前跑apache 的先關掉吧

#/etc/init.d/apache2 stop

在來關閉在開機啟動的設定 (要砍掉也OK啦 自己去找指令)

安裝設定軟體

#apt-get install sysv-rc-conf

#sysv-rc-conf

如下圖,用空白鍵 把底下apache的2 3 4 5 清空,之後按q 離開

sysv-rc-conf

 

設定篇

nginx 的設定 和 很多程式語言一樣 可以使用 include ,所以可以先把通用的設定寫好,需要時include 就可以了

 

進入 nginx  資料夾

#cd /etc/nginx

建立共通設定檔資料夾

#mkdir global

#cd global

#vim common.conf

放入下面內容

——————————————————————————————————————————

# Global configuration file.
# ESSENTIAL : Configure Nginx Listening Port
listen 80;
# ESSENTIAL : Default file to serve. If the first file isn't found, 
index index.php index.html index.htm;
# ESSENTIAL : no favicon logs
location = /favicon.ico {
    log_not_found off;
    access_log off;
}
# ESSENTIAL : robots.txt
location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
}
# ESSENTIAL : Configure 404 Pages
error_page 404 /404.html;
# ESSENTIAL : Configure 50x Pages
error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/www;
    }
# SECURITY : Deny all attempts to access hidden files .abcde
location ~ /\. {
    deny all;
}
# PERFORMANCE : Set expires headers for static files and turn off logging.
location ~* ^.+\.(js|css|swf|xml|txt|ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
    access_log off; log_not_found off; expires 30d;
}

——————————————————————————————————————————

#vim wordpress.conf

放入下面內容

——————————————————————————————————————————

# WORDPRESS : Rewrite rules, sends everything through index.php and keeps the appended query string intact
location / {
    try_files $uri $uri/ /index.php?q=$uri&$args;
}

# SECURITY : Deny all attempts to access PHP Files in the uploads directory
location ~* /(?:uploads|files)/.*\.php$ {
    deny all;
}
# REQUIREMENTS : Enable PHP Support
location ~ \.php$ {
    # SECURITY : Zero day Exploit Protection
    try_files $uri =404;
    # ENABLE : Enable PHP, listen fpm sock
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass localhost:9000;
    fastcgi_index index.php;
    include fastcgi_params;
}
# PLUGINS : Enable Rewrite Rules for Yoast SEO SiteMap
rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;

#Yeah! you did it.

 ——————————————————————————————————————————

 

之後開始正式設定

設定檔案在sites-available,每個網站分開檔案設定 或是 放在一起都OK,只要每個網站都用server {…} 包起來就OK

vim demo

—————————————

server {
    server_name demo.com;
    rewrite ^/(.*)$ http://www.demo.com/$1 permanent;
}
server {
    server_name www.demo.com;
    root /home/demouser/sitedir;
    access_log /var/log/nginx/www.demo.com.access.log;
    error_log /var/log/nginx/www.demo.com.error.log;
    include global/common.conf;
    include global/wordpress.conf;
}

server {
    server_name www.demo1.com;
    root /home/demouser/sitedir1;
    access_log /var/log/nginx/www.demo1.com.access.log;
    error_log /var/log/nginx/www.demo1.com.error.log;
    include global/common.conf;
    include global/wordpress.conf;
    include global/multisite.conf;
}
# More server blocks....

 ——————————————————————————————————————————

sudo ln -s /etc/nginx/sites-available/demo /etc/nginx/sites-enabled/demo
sudo service nginx reload; 

收工

Post to Twitter Post to Plurk Post to Facebook Send Gmail

One Response to Nginx 安裝、設定教學

發表迴響

Copyright © 2024. All Rights Reserved.

歡迎光臨
初音