力扣:最大的團隊表現值
lamp簡介
所謂lamp,其實就是由Linux+Apache+Mysql/MariaDB+Php/Perl/Python的一組動態網站或者伺服器的開源軟體,除Linux外其它各部件本身都是各自獨立的程式,但是因為經常被放在一起使用,擁有了越來越高的相容度,共同組成了一個強大的Web應用程式平臺。
LAMP指的是Linux(作業系統)、Apache(HTTP伺服器)、MySQL(也指MariaDB,資料庫軟體)和PHP(有時也是指Perl或Python)的第一個字母,一般用來建立web應用平臺。
web伺服器工作流程
在說lamp架構平臺的搭建前,我們先來了解下什麼是CGI,什麼是FastCGI,什麼是......
web伺服器的資源分為兩種,靜態資源和動態資源
- 靜態資源就是指靜態內容,客戶端從伺服器獲得的資源的表現形式與原檔案相同。可以簡單的理解為就是直接儲存於檔案系統中的資源
- 動態資源則通常是程式檔案,需要在伺服器執行之後,將執行的結果返回給客戶端
那麼web伺服器如何執行程式並將結果返回給客戶端呢?下面通過一張圖來說明一下web伺服器如何處理客戶端的請求
如上圖所示
階段①顯示的是httpd伺服器(即apache)和php伺服器通過FastCGI協議進行通訊,且php作為獨立的服務程序執行
階段②顯示的是php程式和mysql資料庫間通過mysql協議進行通訊。php與mysql本沒有什麼聯絡,但是由Php語言寫成的程式可以與mysql進行資料互動。同理perl和python寫的程式也可以與mysql資料庫進行互動
cgi與fastcgi
上圖階段①中提到了FastCGI,下面我們來了解下CGI與FastCGI。
CGI(Common Gateway Interface,通用閘道器介面),CGI是外部應用程式(CGI程式)與WEB伺服器之間的介面標準,是在CGI程式和Web伺服器之間傳遞資訊的過程。CGI規範允許Web伺服器執行外部程式,並將它們的輸出傳送給Web瀏覽器,CGI將web的一組簡單的靜態超媒體文件變成一個完整的新的互動式媒體。
FastCGI(Fast Common Gateway Interface)是CGI的改良版,CGI是通過啟用一個直譯器程序來處理每個請求,耗時且耗資源,而FastCGI則是通過master-worker形式來處理每個請求,即啟動一個master主程序,然後根據配置啟動幾個worker程序,當請求進來時,master會從worker程序中選擇一個去處理請求,這樣就避免了重複的生成和殺死程序帶來的頻繁cpu上下文切換而導致耗時
httpd與php結合的方式
httpd與php結合的方式有以下三種:
- modules:php將以httpd的擴充套件模組形式存在,需要載入動態資源時,httpd可以直接通過php模組來加工資源並返回給客戶端
- httpd prefork:libphp5.so(多程序模型的php)
- httpd event or worker:libphp5-zts.so(執行緒模型的php)
- CGI:httpd需要載入動態資源時,通過CGI與php直譯器聯絡,獲得php執行的結果,此時httpd負責與php連線的建立和斷開等
- FastCGI:利用php-fpm機制,啟動為服務程序,php自行執行為一個服務,https通過socket與php通訊
web工作流程
通過上面的圖說明一下web的工作流程:
客戶端通過http協議請求web伺服器資源
- web伺服器收到請求後判斷客戶端請求的資源是靜態資源或是動態資源
- 若是靜態資源則直接從本地檔案系統取之返回給客戶端。
- 否則若為動態資源則通過FastCGI協議與php伺服器聯絡,通過CGI程式的master程序排程worker程序來執行程式以獲得客戶端請求的動態資源,並將執行的結果通過FastCGI協議返回給httpd伺服器,httpd伺服器收到php的執行結果後將其封裝為http響應報文響應給客戶端。在執行程式獲取動態資源時若需要獲得資料庫中的資源時,由Php伺服器通過mysql協議與MySQL/MariaDB伺服器互動,取之而後返回給httpd,httpd將從php伺服器收到的執行結果封裝成http響應報文響應給客戶端。
lamp平臺構建
環境說明:
系統平臺 | IP | 需要安裝的服務 |
centos8 redhat8 | 192.168.220.40 | httpd mysql php php-mysql |
1.安裝HTTP:
//安裝需要的包 [root@chouyu ~]# yum -y install wget bzip2 gcc gcc-c++ make pcre-devel expat-devel libxml2-devel openssl-devel [root@chouyu ~]# yum groups mark install 'Development Tools' //下載和安裝apache,apr和apr-util [root@chouyu~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.46.tar.gz [root@chouyu~]# wget http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.7.0.tar.gz [root@chouyu~]# wget http://mirrors.hust.edu.cn/apache//apr/apr-util-1.6.1.tar.gz //建立apache使用者和組 [root@chouyu~]# useradd -r -M -s /sbin/nologin apache //解壓 [root@chouyu ~]# tar -xf apr-1.7.0.tar.gz [root@chouyu~]# tar -xf httpd-2.4.46.tar.gz [root@chouyu~]# tar -xf apr-util-1.6.1.tar.gz [root@chouyu~]# cd apr-1.7.0 [root@chouyuapr-1.7.0]# vim configure cfgfile="${ofile}T" trap "$RM \"$cfgfile\"; exit 1" 1 2 15 # $RM "$cfgfile" //將此行加上註釋,或者刪除此行 [root@localhost apr-1.7.0]# ./configure --prefix=/usr/local/apr .................................................... //配置過程 [root@localhost apr-1.7.0]# make && make install ................................... //編譯安裝過程 [root@localhost apr-1.7.0]# cd /root/apr-util-1.6.1 [root@localhost apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr .................................... //配置過程 [root@localhost apr-util-1.6.1]# make && make install ......................................... //編譯安裝過程 //apache安裝 [root@chouyu apr-util-1.6.1]# cd /root/httpd-2.4.46 [root@chouyu httpd-2.4.46]# ./configure --prefix=/usr/local/apache \ > --sysconfdir=/etc/httpd24 \ > --enable-so \ > --enable-ssl \ > --enable-cgi \ > --enable-rewrite \ > --with-zlib \ > --with-pcre \ > --with-apr=/usr/local/apr \ > --with-apr-util=/usr/local/apr-util/ \ > --enable-modules=most \ > --enable-mpms-shared=all \ > --with-mpm=prefork ....................................配置過程 [root@chouyu httpd-2.4.46]# make && make install ............................編譯安裝過程 //安裝後配置 [root@localhost ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh [root@localhost ~]# source /etc/profile.d/httpd.sh [root@localhost ~]# ln -s /usr/local/apache/include/ /usr/include/httpd [root@localhost ~]# vim /etc/man_db.conf MANDATORY_MANPATH /usr/man MANDATORY_MANPATH /usr/share/man MANDATORY_MANPATH /usr/local/share/man MANDATORY_MANPATH /usr/local/apache/man //新增此行 [root@chouyu ~]# vim /etc/httpd24/httpd.conf # #ServerName www.example.com:80 //取消前面的註釋 //啟動 [root@chouyu httpd-2.4.46]# apachectl start [root@chouyu httpd-2.4.46]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 128 *:80 *:* [root@chouyu httpd-2.4.46]# //安裝依賴包 [root@localhost ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs //建立使用者和組 [root@localhost ~]# useradd -r -M -s /sbin/nologin mysql //下載二進位制格式mysql軟體包 [root@localhost ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz //解壓軟體 [root@localhost ~]# tar -xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local/ [root@localhost ~]# ln -sv /usr/local/mysql-5.7.31-linux-glibc2.12-x86_64/ /usr/local/mysql '/usr/local/mysql' -> '/usr/local/mysql-5.7.31-linux-glibc2.12-x86_64/' [root@localhost local]# chown -R mysql.mysql mysql* //新增環境變數 [root@localhost local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/myslq.sh [root@localhost local]# source /etc/profile.d/myslq.sh [root@localhost local]# echo $PATH /usr/local/mysql/bin:/usr/local/apache/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin <br>[root@localhost ~]# ln -s /usr/local/mysql/include/ /usr/include/mysql<br>[root@localhost ~]# vim /etc/man_db.conf <br>MANDATORY_MANPATH /usr/local/apache/man<br>MANDATORY_MANPATH /usr/local/mysql/man #新增man檔案<br><br>[root@localhost ~]# vim /etc/ld.so.conf.d/mysql.conf<br>/usr/local/mysql/lib<br><br>[root@localhost ~]# ldconfig <br><br><br><br><br><br> #建立資料存放目錄 [root@localhost local]# mkdir /mydata [root@localhost local]# chown -R mysql.mysql /mydata/ //初始化資料庫 [root@chouyu local]# mysqld --initialize --user=mysql --datadir=/mydata 2021-01-03T12:26:58.269415Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2021-01-03T12:26:58.522285Z 0 [Warning] InnoDB: New log files created, LSN=45790 2021-01-03T12:26:58.546744Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2021-01-03T12:26:58.602304Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: f943bc2c-4dbe-11eb-8248-000c29aa2fbf. 2021-01-03T12:26:58.602972Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2021-01-03T12:26:58.917180Z 0 [Warning] CA certificate ca.pem is self signed. 2021-01-03T12:26:59.390382Z 1 [Note] A temporary password is generated for root@localhost: Ewl1guldYj>! #修改my.cnf配置檔案 [root@localhost ~]# vim /etc/my.cnf #新增以下內容 [mysqld] basedir = /usr/local/mysql datadir = /mydata socket = /tmp/mysql.sock port = 3306 pid-file = /mydata/mysql.pid user = mysql skip-name-resolve [client-server] #配置服務啟動指令碼 [root@localhost ~]# cd /usr/local/mysql/support-files/ [root@localhost support-files]# ls magic mysqld_multi.server mysql-log-rotate mysql.server [root@localhost support-files]# cp mysql.server /etc/init.d/mysqld [root@localhost support-files]# vim /etc/init.d/mysqld # If you change base dir, you must also change datadir. These may get # overwritten by settings in the MySQL configuration files. basedir=/usr/local/mysql datadir=/mydata #啟動mysql [root@localhost ~]# service mysqld start Starting MySQL. SUCCESS! [root@chouyu support-files]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 80 *:3306 *:* LISTEN 0 128 *:80 *:* [root@chouyu support-files]# mysql -uroot -p'Ewl1guldYj>!' mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.31 Copyright (c) 2000, 2020, 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> set password = password('123321123'); Query OK, 0 rows affected, 1 warning (0.00 sec) mysql>
//安裝依賴包 [root@chouyu ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd [root@chouyu ~]# yum -y install php-* [root@chouyu ~]# php -v [root@chouyu ~]# php -v PHP 7.2.24 (cli) (built: Oct 22 2019 08:28:36) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.24, Copyright (c) 1999-2018, by Zend Technologies [root@chouyu ~]# [root@chouyu ~]# vim /etc/php-fpm.d/www.conf ;listen = /run/php-fpm/www.sock #註釋此行 listen = 127.0.0.1:9000 #新增監聽埠 [root@chouyu ~]# systemctl start php-fpm [root@chouyu ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 127.0.0.1:9000 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 80 *:3306 *:* LISTEN 0 128 *:80 *:* [root@chouyu ~]# [root@localhost ~]# mkdir /usr/local/apache/htdocs/ceshi [root@localhost ~]# vim /usr/local/apache/htdocs/ceshi/index.php <?php phpinfo(); ?> [root@localhost ~]# chown -R apache.apache /usr/local/apache/htdocs/ [root@localhost ~]# vim /etc/httpd24/httpd.conf #在配置檔案的最後加入以下內容 <VirtualHost *:80> DocumentRoot "/usr/local/apache/htdocs/ceshi" ServerName www.ceshi11.com ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/ceshi/$1 <Directory "/usr/local/apache/htdocs/ceshi"> Options none AllowOverride none Require all granted </Directory> </VirtualHost> [root@localhost ~]# vim /etc/httpd24/httpd.conf # If the AddEncoding directives above are commented-out, then you # probably should define those extensions to indicate media types: # AddType application/x-compress .Z AddType application/x-gzip .gz .tgz AddType application/x-httpd-php .php #新增此行 AddType application/x-httpd-php-source .phps #新增此行 ....................................................... # is requested. # <IfModule dir_module> DirectoryIndex index.php index.html #新增index.php [root@localhost ~]# apachectl restart
驗證