LAMP架构源码部署(入门级别超详细步骤)
1. lamp簡介
有了前面學(xué)習(xí)的知識的鋪墊,今天可以來學(xué)習(xí)下第一個(gè)常用的web架構(gòu)了。
所謂lamp,其實(shí)就是由Linux+Apache+Mysql/MariaDB+Php/Perl/Python的一組動態(tài)網(wǎng)站或者服務(wù)器的開源軟件,除Linux外其它各部件本身都是各自獨(dú)立的程序,但是因?yàn)榻?jīng)常被放在一起使用,擁有了越來越高的兼容度,共同組成了一個(gè)強(qiáng)大的Web應(yīng)用程序平臺。
LAMP指的是Linux(操作系統(tǒng))、Apache(HTTP服務(wù)器)、MySQL(也指MariaDB,數(shù)據(jù)庫軟件)和PHP(有時(shí)也是指Perl或Python)的第一個(gè)字母,一般用來建立web應(yīng)用平臺。
2. web服務(wù)器工作流程
在說lamp架構(gòu)平臺的搭建前,我們先來了解下什么是CGI,什么是FastCGI,什么是......
web服務(wù)器的資源分為兩種,靜態(tài)資源和動態(tài)資源
靜態(tài)資源就是指靜態(tài)內(nèi)容,客戶端從服務(wù)器獲得的資源的表現(xiàn)形式與原文件相同。可以簡單的理解為就是直接存儲于文件系統(tǒng)中的資源
動態(tài)資源則通常是程序文件,需要在服務(wù)器執(zhí)行之后,將執(zhí)行的結(jié)果返回給客戶端
2.1 cgi與fastcgi
上圖階段①中提到了FastCGI,下面我們來了解下CGI與FastCGI。
CGI(Common Gateway Interface,通用網(wǎng)關(guān)接口),CGI是外部應(yīng)用程序(CGI程序)與WEB服務(wù)器之間的接口標(biāo)準(zhǔn),是在CGI程序和Web服務(wù)器之間傳遞信息的過程。CGI規(guī)范允許Web服務(wù)器執(zhí)行外部程序,并將它們的輸出發(fā)送給Web瀏覽器,CGI將web的一組簡單的靜態(tài)超媒體文檔變成一個(gè)完整的新的交互式媒體。
FastCGI(Fast Common Gateway Interface)是CGI的改良版,CGI是通過啟用一個(gè)解釋器進(jìn)程來處理每個(gè)請求,耗時(shí)且耗資源,而FastCGI則是通過master-worker形式來處理每個(gè)請求,即啟動一個(gè)master主進(jìn)程,然后根據(jù)配置啟動幾個(gè)worker進(jìn)程,當(dāng)請求進(jìn)來時(shí),master會從worker進(jìn)程中選擇一個(gè)去處理請求,這樣就避免了重復(fù)的生成和殺死進(jìn)程帶來的頻繁cpu上下文切換而導(dǎo)致耗時(shí)
2.2 httpd與php結(jié)合的方式
httpd與php結(jié)合的方式有以下三種:
- modules:php將以httpd的擴(kuò)展模塊形式存在,需要加載動態(tài)資源時(shí),httpd可以直接通過php模塊來加工資源并返回給客戶端
- httpd prefork:libphp5.so(多進(jìn)程模型的php)
- httpd event or worker:libphp5-zts.so(線程模型的php)
- CGI:httpd需要加載動態(tài)資源時(shí),通過CGI與php解釋器聯(lián)系,獲得php執(zhí)行的結(jié)果,此時(shí)httpd負(fù)責(zé)與php連接的建立和斷開等
- FastCGI:利用php-fpm機(jī)制,啟動為服務(wù)進(jìn)程,php自行運(yùn)行為一個(gè)服務(wù),https通過socket與php通信
較于CGI方式,FastCGI更為常用,很少有人使用CGI方式來加載動態(tài)資源
2.3 web工作流程
通過上面的圖說明一下web的工作流程:
- 客戶端通過http協(xié)議請求web服務(wù)器資源
- web服務(wù)器收到請求后判斷客戶端請求的資源是靜態(tài)資源或是動態(tài)資源
- 若是靜態(tài)資源則直接從本地文件系統(tǒng)取之返回給客戶端。
- 否則若為動態(tài)資源則通過FastCGI協(xié)議與php服務(wù)器聯(lián)系,通過CGI程序的master進(jìn)程調(diào)度worker進(jìn)程來執(zhí)行程序以獲得客戶端請求的動態(tài)資源,并將執(zhí)行的結(jié)果通過FastCGI協(xié)議返回給httpd服務(wù)器,httpd服務(wù)器收到php的執(zhí)行結(jié)果后將其封裝為http響應(yīng)報(bào)文響應(yīng)給客戶端。在執(zhí)行程序獲取動態(tài)資源時(shí)若需要獲得數(shù)據(jù)庫中的資源時(shí),由Php服務(wù)器通過mysql協(xié)議與MySQL/MariaDB服務(wù)器交互,取之而后返回給httpd,httpd將從php服務(wù)器收到的執(zhí)行結(jié)果封裝成http響應(yīng)報(bào)文響應(yīng)給客戶端。
3. lamp平臺構(gòu)建
環(huán)境說明:
| centos7 redhat7 | 192.168.102.10 | httpd-2.4 mysql-5.7 php php-mysql |
lamp平臺軟件安裝次序:
httpd --> mysql --> php3.1 安裝httpd
//下載和解壓httpd、apr以及apr-util [root@localhost src]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.49.tar.gz [root@localhost src]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz [root@localhost src]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz [root@localhost src]# tar xf httpd-2.4.49.tar.gz [root@localhost src]# tar xf apr-util-1.6.1.tar.gz [root@localhost src]# tar xf apr-1.7.0.tar.gz [root@localhost apr-1.7.0]# vim configure 31878 cfgfile=${ofile}T 31879 trap "$RM \"$cfgfile\"; exit 1" 1 2 15 31880 # $RM "$cfgfile" 將此行加上注釋,或者刪除此行 31881 31882 cat <<_LT_EOF >> "$cfgfile"// 安裝EPEL rpm 包 [root@localhost apr-1.7.0]# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm -y [root@localhost apr-1.7.0]# ls /etc/yum.repos.d/epel-modular.repo epel-testing-modular.repo wjj.repo epel-playground.repo epel-testing.repo epel.repo redhat.repo [root@localhost apr-1.7.0]# yum clean all //清理緩存 Updating Subscription Management repositories. Unable to read consumer identity This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. 33 文件已刪除 [root@localhost apr-1.7.0]# yum makecache //重新建立緩存 Updating Subscription Management repositories. Unable to read consumer identity This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. Extra Packages for Enterprise Linux 563 kB/s | 955 kB 00:01 Extra Packages for Enterprise Linux 616 kB/s | 10 MB 00:17 BaseOS 164 MB/s | 2.3 MB 00:00 AppStream 126 MB/s | 5.8 MB 00:00 元數(shù)據(jù)緩存已建立。// 安裝開發(fā)工具包 [root@localhost apr-1.7.0]# yum groups mark install 'Development Tools' -y Updating Subscription Management repositories. Unable to read consumer identity This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. 上次元數(shù)據(jù)過期檢查:0:01:26 前,執(zhí)行于 2021年09月23日 星期四 17時(shí)49分10秒。 依賴關(guān)系解決。 =====================================================================軟件包 架構(gòu) 版本 倉庫 大小 ===================================================================== 安裝組:Development Tools事務(wù)概要 =====================================================================完畢!// 創(chuàng)建apache服務(wù)的用戶和組 [root@localhost src]# useradd -r -M -s /sbin/nologin apache// 安裝依賴包 [root@localhost src]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make// 編譯安裝apr-1.7.0、apr-util-1.6.1、httpd-2.4.49 [root@localhost apr-1.7.0]# ./configure --prefix=/usr/local/apr [root@localhost apr-1.7.0]# make && make install [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 [root@localhost httpd-2.4.49]# ./configure --prefix=/usr/local/apache \ --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@localhost httpd-2.4.49]# 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 20 MANDATORY_MANPATH /usr/man 21 MANDATORY_MANPATH /usr/share/man 22 MANDATORY_MANPATH /usr/local/share/man 23 MANDATORY_MANPATH /usr/local/apache/man // 把a(bǔ)pache加進(jìn)去//取消ServerName前面的注釋 [root@localhost ~]# vim /usr/local/apache/conf/httpd.conf 203 ServerName www.example.com:80//啟動apache [root@localhost ~]# apachectl start [root@localhost ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 80 *:3306 *:* // 配置開機(jī)自啟 [root@localhost ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/httpd.service [root@localhost ~]# vim /usr/lib/systemd/system/httpd.service [root@localhost ~]# cat /usr/lib/systemd/system/httpd.service [Unit] Description=Httpd server daemon Documentation=man:httpd(8) After=network.target[Service] Type=forking ExecStart=/usr/local/apache/bin/apachectl start ExecStop=/usr/local/apache/bin/apachectl stop ExecReload=/bin/kill -HUP $MAINPID[Install] WantedBy=multi-user.target [root@localhost ~]# systemctl daemon-reload [root@localhost ~]# systemctl enable --now httpd Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service. [root@localhost ~]# systemctl status httpd.service ● httpd.service - Httpd server daemonLoaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; v>Active: active (running) since Thu 2021-09-23 18:47:11 CST; 10s a>Docs: man:httpd(8)3.2 安裝mysql
// 安裝依賴包 [root@localhost ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel// 下載mysql軟件包 [root@localhost src]# ls apr-1.7.0 httpd-2.4.49 apr-1.7.0.tar.gz httpd-2.4.49.tar.gz apr-util-1.6.1 kernels apr-util-1.6.1.tar.gz mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz debug//創(chuàng)建用戶和組 [root@localhost ~]# useradd -r -M -s /sbin/nologin mysql//解壓軟件至/usr/local/ [root@localhost src]# tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/ [root@localhost src]# cd /usr/local/ [root@localhost local]# mv mysql-5.7.34-linux-glibc2.12-x86_64/ mysql//修改目錄/usr/local/mysql的屬主屬組 [root@localhost local]# chown -R mysql.mysql mysql///添加環(huán)境變量 [root@localhost ~]# echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh [root@localhost ~]# source /etc/profile.d/mysql.sh [root@localhost ~]# vim /etc/man_db.conf 20 MANDATORY_MANPATH /usr/man 21 MANDATORY_MANPATH /usr/share/man 22 MANDATORY_MANPATH /usr/local/share/man 23 MANDATORY_MANPATH /usr/local/apache/man 24 MANDATORY_MANPATH /usr/local/mysql/man //把mysql加進(jìn)去 [root@localhost ~]# ln -s /usr/local/mysql/include/ /usr/include/mysql/// 寫一個(gè)配置文件,告訴庫文件lib在/usr/local/mysql下面 [root@localhost ~]# cat /etc/ld.so.conf.d/mysql.conf /usr/local/mysql/lib [root@localhost ~]# ldconfig //建立數(shù)據(jù)存放目錄 [root@localhost ~]# ldconfig [root@localhost ~]# mkdir /opt/data [root@localhost ~]# chown -R mysql.mysql /opt/data///初始化數(shù)據(jù)庫 [root@localhost ~]# mysqld --initialize-insecure --user mysql --datadir /opt/data 2021-09-23T11:04:41.643189Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2021-09-23T11:04:41.799190Z 0 [Warning] InnoDB: New log files created, LSN=45790 2021-09-23T11:04:41.821815Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2021-09-23T11:04:41.875518Z 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: 0d6341e8-1c5e-11ec-b0eb-000c29f6d306. 2021-09-23T11:04:41.876088Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2021-09-23T11:04:42.525643Z 0 [Warning] CA certificate ca.pem is self signed. 2021-09-23T11:04:42.602903Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.//配置mysql [root@localhost ~]# cat /etc/my.cnf [mysqld] basedir = /usr/local/mysql datadir = /opt/data socket = /tmp/mysql.sock port = 3306 pid-file = /opt/data/mysql.pid user = mysql skip-name-resolve//配置服務(wù)啟動腳本 [root@localhost ~]# vim /usr/local/mysql/support-files/mysql.server46 basedir=/usr/local/mysql47 datadir=/opt/data [root@localhost ~]# cp /usr/lib/systemd/system/httpd.service /usr/lib/systemd/system/mysqld.service [root@localhost ~]# cat /usr/lib/systemd/system/mysqld.service [Unit] Description=mysql server daemon After=network.target[Service] Type=forking ExecStart=/usr/local/mysql/support-files/mysql.server start ExecStop=/usr/local/mysql/support-files/mysql.server stop ExecReload=/bin/kill -HUP $MAINPID[Install] WantedBy=multi-user.target//啟動mysql [root@localhost ~]# systemctl daemon-reload [root@localhost ~]# systemctl enable --now mysqld.service [root@localhost ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 80 *:3306 *:* [root@localhost ~]# systemctl status mysqld.service ● mysqld.service - mysql server daemonLoaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; >Active: active (running) since Thu 2021-09-23 19:14:13 CST; 13s a>Process: 673923 ExecStart=/usr/local/mysql/support-files/mysql.ser>Main PID: 673945 (mysqld_safe)//修改密碼 [root@localhost ~]# yum -y install ncurses-compat-libs [root@localhost ~]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.34 MySQL Community Server (GPL)Copyright (c) 2000, 2021, Oracle and/or its affiliates.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('yibie!'); Query OK, 0 rows affected, 1 warning (0.01 sec)mysql> exit Bye3.3 安裝php
//安裝依賴包 [root@localhost ~]# 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 [root@localhost ~]# yum -y install php-mysqlnd// 在php.net網(wǎng)站上下載php軟件包,然后傳到/usr/src下面去,再解壓 [root@localhost src]# ls apr-1.7.0 httpd-2.4.49 apr-1.7.0.tar.gz httpd-2.4.49.tar.gz apr-util-1.6.1 kernels apr-util-1.6.1.tar.gz mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz debug php-8.0.10.tar.xz [root@localhost src]# tar xf php-8.0.10.tar.xz //編譯安裝php [root@localhost php-8.0.10]# yum -y install libsqlite3x-devel [root@localhost src]# yum install autoconf automake libtool -y [root@localhost src]# wget https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz -O oniguruma-6.9.4.tar.gz [root@localhost src]# tar xf oniguruma-6.9.4.tar.gz [root@localhost src]# cd oniguruma-6.9.4/ [root@localhost src]# ./autogen.sh && ./configure --prefix=/usr [root@localhost src]# make && make install [root@localhost php-8.0.10]# yum -y install libzip-devel [root@localhost php-8.0.10]# ./configure --prefix=/usr/local/php8 \ --with-config-file-path=/etc \ --enable-fpm \ --disable-debug \ --disable-rpath \ --enable-shared \ --enable-soap \ --with-openssl \ --enable-bcmath \ --with-iconv \ --with-bz2 \ --enable-calendar \ --with-curl \ --enable-exif \ --enable-ftp \ --enable-gd \ --with-jpeg \ --with-zlib-dir \ --with-freetype \ --with-gettext \ --enable-mbstring \ --enable-pdo \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd \ --with-readline \ --enable-shmop \ --enable-simplexml \ --enable-sockets \ --with-zip \ --enable-mysqlnd-compression-support \ --with-pear \ --enable-pcntl \ --enable-posix +--------------------------------------------------------------------+ | 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. [root@localhost php-8.0.10]# make && make install//安裝后配置 [root@localhost ~]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh [root@localhost ~]# source /etc/profile.d/php.sh [root@localhost php-8.0.10]# which php /usr/local/php8/bin/php [root@localhost php-8.0.10]# php -v PHP 8.0.10 (cli) (built: Sep 23 2021 20:29:40) ( NTS ) Copyright (c) The PHP Group Zend Engine v4.0.10, Copyright (c) Zend Technologies//配置php-fpm [root@localhost php-8.0.10]# cp php.ini-production /etc/php.ini cp:是否覆蓋'/etc/php.ini'? y [root@localhost php-8.0.10]# cd sapi/ [root@localhost sapi]# cp fpm/init.d.php-fpm /etc/init.d/php-fpm [root@localhost sapi]# chmod +x /etc//init.d/php-fpm [root@localhost php-8.0.10]# cd /usr/local/php8/ [root@localhost php8]# cd etc/ //此etc是PHP8目錄下的etc [root@localhost etc]# cp php-fpm.conf.default php-fpm.conf [root@localhost etc]# cd php-fpm.d/ [root@localhost php-fpm.d]# cp www.conf.default www.conf//啟動php-fpm [root@localhost ~]# cp /usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/php-fpm.service [root@localhost ~]# vim /usr/lib/systemd/system/php-fpm.service [Unit] Description=php server daemon After=network.target[Service] Type=forking ExecStart=/etc/init.d/php-fpm start ExecStop=/etc/init.d/php-fpm stop ExecReload=/bin/kill -HUP $MAINPID[Install] WantedBy=multi-user.target[root@localhost ~]# systemctl daemon-reload [root@localhost ~]# systemctl enable --now php-fpm [root@localhost ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 127.0.0.1:9000 0.0.0.0:* LISTEN 0 80 *:3306 *:* LISTEN 0 128 0.0.0.0:80 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 128 0.0.0.0:22 0.0.0.0:*配置apache
啟用代理模塊
在apache httpd 2.4以后已經(jīng)專門有一個(gè)模塊針對FastCGI的實(shí)現(xiàn),此模塊為mod_proxy_fcgi.so,它其實(shí)是作為mod_proxy.so模塊的擴(kuò)展,因此,這兩個(gè)模塊都要加載,編輯httpd.conf文件,取消以下兩行內(nèi)容的注釋:
文件
//創(chuàng)建虛擬主機(jī)目錄并生成php測試頁面 [root@localhost ~]# cd /usr/local/apache/ [root@localhost apache]# cd htdocs/ [root@localhost htdocs]# mkdir test [root@localhost htdocs]# cat test/index.php <?phpphpinfo(); ?> [root@localhost apache]# chown -R apache.apache /usr/local/apache/htdocs/[root@localhost apache]# vim conf/httpd.conf //在配置文件的最后加入以下內(nèi)容 514 <VirtualHost *:80> 515 DocumentRoot "/usr/local/apache/htdocs/test" 516 ServerName www.test.com 517 ProxyRequests Off 518 ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/a pache/htdocs/test/$1 519 <Directory "/usr/local/apache/htdocs/test"> 520 Options none 521 AllowOverride none 522 Require all granted 523 </Directory> 524 </VirtualHost>//搜索AddType,添加以下內(nèi)容 397 AddType application/x-compress .Z 398 AddType application/x-gzip .gz .tgz 399 AddType application/x-httpd-php .php 400 AddType application/x-httpd-php-source .phps260 <IfModule dir_module> 261 DirectoryIndex index.php index.html //加上index.php 262 </IfModule>//重啟apache服務(wù) [root@localhost apache]# systemctl restart httpd.service驗(yàn)證
訪問ip驗(yàn)證
網(wǎng)頁輸入apache IP 地址 得到以下界面則成功? (因原圖片過期,附上新的測試頁面 192.168.57.131/index.php)
總結(jié)
以上是生活随笔為你收集整理的LAMP架构源码部署(入门级别超详细步骤)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hp刀片服务器性能分析,IBM刀片服务器
- 下一篇: 程序流程图规范