1. 程式人生 > >原創:LNMP架構部署個人博客網站 禁止轉載復制

原創:LNMP架構部署個人博客網站 禁止轉載復制

mcr sysv 返回 瀏覽器 you 首頁 gid 軟件程序 esp

nginx編譯安裝步驟

①. 檢查軟件安裝的系統環境

cat /etc/redhat-release

uname -r

②. 安裝nginx的依賴包(pcre-devel openssl-devel)---假設不進行安裝

yum install -y pcre-devel openssl-devel

③. 下載nginx軟件---1.10.2 復制鏈接地址(統一位置進行下載)

mkdir -p /home/oldboy/tools

cd /home/oldboy/tools

wget -q http://nginx.org/download/nginx-1.10.3.tar.gz

說明:軟件很小,用心查看一下

④. 編譯安裝軟件步驟

a. 解壓要編譯安裝的軟件(解壓軟件---配置(./configure)---做菜(編譯 make)---上菜(安裝 make install))

tar xf nginx-1.10.2.tar.gz

ps -ef |grep sshd

id www #查看用戶

uid=666(www) gid=666(www) groups=666(www)

userdel -r www #刪除虛擬用戶

useradd www -s /sbin.nologin -M #創建虛擬用戶

id www

cd nginx-1.10.2

ls (裏面的內容就是源代碼(config readme安裝說明)---默認編譯會安裝到/usr/local目錄)

useradd -s /sbin/nologin -M www <--- 創建web服務程序www用戶

./configure --prefix=/application/nginx-1.10.2 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module #配置文件

echo $?

(編譯參數說明後續補充說明)

make 編譯

echo $?

make install 編譯安裝

軟件編譯過程中,利用返回值,判斷操作是否正確

echo $?

cd /application/

ln -s /application/nginx-1.10.2 /application/nginx <--- 安裝完成一個軟件要做一個軟鏈接

b. 啟動nginx軟件程序進行測試

/application/nginx/sbin/nginx

lsof -i:80

netstat -lntup|grep 80 查看80端口

瀏覽器訪問 10.0.0.8:80

至此軟件安裝完畢:

04:

③. nginx軟件使用過程中疑難雜癥(參考教案說明)

· Nginx啟動的疑難雜癥匯總

④. nginx軟件排查問題三部曲說明

a 在客戶端上ping服務器端IP,檢查鏈路是否通暢

b 在客戶端上telnet服務器端IP、端口,檢查鏈路訪問是否通暢

c 在客戶端上wget檢測模擬頁面訪問是否正常

## nginx命令說明

# nginx 啟動方法

[[email protected] application]# /application/nginx/sbin/nginx

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

nginx: [emerg] still could not bind()

[[email protected] application]#

# nginx 停止方法

[[email protected] application]# /application/nginx/sbin/nginx -s stop

[[email protected] application]# ps -ef|grep nginx

root 4283 1773 0 21:52 pts/0 00:00:00 grep --color=auto nginx

[[email protected] application]# netstat -lntup|grep 80

[[email protected] application]# /application/nginx/sbin/nginx

[[email protected] application]#

# nginx 重啟方法(平滑)

[[email protected] application]# /application/nginx/sbin/nginx -s reload

# nginx 配置文件語法檢查

[[email protected] application]# /application/nginx/sbin/nginx -t

nginx: the configuration file /application/nginx-1.10.3/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.10.3/conf/nginx.conf test is successful

mysql軟件

mysql軟件下載地址信息: www.mysql.com mirrors.sohu.com

mysql軟件下載完畢後,查看mysql解壓後目錄文件大小

講解安裝mysql步驟

1) 下載解壓mysql二進制軟件包

tar xf mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz

#解壓數據庫壓縮包

2) 添加mysql程序運行時的管理用戶

useradd -s /sbin/nologin -M mysql

#創建虛擬用戶

3) 將mysql解壓後的程序包搬家到程序目錄下,並進行重命名

mkdir -p /application/

