更新nginx官方yum源php
[root@web02 ~]# vim /etc/yum.repos.d/nginx.repo [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key
yum安裝nginx軟件並啓動html
[root@web02 ~]# yum -y install nginx [root@web02 ~]# systemctl start nginx [root@web02 ~]# systemctl enable nginx
編寫nginx服務配置文件定義worker用戶爲www(必須爲系統上存在的用戶)mysql
[root@web02 ~]# vim /etc/nginx/nginx.conf user www; [root@web02 ~]# systemctl restart nginx
安裝軟件並啓動:linux
[root@web02 ~]# yum -y install mariadb-server.x86_64 mariadb [root@web02 ~]# systemctl start mariadb.service [root@web02 ~]# systemctl enable mariadb.service
[root@web02 ~]# mysqladmin -u root password '123456' #mysqladmin 是設置密碼的命令而不是修改密碼,修改密碼須要用到mysql的語法來修改
使用root用戶登陸數據庫 (-p 以後緊接着就是密碼信息不能又空格)nginx
[root@web02 ~]# mysql -uroot -p'123456' Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 3 Server version: 5.5.68-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>
更新yum源/卸載系統自帶的php軟件web
[root@web02 ~]# yum remove php-mysql php php-fpm php-common 已加載插件:fastestmirror 參數 php-mysql 沒有匹配 參數 php 沒有匹配 參數 php-fpm 沒有匹配 參數 php-common 沒有匹配 不刪除任何軟件包 [root@web02 ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm 獲取https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm 警告:/var/tmp/rpm-tmp.C7Lv8z: 頭V4 RSA/SHA256 Signature, 密鑰 ID 352c64e5: NOKEY 準備中... ################################# [100%] 正在升級/安裝... 1:epel-release-7-13 警告:/etc/yum.repos.d/epel.repo 已創建爲 /etc/yum.repos.d/epel.repo.rpmnew ################################# [100%] [root@web02 ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm 獲取https://mirror.webtatic.com/yum/el7/webtatic-release.rpm 警告:/var/tmp/rpm-tmp.RS98qy: 頭V4 RSA/SHA1 Signature, 密鑰 ID 62e74ca5: NOKEY 準備中... ################################# [100%] 正在升級/安裝... 1:webtatic-release-7-3 ################################# [100%]
安裝php軟件(此時安裝會比較慢)redis
[root@web02 ~]#yum install -y php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb
編寫配置文件(修改服務進程的管理用戶)sql
[root@web02 ~]# vim /etc/php-fpm.d/www.conf ; Start a new pool named 'www'. [www] ; 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 = www ; RPM: Keep a group allowed to write in log dir. group = www
ps:保證nginx進程的管理用戶和php服務進程的管理用戶保持一致mongodb
啓動php服務shell
[root@web02 ~]# systemctl start php-fpm
用戶訪問網站--->nginx(fastcgi_pass) --FastCGI-->(php-fpm -- wrapper) php (php解析器) ---> mysql(讀取或寫入)
編寫nginx配置文件並重啓nginx服務
[root@web02 ~]# vim /etc/nginx/conf.d/www.conf server { listen 80; server_name www.jiage.com; location / { root /html/www; index index.html; error_page 404 /404.jpg; } location ~\.php$ { root /html/www; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass 127.0.0.1:9000; include fastcgi_params; } } [root@web02 ~]# systemctl reload nginx.service
ps: fastcgi_param 該行表明着URL 與URI信息
/etc/nginx/fastcgi_params 下能夠看到定義的變量 $document_root$fastcgi_script_name;
編寫動態資源測試文件
[root@web02 ~]# vim /html/www/test_php.php <?php phpinfo(); ?>
進行訪問測試
www.jiage.com/test_php.php
編寫php代碼文件
[root@web02 ~]# vim /html/www/test_mysql.php <?php $servername = "localhost"; $username = "root"; $password = "123456"; //$link_id=mysql_connect('主機名','用戶','密碼'); //mysql -u用戶 -p密碼 -h 主機 $conn = mysqli_connect($servername, $username, $password); if ($conn) { echo "mysql successful by root !\n"; }else{ die("Connection failed: " . mysqli_connect_error()); } ?> //mysqli_connect() 該函數主要是用於與數據庫連接
自此說明LNMP的基礎架構已經搭建好了 後面以搭建一個blog網站頁面爲例子
*www網站頁面: http://www.dedecms.com/
bbs網站頁面: http://www.discuz.net/forum.php
blog網站頁面: https://cn.wordpress.org/
wecenter網站頁面: http://www.wecenter.com/?copyright*
注意:若是站點目錄中有文件最好先備份 再將解壓後的信息移過去,這樣防止有些一樣名字的文件被替代
[root@web02 ~]# vim /etc/nginx/conf.d/blog.conf server { listen 80; server_name blog.jiage.com; location / { root /html/blog; index index.html; } location ~\.php$ { root /html/blog; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass 127.0.0.1:9000; include fastcgi_params; } } [root@web02 ~]# vim /html/blog/test_mysql.php <?php $servername = "localhost"; $username = "root"; $password = "123456"; //$link_id=mysql_connect('主機名','用戶','密碼'); //mysql -u用戶 -p密碼 -h 主機 $conn = mysqli_connect($servername, $username, $password); if ($conn) { echo "mysql successful by root !\n"; }else{ die("Connection failed: " . mysqli_connect_error()); } ?> [root@web02 html]# systemctl reload nginx.service [root@web02 blog]# tar xf wordpress-5.6-zh_CN.tar.gz [root@web02 blog]# ll 總用量 15724 -rw-r--r-- 1 root root 24 1月 5 23:55 index.html -rw-r--r-- 1 root root 372 1月 9 20:56 test_mysql.php drwxr-xr-x 5 1006 1006 4096 12月 22 22:00 wordpress -rw-r--r-- 1 root root 16086935 12月 24 22:43 wordpress-5.6-zh_CN.tar.gz [root@web02 blog]# mv wordpress/* ./ [root@web02 blog]# ls index.html test_mysql.php wp-admin wp-content wp-load.php wp-signup.php index.php wordpress wp-blog-header.php wp-cron.php wp-login.php wp-trackback.php license.txt wordpress-5.6-zh_CN.tar.gz wp-comments-post.php wp-includes wp-mail.php xmlrpc.php readme.html wp-activate.php wp-config-sample.php wp-links-opml.php wp-settings.php
[root@web02 html]# chown -R www.www blog/
瀏覽器訪問: blog.jiage.com/index.php
ps:此時先不要點提交,經過第五步建立對應的數據庫和數據庫用戶
建立與第四步對應的數據庫和數據庫用戶:
[root@web02 html]# mysql -u root -p123456 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 4 Server version: 5.5.68-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> create database wordpress; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> grant all on wordpress.* to 'wordpress'@'localhost'identified by '123456'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> select user,host from mysql.user; +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | | root | ::1 | | | localhost | | root | localhost | | wordpress | localhost | | | web02 | | root | web02 | +-----------+-----------+ 7 rows in set (0.00 sec)
此時訪問會出現403錯誤,緣由是沒有首頁文件 index.php
[root@web02 html]# vim /etc/nginx/conf.d/blog.conf server { listen 80; server_name blog.jiage.com; location / { root /html/blog; index index.php index.html; } [root@web02 html]# systemctl restart nginx
利用瀏覽器訪問 blog.jiage.com
[root@web02 ~]# vim /etc/nginx/nginx.conf http { client_max_body_size 50M; }
**選擇在`http{}`中設置:client_max_body_size 50m;** **也能夠選擇在`server{}`中設置:client_max_body_size 50m;** **還能夠選擇在`location{}`中設置:client_max_body_size 50m;** **三者有區別,不一樣的做用域參數設置,有不一樣的含義。** **設置到`http{}`內,控制全局nginx全部請求報文大小;** **設置到`server{}`內,控制該server的全部請求報文大小;** **設置到`location{}`內,控制知足該路由規則的請求報文大小 ;**
因爲workpress採用php設計,文件上傳大小限制值,是由php環境的配置決定的。修改 /etc/php.ini 配置文件便可
[root@web02 ~]# vim /etc/php.ini upload_max_filesize = 50M post_max_size = 50M max_execution_time = 3 [root@web02 ~]# systemctl restart php-fpm.service
自此咱們發現數據庫數據與web數據都在同一臺服務器上,這樣信息不安全,所以咱們須要準備一臺mysql服務器和nfs存儲共享服務器。
兩種方法:1) 定位到站點目錄中查找最近上傳的文件
[root@web02 blog]# find /html/blog/ -type f -mmin -20
2)經過發表的文章,在瀏覽器上覆製圖片地址
http://blog.jiage.com/wp-content/uploads/2021/01/1-768x1024.jpg
blog.jiage.com表明着站點目錄的位置/html/blog
在nfs服務器上建立與web服務器對應的id用戶www爲映射用戶。
[root@nfs01 ~]# useradd www [root@nfs01 ~]# id www uid=1001(www) gid=1001(www) 組=1001(www) [root@nfs01 ~]# chown -R www.www /data
編寫nfs配置文件並重啓後檢查服務是否正常
[root@nfs01 ~]# vim /etc/exports /data/bbs 172.16.1.0/24(rw,sync,anonuid=1001,anongid=1001) /data/blog 172.16.1.0/24(rw,sync,anonuid=1001,anongid=1001) /data/www 172.16.1.0/24(rw,sync,anonuid=1001,anongid=1001) [root@nfs01 ~]# systemctl restart nfs [root@nfs01 ~]# showmount -e Export list for nfs01: /data/www 172.16.1.0/24 /data/blog 172.16.1.0/24 /data/bbs 172.16.1.0/24
web上測試nfs掛載
[root@web02 2021]# yum -y install nfs-utils [root@web02 2021]# mount -t nfs 172.16.1.31:/data/www /mnt [root@web02 2021]# df -Th 文件系統 類型 容量 已用 可用 已用% 掛載點 172.16.1.31:/data/www nfs4 47G 1.5G 46G 4% /mnt [root@web02 2021]# umount /mnt
說明web服務器與nfs存儲服務器已可創建關係
將web服務器上blog存儲的數據進行遷移(由於直接掛載至關於格式化了)
[root@web02 uploads]# ls 2021 [root@web02 uploads]# pwd /html/blog/wp-content/uploads [root@web02 uploads]# mv ./2021 /tmp
將blog存儲數據的目錄掛載到nfs服務器上
[root@web02 uploads]# cd ~ [root@web02 ~]# mount -t nfs 172.16.1.31:/data/blog /html/blog/wp-content/uploads [root@web02 ~]# mv /tmp/2021/ /html/blog/wp-content/uploads/ [root@web02 ~]# df -Th 文件系統 類型 容量 已用 可用 已用% 掛載點 172.16.1.31:/data/blog nfs4 47G 1.5G 46G 4% /html/blog/wp-content/uploads
ps:若是/data/目錄權限的擁護者與nfs配置文件上映射用戶的id不一致會道中沒有權限(須要瞭解nfs服務root用戶與普通用戶的映射關係)
此時看似將數據存儲在原先/html/blog/wp-content/uploads這目錄下,實則已存儲在nfs服務器上的/data/blog中
[root@nfs01 data]# cd ./blog/ [root@nfs01 blog]# ll 總用量 0 drwxr-xr-x 3 www www 16 1月 9 22:09 2021
測試:在瀏覽器發表文章後能夠在nfs服務上看到上傳的圖片,即web與nfs已經創建關係。
[root@web02 01]# mysqldump -u root -p123456 --all-database > /tmp/web_bak.sql
[root@web02 01]# scp -rp /tmp/web_bak.sql 172.16.1.51:/tmp/ The authenticity of host '172.16.1.51 (172.16.1.51)' can't be established. ECDSA key fingerprint is SHA256:FuqiLIfW+LFJVdI/IFgcIv+9Tf1gXtgdWKXm4SKORgU. ECDSA key fingerprint is MD5:e6:33:4f:bc:e2:ad:d0:01:07:ae:78:4e:c4:e1:0f:d2. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '172.16.1.51' (ECDSA) to the list of known hosts. root@172.16.1.51's password: web_bak.sql 100% 927KB 75.9MB/s 00:0
[root@db01 ~]# yum -y install mariadb-server.x86_64 mariadb [root@db01 ~]# systemctl start mariadb.service [root@db01 ~]# systemctl enable mariadb.service [root@db01 ~]# mysqladmin -u root password 123456 [root@db01 ~]# mysql -uroot -p123456 < /tmp/web_bak.sql
MariaDB [(none)]> select user,host from mysql.user; +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | | root | ::1 | | | localhost | | root | localhost | | wordpress | localhost | | | web02 | | root | web02 | +-----------+-----------+ 7 rows in set (0.00 sec) 此時看到的wordpress只容許在本地登陸
優化:刪除無用的用戶信息以避免對實驗形成影響
MariaDB [(none)]> delete from mysql.user where user="" and host="localhost"; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> delete from mysql.user where user="" and host="web02"; Query OK, 1 row affected (0.00 sec)
添加:添加新的用戶信息
MariaDB [(none)]> grant all on wordpress.* to 'wordpress'@'172.16.1.%' identified by '123456'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.00 sec)
[root@web02 blog]# vim /html/blog/wp-config.php /** MySQL主機 */ define( 'DB_HOST', '172.16.1.51' );
測試: 瀏覽器訪問blog.jiage.com 還能正常訪問,並註冊新用戶,能夠在數據庫服務器上查到。
MariaDB [wordpress]> select user_login from wp_users; +------------+ | user_login | +------------+ | admin | | jiage | +------------ 2 rows in set (0.00 sec)