(1)用戶經過瀏覽器輸入域名請求Nginx web服務;php
(2)Nginx對請求的資源進行判斷,若是是靜態資源,則由Nginx返回給用戶;若是是動態請求(.php文件),那麼Nginx就會把它經過FastCGI接口發送給PHP引擎服務(FastCGI進程php-fpm)進行解析;html
(3)當動態請求須要讀取數據庫數據,PHP就會繼續向後端請求Mysql數據庫,以讀取須要的數據;mysql
(4)最後經過Nginx服務把獲取的數據返回給用戶。linux
(1)下載二進制免編譯版本mysql 5.6.35 [root@localhost tools]# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz [root@localhost tools]# wget http://cn.php.net/distributions/php-7.2.5.tar.gz (2)增長mysql運行用戶 [root@localhost tools]# useradd -s /sbin/nologin -M mysql (3)解壓並移動Mysql到指定的安裝路徑 [root@localhost tools]# tar -zxf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz [root@localhost tools]# mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql-5.6.35 (4)建立軟鏈接並更改目錄所屬 [root@localhost tools]# ln -sv /usr/local/mysql-5.6.35 /usr/local/mysql ‘/usr/local/mysql’ -> ‘/usr/local/mysql-5.6.35’ [root@localhost mysql]# chown -R mysql.mysql /usr/local/mysql (5)初始化Mysql [root@localhost mysql]# scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql (6)拷貝Mysql啓動腳本,並修改腳本權限啓動 [root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld [root@localhost mysql]# chmod 755 /etc/init.d/mysqld [root@localhost mysql]# vim /etc/init.d/mysqld basedir=/usr/local/mysql datadir=/usr/local/mysql/data [root@localhost mysql]# cp support-files/my-default.cnf /etc/my.cnf [root@localhost mysql]# /etc/init.d/mysqld start Starting MySQL.Logging to '/usr/local/mysql/data/localhost.err'. ... SUCCESS! [root@localhost mysql]# netstat -tulnp |grep 3306 tcp6 0 0 :::3306 :::* LISTEN 62679/mysqld (7)加入開機啓動,測試登陸 [root@localhost mysql]# chkconfig --add mysqld [root@localhost mysql]# chkconfig mysqld on [root@localhost mysql]# export PATH=/usr/local/mysql/bin/:$PATH [root@localhost mysql]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.35 MySQL Community Server (GPL) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> quit; (8)mysql安全設置 [root@localhost mysql]# mysqladmin -uroot password '123456' //配置mysql的root用戶密碼 Warning: Using a password on the command line interface can be insecure. [root@localhost mysql]# mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) [root@localhost mysql]# mysql -uroot -p123456 -e "show databases;" Warning: Using a password on the command line interface can be insecure. +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ [root@localhost mysql]# mysql -uroot -p //清理無用的Mysql用戶和庫 Enter password: mysql> select user,host from mysql.user; +------+-----------+ | user | host | +------+-----------+ | root | 127.0.0.1 | | root | ::1 | | | localhost | | root | localhost | +------+-----------+ 4 rows in set (0.01 sec) mysql> drop user "root"@"::1" -> ; Query OK, 0 rows affected (0.00 sec) mysql> drop user ""@"localhost"; Query OK, 0 rows affected (0.00 sec) mysql> select user,host from mysql.user; +------+-----------+ | user | host | +------+-----------+ | root | 127.0.0.1 | | root | localhost | +------+-----------+ 2 rows in set (0.00 sec) 有時使用drop命令刪除不了用戶,多是大寫或者是特殊的Linux主機名致使的,以下: mysql> drop user ''@'MySQL'; ERROR 1396 (HY000): Operation DROP USER failed for ''@'mysql' 解決辦法以下: mysql> delete from mysql.user where user='' and host='MySQL'; mysql> flush privileges;
一、CGI和FastCGInginx
CGI(Common Gateway Interface)通用網關接口,爲HTTP服務器與其餘機器上的程序服務通訊交流的一種工具,CGI程序必需要在網絡服務器上運行。傳統的CGI接口方式存在很大的缺點就是性能較差。每次HTTP服務器遇到動態程序時,都須要從新啓動解析器來執行解析,以後結果纔會被返回給HTTP服務器,而這種方式在高併發的場景下是沒法使用的,爲了解決這一問題,隨之產生的就是FastCGI。c++
FastCGI是一個可伸縮、高速和HTTP服務器和動態腳本語言之間通訊的接口(在Linux環境下,FastCGI接口即爲socket,這個socket能夠是文件socket,也能夠是ip socket),主要優勢是把動態語言和HTTP服務器分離開來。FastCGI採用的是C/S架構,能夠將HTTP服務器和腳本解析服務器分開,同時還能在腳本解析服務器上啓動一個或多個腳本解析守護進程,然後將獲得的結果返回給瀏覽器。這種方式可使HTTP服務器專注處理靜態請求,或者是將動態請求的結果返回客戶端,在這一點上大大提高了應用系統的性能。web
二、Nginx FastCGI的運行原理算法
Nginx不支持對外部動態程序的直接調用或者解析,全部的外部程序(如PHP)必須經過FastCGI接口來調用。FastCGI在Linux下是socket,爲了調用CGI程序,還須要一個FastCGI的wrapper(能夠理解爲用於啓動線程的工具),這個wrapper綁定在某個固定的socket上,如端口或文件socket,好比127.0.0.1:9000。當Nginx將CGI請求發送到這個socket的時候,經過FastCGI接口,wrapper接口到請求,而後派生一個新的線程,這個線程再去調用解釋器或者外部程序處理腳原本讀取返回的數據;接着wrapper再將返回的數據經過FastCGI接口,沿着原來的socket傳遞個Nginx;最後Nginx將返回的數據發送給客戶端。sql
三、編譯安裝PHP 7.2.5數據庫
(1)準備PHP依賴的庫環境 [root@localhost ~]# yum install -y gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libpng libpng-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses curl openssl-devel gdbm-devel db4-devel libXpm-devel libX11-devel gd-devel gmp-devel readline-devel libxslt-devel expat-devel xmlrpc-c xmlrpc-c-devel [root@localhost ~]# cd /tools (2)下載php 7.2.5並解壓 [root@localhost ~]# wget http://cn.php.net/distributions/php-7.2.5.tar.gz [root@localhost tools]# tar -zxf php-7.2.5.tar.gz [root@localhost tools]# cd php-7.2.5 (3)編譯安裝 [root@localhost php-7.2.5]# ./configure \ --prefix=/usr/local/php7.2.5 \ //指定 php 安裝目錄 --with-config-file-path=/usr/local/php7.2.5/etc \ //指定php.ini位置 --with-config-file-scan-dir=/usr/local/php7.2.5/etc/conf.d \ //指定擴展php.ini位置 --enable-inline-optimization \ //優化線程 --disable-debug \ //關閉調試模式 --disable-rpath \ //關閉額外的運行庫文件 --enable-shared \ --enable-opcache \ //啓用操做碼緩存 --enable-fpm \ //表示激活PHP-FPM方式服務,即FactCGI方式運行PHP服務。 --with-mysql=mysqlnd \ //增長mysql支持 --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd \ --with-gettext \ //打開gnu 的gettext 支持,編碼庫用到 --enable-mbstring \ //多字節,字符串的支持 --with-iconv \ //打開iconv函數,多種字符集間的轉換 --with-mcrypt \ //啓用mcrypt加密算法 --with-mhash \ //啓用mhash加密算法 --with-openssl \ //openssl的支持,加密傳輸時用到的 --enable-bcmath \ //打開圖片大小調整,用到zabbix監控的時候用到了這個模塊 --enable-soap \ --with-libxml-dir \ //打開libxml2庫的支持 --enable-pcntl \ //freeTDS須要用到,多是連接mssql --enable-shmop \ --enable-sysvmsg \ --enable-sysvsem \ //使用sysv信號機制 --enable-sysvshm \ --enable-sockets \ //打開sockets支持 --enable-exif \ --enable-zend-signals \ --enable-gd-native-ttf \ //支持TrueType字符串函數庫 --enable-ftp \ //打開ftp的支持 --with-curl \ //打開curl瀏覽工具的支持 --with-zlib \ //打開zlib庫的支持 --enable-zip \ //打開對zip的支持 --with-bz2 \ //打開對bz2文件的支持 --with-readline \ --with-jpeg-dir \ //打開對jpeg圖片的支持 --with-png-dir \ //打開對png圖片的支持 --with-gd \ //打開gd庫的支持 --with-freetype-dir \ //打開對freetype字體庫的支持 --with-pear \ //打開pear命令的支持,PHP擴展用的 [root@localhost php-7.2.5]# make && make install (4)修改php服務的相關配置文件 [root@localhost php-7.2.5]# cp php.ini-production /usr/local/php7.2.5/etc/php.ini [root@localhost php-7.2.5]# cd /usr/local/php7.2.5/etc/ [root@localhost etc]# cp php-fpm.conf.default php-fpm.conf [root@localhost etc]# cp php-fpm.d/www.conf.default php-fpm.d/www.conf (5)拷貝啓動腳本,並添加到開機啓動 [root@localhost etc]# cp /tools/php-7.2.5/sapi/fpm/php-fpm.service /usr/lib/systemd/system/ [root@localhost etc]# /usr/local/php7.2.5/sbin/php-fpm -t [17-Jul-2018 16:56:41] NOTICE: configuration file /usr/local/php7.2.5/etc/php-fpm.conf test is successful [root@localhost etc]# export PATH=/usr/local/php7.2.5/bin/:/usr/local/php7.2.5/sbin/:$PATH [root@localhost etc]# systemctl enable php-fpm Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service. (6)啓動php-fpm,並檢查端口 [root@localhost etc]# systemctl start php-fpm [root@localhost etc]# netstat -tulnp |grep 9000 tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 14853/php-fpm: mast [root@localhost php7.2.5]# ln -sv /usr/local/php7.2.5 /usr/local/php ‘/usr/local/php’ -> ‘/usr/local/php7.2.5’
[root@localhost conf]# vim vhosts/www.abc.org.ssl.conf server { listen 443 ssl; server_name www.abc.org abc.org; root /vhosts/html/www; index index.html index.htm index.php; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers AES128-SHA:AES256-SHA:RC4-SHA:DES-CBC3-SHA:RC4-MD5; ssl_certificate /usr/local/nginx/conf/cert.pem; ssl_certificate_key /usr/local/nginx/conf/cert.key; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; location /nginx_status { stub_status on; access_log off; } location ~ \.php$ { root /vhosts/html/www; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } [root@localhost conf]# nginx -t nginx: the configuration file /usr/local/nginx1.15.1/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx1.15.1/conf/nginx.conf test is successful [root@localhost conf]# echo "<?php phpinfo(); ?>" > /vhosts/html/www/index.php [root@localhost conf]# mv /vhosts/html/www/index.html /tmp/
訪問:www.abc.org,會出現下面的圖,表示php解析成功
針對Nginx請求訪問PHP,而後對PHP鏈接MySQL的鏈接進行測試,編輯測試頁面test.php
[root@localhost vhosts]# mv www.abc.org.conf.ssl.conf /tmp [root@localhost vhosts]# vim www.abc.org.conf server { listen 80; server_name www.abc.org abc.org; root /vhosts/html/www; index index.html index.php index.htm; access_log logs/www.abc.org_access.log main; error_log logs/www.abc.org_error.log info; location ~ \.php$ { root /vhosts/html/www; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } [root@localhost vhosts]# vim /vhosts/html/www/test.php <?php $link_id=mysqli_connect('192.168.56.11','root','123456') or mysqli_error(); if ($link_id) { echo "php_mysql is Success!"; } else { echo "php_mysql is Failed!"; } ?>
訪問:http://www.abc.org/test.php
出現的問題:因爲本次PHP版本使用的是7.2.5,在測試鏈接數據庫時使用的庫函數mysql_connect()沒法正常鏈接數據庫,提示報錯信息以下:
2018/07/18 09:39:56 [error] 31736#0: *525 FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught Error: Call to undefined function mysql_connect() in /vhosts/html/www/test.php:2 Stack trace: #0 {main} thrown in /vhosts/html/www/test.php on line 2" while reading response header from upstream, client: 192.168.56.1, server: www.abc.org, request: "GET /test.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.abc.org"
表示並無定義mysql_connect()函數,查閱PHP7資料發現PHP5中使用mysql_connect()函數進行鏈接,但實際上,PHP5.5開始,MySQL就不推薦使用了,屬於廢棄函數。PHP7中貌似已經完全不支持了,根據官網說明,取而代之的是以下兩個:
本擴展自 PHP 5.5.0 起已廢棄,並在未來會被移除。應使用 MySQLi 或 PDO_MySQL 擴展來替換之。參見 MySQL:選擇 API 指南以及相關 FAQ 以獲取更多信息。用以替代本函數的有:
使用時,不要在使用mysql_connect了,能夠換用mysqli_connect(),用法基本相似吧,聽說是面向對象的庫。
一、MySQL數據庫配置準備
[root@localhost tools]# wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz //下載wordpress源碼包 [root@localhost tools]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 24 Server version: 5.6.35 MySQL Community Server (GPL) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> create database wordpress default character set = 'utf8'; //建立wordpress專用數據庫,用於存放blog數據 Query OK, 1 row affected (0.00 sec) mysql> show databases like "wordpress"; +----------------------+ | Database (wordpress) | +----------------------+ | wordpress | +----------------------+ 1 row in set (0.02 sec) mysql> grant all on wordpress.* to wordpress@'%' identified by '123456'; //受權數據庫管理用戶 Query OK, 0 rows affected (0.02 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) mysql> quit; Bye
二、Nginx和PHP配置準備
[root@localhost vhosts]# vim wordpress.conf //編輯博客虛擬主機配置 server { listen 80; server_name blog.test.com; root /vhosts/html/wordpress; index index.html index.php index.htm; access_log logs/blog.test.com_access.log main; error_log logs/blog.test.com_error.log info; location ~ \.php$ { root /vhosts/html/wordpress; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } [root@localhost tools]# tar -zxf wordpress-4.9.4-zh_CN.tar.gz //解壓博客源碼包 [root@localhost tools]# mv wordpress /vhosts/html/ [root@localhost wordpress]# chown -R nginx.nginx /vhosts/html/wordpress //更改所屬權限 [root@localhost wordpress]# nginx -t nginx: the configuration file /usr/local/nginx1.15.1/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx1.15.1/conf/nginx.conf test is successful [root@localhost wordpress]# nginx -s reload
windows下作hosts域名解析 192.168.56.11 blog.test.com,訪問blog.test.com,出現如下界面,進行安裝wordpress
填寫數據庫相關信息
提交後,點擊如今安裝,然後輸入博客相關信息。完成後登陸博客,可進入到博客內部,如圖:
在此界面能夠進行發佈文章,發佈完成後,從新訪問blog.test.com時,則跳到了正常的博客訪問頁面。
說到URL靜態化,就是常說的僞靜態了,那麼什麼是僞靜態呢?
僞靜態是相對真實靜態而言,那麼什麼又是靜態頁面呢?靜態頁面,是客戶訪問服務器的資源,服務器會直接把訪問的頁面中展現在瀏覽器當中,不須要數據庫的支持,通俗地來講就是.html .htm這樣的訪問頁面。而說到這就得說說動態頁面,網頁會根據客戶的請求,從數據庫當中篩選出客戶想要的內容展現到瀏覽器當中,通俗地來講,就是.php 等動態語言展現的頁面。
那麼僞靜態,顧名思義就是假裝的靜態頁面,其目的是爲了更好地被搜索引擎收錄而經過必定的規則,把動態頁面的地址轉換成html或htm結尾的地址,看起來是靜態的,實際上依然是動態頁面。舉個例子:
京東的活動靜態頁面:https://sale.jd.com/act/xuFh7kMODlwN6A.html
京東的登陸頁面:https://passport.jd.com/uc/login?ReturnUrl=https%3A%2F%2Forder.jd.com%2Fcenter%2Flist.action
對比以上2個頁面,就發現靜態頁面是以.html形式訪問,而動態的頁面則會在連接當中帶? %等一系列的特殊字符。而僞靜態的做用則是將原來的動態頁面經過一系列的規則轉換成靜態頁面。可是僞靜態也存在不足之處:
(1)因爲僞靜態是用正則判斷而不是真實地址,分別顯示哪一個頁面的責任也由直接指定轉由CPU來判斷了,因此會致使CPU使用率的上升;
(2)網站承受力下降
(3)網頁打開速度慢,因爲僞靜態依舊須要讀取數據庫,額外地還須要進行URL地址重寫,在速度上也是會偏慢的。
(4)作了僞靜態,原有的頁面也能夠進行訪問,這也會形成大量的僞靜態頁面和動態頁面重複,致使大量的重複頁面。
那麼既然有這麼多缺點,爲何還要用僞靜態呢?其最主要的功能仍是爲了搜索引擎的收錄。這裏僅僅做爲了解什麼是僞靜態和配置,涉及SEO方面的知識,哈哈,不懂!
這裏在訪問博客的一篇文章時,點擊訪問文章,能夠看到鏈接爲:http://blog.test.com/?p=4,這就是一個動態的頁面,要實現URL靜態化,首先要在Wordpress後臺設置-->固定連接-->自定義結構,而後輸入下面的代碼,如圖:
/archives/%post_id%.html 說明:%post_id% 表示的是數據庫對應博文內容的惟一ID。而後修改Nginx虛擬主機配置文件:
[root@localhost vhosts]# vim wordpress.conf server { listen 80; server_name blog.test.com; root /vhosts/html/wordpress; index index.html index.php index.htm; access_log logs/blog.test.com_access.log main; error_log logs/blog.test.com_error.log info; if (-f $request_filename/index.html){ rewrite (.*) $1/index.html break; } if (-f $request_filename/index.php){ rewrite (.*) $1/index.php; } if (!-f $request_filename){ rewrite (.*) /index.php; } location ~ \.php$ { root /vhosts/html/wordpress; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } [root@localhost vhosts]# nginx -t nginx: the configuration file /usr/local/nginx1.15.1/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx1.15.1/conf/nginx.conf test is successful [root@localhost vhosts]# nginx -s reload
訪問:http://blog.test.com 點擊博文,能夠看到連接由:http://blog.test.com/?p=4 變成了:http://blog.test.com/archives/4.html,至此就實現了URL的僞靜態效果。