#創建程序目錄

mv /server/tools/mysql-5.6.34-*-x86_64 /application/mysql-5.6.34

#將程序搬遷

4) 為mysql服務程序創建軟鏈接

ln -s /application/mysql-5.6.34/ /application/mysql

#創建軟連接

5) 讓mysql用戶管理 /application/mysql/

chown -R mysql.mysql /application/mysql/data

## 用虛擬用戶管理

6) 初始化數據庫

#1. 確認軟件安裝在哪裏

#2. 確認數據存放在哪裏

#3. 確認MySQL使用的用戶誰?

/application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/application/mysql/data --user=mysql

==================================================================================================

下面是解釋

To start mysqld at boot time you have to copy

support-files/mysql.server to the right place for your system

mysql啟動腳本 默認放在support-files/mysql.server

記得給MySQL設置個密碼

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !

To do so, start the server, then issue the following commands:

創建密碼的代碼

/application/mysql/bin/mysqladmin -u root password ‘new-password‘

遠程創建密碼

/application/mysql/bin/mysqladmin -u root -h web01 password ‘new-password‘

==================================================================================================

7) 復制啟動腳本 授權

cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld

chmod +x /etc/init.d/mysqld

8) 修改啟動腳本 和 mysql命令中的路徑

sed -i ‘s#/usr/local/mysql#/application/mysql#g‘ /application/mysql/bin/mysqld_safe /etc/init.d/mysqld

#將默認的東西改為自己的 將後面兩個配置文件的全部都`修改

9) 復制默認的配置文件

\cp /application/mysql/support-files/my-default.cnf /etc/my.cnf #復制

/etc/init.d/mysqld start #啟動

至此,mysql數據庫安裝與啟動完成;可以檢測進程和服務端口,確認mysql服務是否啟動正確

講解管理mysql數據庫

1) 進入與退出mysql數據庫方法:

/application/mysql/bin/mysql

=============================================

擴展知識:登錄數據庫命令簡化方法

echo ‘export PATH=/application/mysql/bin:$PATH‘ >>/etc/profile

source /etc/profile

which mysql

=============================================

退出數據庫時,盡量不要用ctrl+c進行退出mysql 用ctrl+d進行退出

2) 設置mysql服務開機自啟動

####11.加入開機自啟動

chkconfig --add mysqld

chkconfig mysqld on

3) 設置mysql數據庫登錄用戶名與密碼

####12.給MySQL root用戶設置密碼

/application/mysql/bin/mysqladmin -u root password ‘oldboy123‘

mysql -u root -p oldboy123

初級 添加刪除系列

#創建數據庫

create database wordpress;

#刪除數據庫

drop database wordpress;

#############################################################################################################

02:LNMP PHP軟件安裝

講解安裝PHP步驟

1) 解決PHP軟件的依賴關系

yum install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel -y

yum install freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel libxslt-devel -y

rpm -qa zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel

#查看是否下載成功

libiconv軟件安裝--和字符集轉換相關的軟件

#mkdir -p /server/tools

cd /server/tools

#wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz

tar zxf libiconv-1.14.tar.gz

cd libiconv-1.14

./configure --prefix=/usr/local/libiconv

make

make install

cd ../

和數據加密相關的三個軟件安裝

#wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

#更換源

yum -y install libmcrypt-devel mhash mcrypt

#查看是否安裝成功

rpm -qa libmcrypt-devel mhash mcrypt

2) 下載解壓PHP軟件

cd /server/tools/ #進入目錄

tar xf php-5.5.32.tar.gz #解壓壓縮包

cd php-5.5.32 #進入目錄

#下面是配置文件

./configure \

--prefix=/application/php-5.5.32 \

--with-mysql=/application/mysql/ \

--with-pdo-mysql=mysqlnd \

--with-iconv-dir=/usr/local/libiconv \

--with-freetype-dir \

--with-jpeg-dir \

--with-png-dir \

--with-zlib \

--with-libxml-dir=/usr \

