Tag Archives: blog

架設 NPMF 系統的 WORDPRESS 網站 《其之二》

準備這一篇的時候,發現架設時搜尋到的一些網頁還沒有關閉,其中這個網頁 [1] 清晰地解說了完整步驟,那麼我就省時間直接翻譯吧。


LEMP 是一個作業系統和開源軟體堆疊的組合。LEMP 此縮寫是來自 Linux、Nginx HTTP 伺服器、MySQL/MariaDB 資料庫、PHP/Perl/Python 的第一個英文字母。我們已經為您展示了如何在很多平台上安裝 LAMP 了。

安裝 Nginx

Nginx (唸 engine-x) 是一個由 Igor Sysoev 寫的免費、開源、高效率的 HTTP 伺服器和反向代理伺服器,也是一個 IMAP/POP3 代理伺服器。

首先以 root 使用者登入進行安裝:

$ su

注意:如果您之前經已安裝 apache 或其他網頁伺服器,就要先把它們移除或停用。

# systemctl disable httpd.service # systemctl stop httpd.service

要安裝 Nginx,請輸入在您的終端機裏輸入以下命令:

# yum install nginx -y

啟用 Nginx 服務以在每次啟動時自動開始運行:

# systemctl enable nginx.service

使用命令開始運行 Nginx 服務:

# systemctl start nginx.service

測試 Nginx

打開你的網頁瀏覽器後,瀏覽到 http://ip-位址 或 http://localhost/ 。您將會看到一個和以下類似的畫面。

nginx test page

 

設置 Nginx

用任何編輯器打開檔案 /etc/nginx/nginx.conf

# vi /etc/nginx/nginx.conf

把 worker_processes (即是您系統的處理器數目)。要找出有幾個處理器,可使用 “lscpu”。我的情況是 “1″,我就把它設定成 “1″:

worker_processes 1;

往下捲動此設定檔案,設置伺服器名稱和 PHP scripts:

#
# The default server
#
server {
 listen 80;
 server_name localhost;

 #charset koi8-r;

 #access_log logs/host.access.log main;

 location / {
 root /usr/share/nginx/html;
 index index.html index.htm;
 }

 error_page 404 /404.html;
 location = /404.html {
 root /usr/share/nginx/html;
 }

 # redirect server error pages to the static page /50x.html
 #
 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
 root /usr/share/nginx/html;
 }

 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 #
 #location ~ \.php$ {
 # proxy_pass http://127.0.0.1;
 #}

 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 ## Uncomment or Add the following lines
 location ~ \.php$ {
     root /usr/share/nginx/html; 
     try_files $uri =404; 
     fastcgi_split_path_info ^(.+\.php)(/.+)$; 
     fastcgi_pass 127.0.0.1:9000; 
     fastcgi_index index.php; 
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
     include fastcgi_params;
 }

 # deny access to .htaccess files, if Apache's document root
 # concurs with nginx's one
 #
 #location ~ /\.ht {
 # deny all;
 #}
}

儲存後關閉檔案,重新運行 Nginx 服務:

# systemctl restart nginx.service

安裝 MariaDB

MariaDB 是一個 MySQL 替代,是一個健全、可擴展、可靠的 SQL 伺服器,帶有很豐富的新技術。Fedora 19 預設的資料庫是 MariaDB。

使用以下命令進行安裝:

# yum install mysql mysql-server -y

以以下命令在系統始動時啟用 MySQL 服務:

# systemctl enable mysqld.service

也使用此命令運行 MySQL 服務:

# systemctl start mysqld.service

設定 MySQL root 密碼

預設 MySQL root 密碼是空置的,因此要防止對 MySQL 的非法進入,我們設置一個 root 使用者密碼吧:

# mysql_service_installation
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
 SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
 
In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here.
 
Enter current password for root (enter for none): 
OK, successfully used password, moving on...
 
Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorization.
 
You already have a root password set, so you can safely answer 'n'.
 
Change the root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!
 
By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a production environment.
 
Remove anonymous users? [Y/n] 
 ... Success!
 
Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network.
 
Disallow root login remotely? [Y/n]
 ... Success!
 
By default, MariaDB comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.
 
Remove test database and access to it? [Y/n] 
 - Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist...
Failed! Not critical, keep moving...
Removing privileges on test database...
 ... Success!
 
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
 
Reload privilege tables now? [Y/n] 
 ... Success!
 
Cleaning up...
 
All done! If you've completed all of the above steps, your MariaDB installation should now be secure.
 
Thanks for using MariaDB!

安裝 PHP

PHP (PHP: 超文件預處理器 的遞歸縮寫) 是一個廣泛使用的開源通用腳本語言,尤其適用於網頁開發和內嵌到 HTML 裏。

以以下命令安裝 PHP:

# yum install php php-fpm php-mysql php-common -y

啟用和運行 php-fpm 服務:

# systemctl enable php-fpm.service
# systemctl start php-fpm.service

設置 PHP

在任何編輯器打開 /etc/php.ini:

# vi /etc/php.ini

尋找有 cgi.fix_pathinfo 的一行,取消批注後將數值由 1 更改到 0 (零):

[...]
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=0
[...]

打開檔案 /etc/php-fpm.d/www.conf

# vi /etc/php-fpm.d/www.conf

之後將使用者和群組數值由 apache 更改到 nginx

[...]
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
; RPM: apache Choosed to be able to access some dir as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx
[...]

儲存後關閉檔案,重新運行 php-fpm 服務:

# systemctl restart php-fpm.service

測試 PHP

在 Apache 文檔根文件夾裏,建立一個樣本 “testphp.php” 檔案:

# vi /usr/share/nginx/html/testphp.php

附加如下一行:

<?php
    phpinfo();
?>

儲存後關閉檔案,重新運行 Nginx 服務:

# systemctl restart nginx.service

瀏覽到 http://伺服器 IP 位址/testphp.php。頁面將會顯示有關 PHP 有關的詳情,像版本號碼、編譯日期、命令等等。

phpinfo-Mozilla-Firefox_002

[1] - Install LEMP Server (Nginx, MariaDB, And PHP) On Fedora 20/19