LNMP搭建

一.準備四臺主機

nginx主機: 172.16.10.17/16   192.168.10.17/24

php主機: 192.168.10.27/24

mysql主機: 192.168.10.87/24

CA簽證主機: 172.16.10.77

二.分別在三臺主機上安裝相關軟件包

nginx主機:nginx

php主機:php-fpm   php-mysql  php-mbstring

mysql主機:mariadb

三.搭建LNMP

1.配置nginx

1)編輯配置文件 

vim /etc/nginx/nginx.conf

  添加下列內容,

server {

        listen        80 default_server;

        server_name     www1.douhua.com;

      root         /data/html/www1;

    }

    server {

        listen         80 ;

             server_name      www2.douhua.com;

        root          /data/html/www2;

    }


2)創建網頁文件目錄,主頁文件

mkdir -pv /data/html/www{1,2}

分別在兩個目錄中創建index.html文件

cd /data/html/www1

編輯 vim index.html

添加一行  <h1> www1 page </h1>

www2目錄中同理

3)檢測語法錯誤並開啓nginx服務,並使用瀏覽器驗證

檢測語法   nginx   -t   

開啓服務   nginx

2.配置nginx與php-fpm的連接 

1)編輯nginx配置文件,添加location,配置與php-fpm的連接相關

location ~ \.php{

                root            www1;    

                fastcgi_pass    192.168.10.27:9000;   

              fastcgi_index   index.php;

                fastcgi_param   SCRIPT_FILENAME    /data/php/www1$fastcgi_script_name;

              include         fastcgi_params;

      }

上述配置文件表示:

root 根目錄

fastcgi_pass    php-fpm監聽的地址與端口

              fastcgi_index   主頁文件

                fastcgi_param   nginx服務器上文件映射至php-fpm服務器上的文件路徑

              include         包含fastcgi_params中的配置

虛擬主機2配置同

2)在虛擬資源目錄中添加index.php文件

touch  /data/html/www1/index.php

3)在php-fpm主機上編輯配置文件

vim /etc/php-fpm.d/www.conf

更改監聽地址與允許的客戶端

listen = 192.168.10.27:9000

listen.allowed_clients = 192.168.10.17

4)編輯測試文件

a)創建目錄

mkdir -pv /data/php/www{1,2}

b)進入該目錄,並編輯測試文件

cd /data/php/www1

vim index.php

內容爲:

<h1> Test 1 </h1>


<?php

phpinfo();

?>

5)重啓php-fpm服務,並打開瀏覽器檢測

http://172.16.10.17/index.php

3.配置php-fpm與mysql的連接

1)在mysql主機上啓動mariadb服務

2)登錄mysql,創建測試所需用戶


MariaDB [(none)]> GRANT ALL ON testdb.* TO 'testuser'@'192.168.%.%' IDENTIFIED BY 'passwd';


3)在php-fpm主機上修改測試文件index.php內容爲:

<h1>Test php-mysql </h1>


<?php

        $conn = mysql_connect('192.168.10.87','testuser','passwd');

        if ($conn)

                echo "ok";

else

                echo "false";

?>

4)打開瀏覽器測試

http://172.16.10.17/index.php


四.在虛擬主機www1.douhua.com上部署wordpress

1)下載並解壓wordpress包

2)將解壓後的包拷貝至php-fpm主機上的/data/php/www1目錄下

3)拷貝配置文件並編輯

cp wp-config-sample.php  wp-config.php

編輯配置文件

vim  wp-config.php

define('DB_NAME', 'wpdb'); /** WordPress數據庫的名稱 */

define('DB_USER', 'wpuser'); /** MySQL數據庫用戶名 */

define('DB_PASSWORD', 'passwd'); /** MySQL數據庫密碼 */

define('DB_HOST', '192.168.10.87'); /** MySQL主機 */

4)在mysql主機上創建用戶和數據庫

MariaDB [(none)]> GRANT ALL ON wpdb.* TO 'wpuser'@'192.168.%.%' IDENTIFIED BY 'passwd';

MariaDB [(none)]> CREATE DATABASE wpdb;

5)在nginx主機上做相關配置

拷貝解壓後的wordpress文件至/data/html/www1目錄中

cp -r /root/wordpress   /data/html/www1

6)打開瀏覽器驗證(事先配置好DNS解析,或者直接輸入ip地址訪問)

http://www1.douhua.com/wordpress/index.php   

五.在虛擬主機www2.douhua.com上部署phpMyadmin

1)下載並解壓phpMyadmin包

2)將解壓後的包拷貝至php-fpm主機上的/data/php/www2目錄下

3)生成隨機數並以便添加至phpMyadmin配置文件中

4)創建軟鏈接便於管理

cd  /data/php/www2

ln -sv  phpMyAdmin-4.4.14.1-all-languages pma

5)修改phpMyadmin配置文件

cd pma

cp config.sample.inc.php config.inc.php

vim config.inc.php

添加隨機數至$cfg['blowfish_secret'] = '';中的單引號中

$cfg['blowfish_secret'] = 'ZmAz04rjIHxjgw';

並將服務器地址更改爲mysql主機地址

$cfg['Servers'][$i]['host'] = '192.168.10.87';

6)創建php的session目錄

mkdir   /var/lib/php/session

chown apache.apache  /var/lib/php/session   此處用戶與php-fpm配置文件中的用戶一致

7)在nginx主機上配置

拷貝解壓後的phpMyadmin目錄至/data/html/www2中並改名爲pma

8)登錄瀏覽器查看

http://www2.douhua.com/pma


六.爲www2.douhua.com主機提供https服務

1)創建證書目錄,及證書

創建目錄:mkdir /etc/nginx/ssl

cd /etc/nginx/ssl

生成私鑰

(umask 066;openssl genrsa -out nginx.key 2048)

生成申請證書:

openssl req -new -key nginx.key -out nginx.csr

在另一主機上籤證,將簽完後的證書拷貝至此目錄中

2)編輯配置文件:

vim /etc/nginx/nginx.conf

在www2.douhua.com中添加配置

listen           443 ssl;  

        ssl_certificate         /etc/nginx/ssl/nginx.crt;

        ssl_certificate_key     /etc/nginx/ssl/nginx.key;

3)重載配置文件

nginx   -t 

nginx   -s  reload

4)打開瀏覽器驗證(瀏覽器須事先將根證書導入並添加信任)

https://www2.douhua.com


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章