--enable-xml \

--disable-rpath \

--enable-bcmath \

--enable-shmop \

--enable-sysvsem \

--enable-inline-optimization \

--with-curl \

--enable-mbregex \

--enable-fpm \

--enable-mbstring \

--with-mcrypt \

--with-gd \

--enable-gd-native-ttf \

--with-openssl \

--with-mhash \

--enable-pcntl \

--enable-sockets \

--with-xmlrpc \

--enable-soap \

--enable-short-tags \

--enable-static \

--with-xsl \

--with-fpm-user=www \

--with-fpm-group=www \

--enable-ftp \

--enable-opcache=no

#Generating files

#configure: creating ./config.status

#creating main/internal_functions.c

#creating main/internal_functions_cli.c

#+--------------------------------------------------------------------+

#| License: |

#| This software is subject to the PHP License, available in this |

#| distribution in the file LICENSE. By continuing this installation |

#| process, you are bound by the terms of this license agreement. |

#| If you do not agree with the terms of this license, you must abort |

#| the installation process at this point. |

#+--------------------------------------------------------------------+

#

#Thank you for using PHP.

make

make install <- 創建目錄,生成程序文件信息

了解FastCGI

3) PHP軟件程序創建軟鏈接

ln -s /application/php-5.5.32/ /application/php

4) 配置php解析文件/配置php-fpm配置文件

#上邊是開發用的 下面是運維用的

cd /server/tools/php-5.5.32

cp php.ini-production /application/php/lib/php.ini

php.ini-production 與 php.ini-development 文件區別關系對比

擴展:文件比較命令 diff zdiff vimdiff windows上的文件比較

cd /application/php/etc/

cp php-fpm.conf.default php-fpm.conf

5) 啟動php-fpm程序

/application/php/sbin/php-fpm

)6檢查

lsof -i :9000 <--- 確認php 9000端口是否正確啟動

ps -ef|grep php-fpm

03:wordpress博客站點部署配置

1) 修改nginx配置文件,使nginx程序與php程序建立聯系

vim extra/blog.conf

server {

listen 80;

server_name blog.etiantian.org;

location / {

root html/blog;

index index.php index.html index.htm; <-- 需要註意編輯修改默認首頁文件 index

}

location ~* .*\.(php|php5)?$ { #主要添加下面這行

root html/blog;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fastcgi.conf;

}

}

說明:利用nginx的location區塊實現動態請求與靜態請求的分別處理

實例

[[email protected] extra]# pwd

/application/nginx/conf/extra

[[email protected] extra]# cat blog.conf

server {

listen 80;

server_name blog.etiantian.org;

location / {

root html/blog;

index index.php index.html index.htm;

}

location ~* .*\.(php|php5)?$ {

root html/blog;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fastcgi.conf;

}

}

2) 編輯nginx與php連通性測試文件,並進行測試

echo ‘<?php phpinfo(); ?>‘ >/application/nginx/html/blog/test_info.php #創建測試文件

測試站點

curl http://blog.etiantian.org/index.html <-- 靜態請求站點文件信息測試

curl http://blog.etiantian.org/test_info.php <-- 動態請求站點文件信息測試

說明:當php服務停止時,9000端口信息消失,即停止PHP錯誤報502錯誤

linux系統測試完畢後,建議利用瀏覽器進行最終測試,測試效果更明顯些

3) 編輯php與mysql連通性測試文件,並進行測試

a. 創建數據庫

mysql -u root -p oldboy123; #進入數據庫

show databases; <--- 查看當前數據庫信息

create database wordpress;

實例

mysql> mysql -uroot -poldboy123;

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘mysql -uroot -poldboy123‘ at line 1

mysql> create database wordpress;

Query OK, 1 row affected (0.53 sec)

b. 添加用戶信息

grant all on wordpress.* to ‘wordpress‘@‘localhost‘ indentified by ‘oldboy123‘;

grant all on wordpress.* to ‘wordpress‘@‘172.16.1.%‘ indentified by ‘oldboy123‘;

