Linux中詳細搭建lamp架構
阿新 • • 發佈:2018-08-20
fas all master 網站 The 運行環境 -c apr ons 1.LAMP架構介紹
那麽web服務器如何執行程序並將結果返回給客戶端呢?下面通過一張圖來說明一下web服務器如何處理客戶端的請求:
若是靜態資源則直接從本地文件系統取之返回給客戶端
否則若為動態資源則通過FastCGi協議與php服務器聯系,通過CGI程序的master進程調度worker進程來執行程序以獲得客戶端請求的動態資源,並將執行的結果通過FastCGI協議返回給httpd服務器,httpd服務器收到的php的執行結果後將其封裝為http響應報文響應給客戶端。在執行程序獲取動態資源時若需要獲得數據庫中的資源時,由php服務器通過mysql協議與Mysql/MariaDB服務器交互,取之而後返回給httpd,httpd將從php服務器收到的執行結果封裝為http響應給客戶端
LAMP是Linux+Apache(httpd)+MySQL+PHP的簡寫,即把Apache、MySQL以及PHP安裝在linux系統上,組成一個運行環境來運行PHP腳本語言,通常是網站。比如Google、淘寶、百度、51cto博客、猿課論壇等就是用PHP語言寫出來的。
2.web服務器工作流程
在說lamp架構平臺的搭建前,我們先來了解下什麽的CGI,什麽是FastCGI,什麽是 web服務器的資源分為兩種,靜態資源和動態資源。
靜態資源就是指靜態內容,客戶端從服務器獲得的資源的表現形式與原文件相同,可以簡單的理解為就是直接存儲於文件系統中的 資源。
動態資源則通常是程序文件,需要在服務器執行之後,將執行的結果返回給客戶端。
如上圖所示
階段①顯示的是httpd服務器(即apache)和php服務器通過FastCGI協議進行通信,且php作為獨立的服務器進程運行
階段②顯示的是php程序和mysql數據庫間通過mysql協議進行通信。php與mysql本沒有什麽聯系,但是由php語言寫成的程序可以與mysql進行數據交互。同理perl和python寫的程序也可以與mysql數據庫進行交互
通過上面的圖說明一下web的工作流程:
客戶端通過http協議請求web服務器資源
web服務器收到請求後判斷客戶端請求的資源是靜態資源還是動態資源
否則若為動態資源則通過FastCGi協議與php服務器聯系,通過CGI程序的master進程調度worker進程來執行程序以獲得客戶端請求的動態資源,並將執行的結果通過FastCGI協議返回給httpd服務器,httpd服務器收到的php的執行結果後將其封裝為http響應報文響應給客戶端。在執行程序獲取動態資源時若需要獲得數據庫中的資源時,由php服務器通過mysql協議與Mysql/MariaDB服務器交互,取之而後返回給httpd,httpd將從php服務器收到的執行結果封裝為http響應給客戶端
3.lamp平臺構建
環境說明:
系統平臺 | IP | 需要安裝的服務 |
---|---|---|
centos7 redhat7 | 192.168.209.12 | httpd-2.4 mysql-5.7 php php-mysql |
lamp平臺軟件安裝次序:
http -- > mysql -- > php
註意:php要求httpd使用prowork MPM
4.實例演示:
//關閉防火墻firewalld
//關閉seLinux
1.安裝httpd
//安裝開發工具包
[root@lanzhiyong ~]# yum groups mark install ‘Development Tools‘
//創建apache服務的用戶和組
[root@lanzhiyong ~]# groupadd -r apache
[root@lanzhiyong ~]# useradd -r -M -s /sbin/nologin -g apache apache
//安裝依賴包
[root@lanzhiyong ~]# yum -y install openssl-devel pcre-devel expat-devel libtool
//下載和安裝apar以及apr-util
[root@lanzhiyong ~]# cd /usr/src/
[root@lanzhiyong src]# wget http://mirrors.shu.edu.cn/apache//apr/apr-1.6.3.tar.bz2
[root@lanzhiyong src]# wget http://mirrors.shu.edu.cn/apache//apr/apr-util-1.6.1.tar.bz2
[root@lanzhiyong src]# tar xf apr-1.6.3.tar.bz2
[root@lanzhiyong src]# tar xf apr-util-1.6.1.tar.bz2
[root@lanzhiyong src]# cd apr-1.6.3
[root@lanzhiyong apr-1.6.3]# vim configure
/$cfgfile //將此行加上註釋
[root@lanzhiyong apr-1.6.3]# ./configure --prefix=/usr/local/apr
[root@lanzhiyong apr-1.6.3]# make -j 8 && make install
[root@lanzhiyong src]# cd apr-util-1.6.1
[root@lanzhiyong apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@lanzhiyong apr-util-1.6.1]# make -j 8 && make install
//編譯安裝httpd
[root@lanzhiyong ~]# wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.34.tar.bz2
[root@lanzhiyong ~]# tar xf httpd-2.4.34.tar.bz2
[root@lanzhiyong ~]# cd httpd-2.4.34
[root@lanzhiyong httpd-2.4.34]# ./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@lanzhiyong httpd-2.4.34]# make -j 4 && make install
//安裝後配置
[root@lanzhiyong ~]# echo ‘PATH=/usr/local/apache/bin:$PATH‘ > /etc/profile.d/httpd.sh
[root@lanzhiyong ~]# source /etc/profile.d/httpd.sh
[root@lanzhiyong ~]# ln -s /usr/local/apache/include/ /usr/include/httpd
[root@lanzhiyong ~]# echo ‘MANPATH /usr/local/apache/man‘ >> /etc/man_db.conf
//取消ServerName前面的註釋
[root@lanzhiyong ~]# sed -i ‘/#ServerName/s/#//g‘ /etc/httpd24/httpd.conf
//啟動apache
[root@lanzhiyong ~]# apachectl start
[root@lanzhiyong ~]# ss -antl
2.安裝mysql 數據庫
//安裝依賴 包
[root@lanzhiyong ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
//創建用戶和組
[root@lanzhiyong ~]# cd /usr/src/
[root@lanzhiyong src]# groupadd -r -g 306 mysql
[root@lanzhiyong src]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql
//下載二進制格式的mysql軟件包
[root@lanzhiyong src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
//解壓軟件至/usr/local
[root@lanzhiyong src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@lanzhiyong src]# cd /usr/local/
//創建軟連接
[root@lanzhiyong local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
//修改目錄/usr/local/mysql的屬主屬組
[root@lanzhiyong local]# chown -R mysql.mysql /usr/local/mysql
[root@lanzhiyong local]# ll /usr/local/mysql
//添加環境變量
[root@lanzhiyong ~]# ls /usr/local/mysql
[root@lanzhiyong ~]# echo ‘export PATH=/usr/local/mysql/bin:$PATH‘ > /etc/profile.d/mysql.sh
[root@lanzhiyong ~]# source /etc/profile.d/mysql.sh
[root@lanzhiyong ~]# echo $PATH
//建立數據存放目錄
[root@lanzhiyong ~]# mkdir /opt/data
[root@lanzhiyong ~]# chown -R mysql.mysql /opt/data/
[root@lanzhiyong ~]# ll /opt/
總用量 0
drwxr-xr-x. 2 mysql mysql 6 8月 19 13:20 data
//初始化數據庫 註意這個命令後會生成臨時密碼 要記住 jd?ajfrKY4pQ
[root@lanzhiyong ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
//配置mysql
//軟連接
[root@lanzhiyong ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
[root@lanzhiyong ~]# echo ‘/usr/local/mysql/lib‘ > /etc/ld.so.conf.d/mysql.conf
[root@lanzhiyong ~]# ldconfig -v
//生成配置文件
[root@lanzhiyong ~]# cat > /etc/my.cnf << EOF
> [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
> EOF
//配置服務啟動腳本
[root@lanzhiyong ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@lanzhiyong ~]# sed -ri ‘s#^(basedir=).*#\1/usr/local/mysql#g‘ /etc/init.d/mysqld
[root@lanzhiyong ~]# sed -ri ‘s#^(datadir=).*#\1/opt/data#g‘ /etc/init.d/mysqld
//啟動mysql
[root@lanzhiyong ~]# service mysqld start
[root@lanzhiyong ~]# ps -ef |grep mysql
[root@lanzhiyong ~]# ss -antl
//修改密碼 使用臨時密碼登入
[root@lanzhiyong ~]# mysql -uroot -p
jd?ajfrKY4pQ 這是以上步驟的臨時密碼
mysql> set password = password(‘lanzhiyong‘);
3.安裝php
//配置yum源
[root@lanzhiyong ~]# cd /etc/yum.repos.d/
[root@lanzhiyong yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@lanzhiyong ~]# sed -i ‘s/\$releasever/7/g‘ /etc/yum.repos.d/CentOS7-Base-163.repo
[root@lanzhiyong ~]# sed -i ‘s/^enabled=.*/enabled=1/g‘ /etc/yum.repos.d/CentOS7-Base-163.repo
[root@lanzhiyong ~]# yum install -y epel-release
//安裝依賴包
[root@lanzhiyong ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel
//下載php
[root@lanzhiyong ~]# cd /usr/src/
[root@lanzhiyong src]# wget http://cn.php.net/distributions/php-7.2.8.tar.xz
//編譯安裝php [root@lanzhiyong src]# tar xf php-7.2.8.tar.xz
[root@lanzhiyong src]# cd php-7.2.8
[root@lanzhiyong php-7.2.8]#
./configure --prefix=/usr/local/php7 --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir=/usr --with-mysqli=/usr/local/mysql/bin/mysql_config --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-jpeg-dir --with-png-dir --with-xmlrpc --with-xsl --with-zlib --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml --enable-zip
[root@lanzhiyong php-7.2.8]# make -j $(cat /proc/cpuinfo |grep processor |wc -l)
[root@lanzhiyong php-7.2.8]# make install
//安裝後配置
[root@lanzhiyong ~]# echo ‘export PATH=/usr/local/php7/bin:$PATH‘ > /etc/profile.d/php7.sh
[root@lanzhiyong ~]# source /etc/profile.d/php7.sh
[root@lanzhiyong ~]# cd /usr/src/php-7.2.8
[root@lanzhiyong php-7.2.8]# which php
/usr/local/php7/bin/php
[root@lanzhiyong php-7.2.8]# php -v
//配置php-fpm
[root@lanzhiyong php-7.2.8]# cp php.ini-production /etc/php.ini
[root@lanzhiyong php-7.2.8]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@lanzhiyong php-7.2.8]# chmod +x /etc/rc.d/init.d/php-fpm
[root@lanzhiyong php-7.2.8]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@lanzhiyong php-7.2.8]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
//配置fpm的相關選項為你所需的值:
[root@lanzhiyong ~]# vim /usr/local/php7/etc/php-fpm.conf
*****在配置文件最後面添加一下內容
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
[root@lanzhiyong ~]# tail /usr/local/php7/etc/php-fpm.conf
//啟動php-fpm
[root@lanzhiyong ~]# service php-fpm start
Starting php-fpm done
[root@lanzhiyong ~]# ss -antl
******php-fpm監聽在9000端口
[root@lanzhiyong ~]# ps -ef |grep php
//////////////////配置apache
//啟用httpd的相關模塊
[root@lanzhiyong ~]# sed -i ‘/proxy_module/s/#//g‘ /etc/httpd24/httpd.conf
[root@lanzhiyong ~]# sed -i ‘/proxy_fcgi_module/s/#//g‘ /etc/httpd24/httpd.conf
///////////////////////配置虛擬主機
//創建虛擬主機目錄並生成php測試頁面
[root@lanzhiyong ~]# mkdir /usr/local/apache/htdocs/lanzhiyong.com
[root@lanzhiyong ~]# cat > /usr/local/apache/htdocs/lanzhiyong.com/index.php << EOF
> <?php
> phpinfo();
> ?>
> EOF
[root@lanzhiyong ~]# chown -R apache.apache /usr/local/apache/htdocs/
[root@lanzhiyong ~]# ll -d /usr/local/apache/htdocs/
drwxr-xr-x. 3 apache apache 46 8月 19 15:35 /usr/local/apache/htdocs/
//在配置文件的最後加入一下內容
[root@lanzhiyong ~]# vim /etc/httpd24/httpd.conf
<VirtualHost *:80>
ServerName www.lanzhiyong.com
DocumentRoot "/usr/local/apache/htdocs/lanzhiyong.com"
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/lanzhiyong.com/$1
<Directory "/usr/local/apache/htdocs/lanzhiyong.com">
Options none
AllowOverride none
Require all granted
</Directory>
</VirtualHost>
</VirtualHost>
[root@lanzhiyong ~]# vim /etc/httpd24/httpd.conf
//搜索AddType 添加以下兩行
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php //添加此行
AddType application/x-httpd-php-source .phps //添加此行
[root@lanzhiyong ~]# sed -i ‘/ DirectoryIndex/s/index.html/index.php index.html/g‘ /etc/httpd24/httpd.conf
//重啟apache服務
[root@lanzhiyong ~]# apachectl stop
[root@lanzhiyong ~]# ss -antl
[root@lanzhiyong ~]# apachectl start
[root@lanzhiyong ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 127.0.0.1:9000 *:*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
5.驗證:
1.修改/etc/hosts文件,添加域名與IP的映射
2.在瀏覽器上使用域名訪問,若看到以下界面則表示lamp架構搭建成功,否則請檢查你的操作
Linux中詳細搭建lamp架構