flush privileges;

drop user [email protected]‘172.16.1.8‘; <--- 刪除用戶信息

select user,host from mysql.user; <--- 查看用戶信息

mysql -uwordpress -p123456 <--- 測試創建的用戶連接

show databases; <--- 查看當前數據庫信息

實例

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| test |

| wordpress |

+--------------------+

5 rows in set (0.33 sec)

mysql> grant all on wordpress.* to ‘wordpress‘@‘10.0.0.%‘ identified by ‘oldboy123‘;

Query OK, 0 rows affected (0.47 sec)

mysql> select user,host from mysql.user;

+-----------+-----------+

| user | host |

+-----------+-----------+

| wordpress | 10.0.0.% |

| root | 127.0.0.1 |

| root | ::1 |

| | localhost |

| root | localhost |

| | web01 |

| root | web01 |

+-----------+-----------+

7 rows in set (0.00 sec)

c. 測試php與數據庫連通性

vim test_mysql.php

<?php

//$link_id=mysql_connect(‘主機名‘,‘用戶‘,‘密碼‘);

//mysql -u用戶 -p密碼 -h 主機

$link_id=mysql_connect(‘localhost‘,‘wordpress‘,‘oldboy123‘) or mysql_error();

if($link_id){

echo "mysql successful by oldboy !\n";

}else{

echo mysql_error();

}

?>

curl blog.etiantian.org/test_mysql.php <-- 測試php與數據庫的連通性

4) 下載部署wordpress博客程序(https://cn.wordpress.org/ 英文官網:https://www.wordpress.org/ )

wget https://cn.wordpress.org/wordpress-4.5.1-zh_CN.tar.gz

tar xf wordpress-4.5.1-zh_CN.tar.gz

mv wordpress/* /application/nginx/html/blog/

chown -R www.www /application/nginx/html/blog/

註意:確認hosts文件進行了解析

瀏覽器頁面進行wordpress部署

vim wp-config.php 可以修改wordpress上的數據庫連接參數信息

04:wordpress博客站點數據庫遷移

遷移數據庫:利用數據庫備份命令

1) 備份數據庫數據庫信息

mysqldump -uroot -poldboy123 --all-databases >/tmp/bak.sql

ll /tmp/bak.sql -h

scp /tmp/bak.sql 172.16.1.51:/tmp/

2) 恢復數據庫數據庫信息

##db01

mysql -uroot -poldboy123 </tmp/bak.sql

###db01添加新的用戶

grant all on wordpress.* to [email protected]‘172.16.1.0/255.255.255.0‘ identified by ‘oldboy123‘;

flush privileges;

mysql -uwordpress -poldboy123 -h 172.16.1.51

3) 數據庫遷移完畢,修改網站連接數據庫的配置文件

mysql -uwordpress -poldboy123 -h 172.16.1.51 <-- 修改配置文件之前,先測試網站web服務器與遷移後的數據庫連通性

vim wp-config.php <-- 修改wordpress上的數據庫連接參數信息

/** MySQL主機 */

define(‘DB_HOST‘,‘172.16.1.51‘) <-- 修改連接的主機信息,將localhost修改為172.16.1.51

說明:web服務器數據庫此時可以關閉了

05. 進行數據遷移到NFS共享服務器

01:先將原有目錄中數據移出

cd /application/nginx/html/blog/wp-content/uploads

mkdir /tmp/wordpress_backup -p

mv ./* /tmp/wordpress_backup/

02:NFS服務器上配置創建共享目錄

vim /etc/exports

/data 172.16.1.0/24(rw,sync,all_squash)

showmount -e 172.16.1.31

mount -t nfs 172.16.1.31:/data /mnt/

showmount -e 172.16.1.31

mount -t nfs 172.16.1.31:/data/ ./uploads/

mv /tmp/wordpress_backup/* ./

技術分享

原創:LNMP架構部署個人博客網站 禁止轉載復制