lamp架構

      1. lamp簡介
        lamp (Web應用軟件組合)
        Linux+Apache+Mysql/MariaDB+Perl/PHP/Python一組常用來搭建動態網站或者服務器的開源軟件,本身都是各自獨立的程序,但是因爲常被放在一起使用,擁有了越來越高的兼容度,共同組成了一個強大的Web應用程序平臺。
        web服務器工作流程
        web服務器的資源分爲兩種,靜態資源和動態資源
        靜態資源就是指靜態內容,客戶端從服務器獲得的資源的表現形式與原文件相同。可以簡單的理解爲就是直接存儲於文件系統中的資源。
        動態資源則通常是程序文件,需要在服務器執行之後,將執行的結果返回給客戶端。
        那麼web服務器如何執行程序並將結果返回給客戶端?,通過一張圖來說明一下web服務器如何處理客戶端的請求
        lamp架構

如上圖所示
階段1顯示的是httpd服務器(即apache)和php服務器通過FastCGI協議進行通信,且php作爲獨立的服務進程運行

階段2顯示的是php程序和mysql數據庫間通過mysql協議進行通信。php與mysql本沒有什麼聯繫,但是由php語言寫成的程序可以mysql進行數據交互。同理Perl和Python寫的程序也可以與mysql數據庫進行交互。

lamp平臺構建
環境說明

系統平臺 IP 需要安裝的服務
centos7,redhat7 192.168.47.12 httpd-2.4,mysql-5.7,php,php-mysql

lamp 平臺軟件安裝次序
httpd --> mysql --> php

安裝httpd
//  安裝開發工具包
[root@yanyinglai3 ~]#  yum groups install 'Development Tools'

//創建apache服務的用戶和組
[root@yanyinglai3 ~]# groupadd -r apache
[root@yanyinglai3 ~]# useradd -r -M -s /sbin/nologin -g apache apache

//安裝依賴包
[root@yanyinglai3 ~]# yum -y install openssl-devel pcre-devel expat-devel libtool

//下載和安裝apr以及apr-util
 [root@yanyinglai3 ~]# cd /usr/src/
 [root@yanyinglai3 src]# wget http://mirrors.shu.edu.cn/apache//apr/apr-1.6.3.tar.bz2

[root@yanyinglai3 src]# wget http://mirrors.shu.edu.cn/apache//apr/apr-util-1.6.1.tar.bz2

[root@yanyinglai3 src]# ls
apr-1.6.3.tar.bz2  apr-util-1.6.1.tar.bz2  debug  kernels

[root@yanyinglai3 src]#  tar xf apr-1.6.3.tar.bz2
[root@yanyinglai3 src]# tar xf apr-util-1.6.1.tar.bz2
[root@yanyinglai3 src]# ls
apr-1.6.3          apr-util-1.6.1          debug
apr-1.6.3.tar.bz2  apr-util-1.6.1.tar.bz2  kernels

[root@yanyinglai3 src]#  cd apr-1.6.3
[root@yanyinglai3 apr-1.6.3]# vim configure
cfgfile="${ofile}T"
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
# $RM "$cfgfile"             //將此行加上註釋,或者刪除此行。

[root@yanyinglai3 apr-1.6.3]#  ./configure --prefix=/usr/local/apr
[root@yanyinglai3 apr-1.6.3]# make && make  install

[root@yanyinglai3 apr-1.6.3]# cd /usr/src/apr-util-1.6.1
[root@yanyinglai3 apr-util-1.6.1]#  ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr

 [root@yanyinglai3 apr-util-1.6.1]# make && make install

// 編譯安裝httpd
    [root@yanyinglai3 ~]# wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.34.tar.bz2

[root@yanyinglai3 ~]# ls
anaconda-ks.cfg  httpd-2.4.34.tar.bz2
[root@yanyinglai3 ~]# tar xf httpd-2.4.34.tar.bz2
[root@yanyinglai3 ~]#  cd 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@yanyinglai3 httpd-2.4.34]#  make && make install
//安裝後配置
  [root@yanyinglai3 ~]# echo 'PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@yanyinglai3 ~]# source /etc/profile.d/httpd.sh
[root@yanyinglai3 ~]#  ln -s /usr/local/apache/include/ /usr/include/httpd
[root@yanyinglai3 ~]#  echo 'MANPATH /usr/local/apache/man' >> /etc/man.config

取消ServerName 前面的註釋
[root@yanyinglai3 ~]#  sed -i '/#ServerName/s/#//g' /etc/httpd24/httpd.conf

//啓動apache
 [root@yanyinglai3 ~]#  apachectl start
[root@yanyinglai3 ~]#  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     :::80                  :::*                  
LISTEN      0      128     :::22                  :::*                  
LISTEN      0      100    ::1:25                  :::*                  

安裝mysql
//安裝依賴包
 [root@yanyinglai3 ~]#  yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel

// 創建用戶和組
[root@yanyinglai3 ~]#  groupadd -r -g 306 mysql
[root@yanyinglai3 ~]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql

//下載二進制格式的mysql軟件包,可以先下載到本地上傳
[root@yanyinglai3 ~]#  cd /usr/src/

lamp架構

// 解壓軟件至/usr/local/
[root@yanyinglai3 src]# ls
apr-1.6.3               debug
apr-1.6.3.tar.bz2       kernels
apr-util-1.6.1          mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
apr-util-1.6.1.tar.bz2

[root@yanyinglai3 src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C/usr/local/
[root@yanyinglai3 src]#  ls /usr/local/
apache    bin    include  libexec                              share
apr       etc    lib      mysql-5.7.22-linux-glibc2.12-x86_64  src
apr-util  games  lib64    sbin

[root@yanyinglai3 ~]#  cd /usr/local/
[root@yanyinglai3 local]#  ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.22-linux-glibc2.12-x86_64/"

[root@yanyinglai3 local]# ll
總用量 0
drwxr-xr-x. 13 root root 152 8月  21 16:31 apache
drwxr-xr-x.  6 root root  58 8月  21 16:12 apr
drwxr-xr-x.  5 root root  43 8月  21 16:16 apr-util
drwxr-xr-x.  2 root root   6 11月  5 2016 bin
drwxr-xr-x.  2 root root   6 11月  5 2016 etc
drwxr-xr-x.  2 root root   6 11月  5 2016 games
drwxr-xr-x.  2 root root   6 11月  5 2016 include
drwxr-xr-x.  2 root root   6 11月  5 2016 lib
drwxr-xr-x.  2 root root   6 11月  5 2016 lib64
drwxr-xr-x.  2 root root   6 11月  5 2016 libexec
lrwxrwxrwx.  1 root root  36 8月  21 17:13 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
drwxr-xr-x.  9 root root 129 8月  21 17:09 mysql-5.7.22-linux-glibc2.12-x86_64
drwxr-xr-x.  2 root root   6 11月  5 2016 sbin
drwxr-xr-x.  5 root root  49 7月  30 17:17 share
drwxr-xr-x.  2 root root   6 11月  5 2016 src

// 修改目錄/usr/local/mysql的屬主屬組
[root@yanyinglai3 local]#  chown -R mysql.mysql /usr/local/mysql
[root@yanyinglai3 local]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 8月  21 17:13 /usr/local/mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/

// 添加環境變量
  [root@yanyinglai3 local]# ls /usr/local/mysql
bin  COPYING  docs  include  lib  man  README  share  support-files
[root@yanyinglai3 local]#  echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@yanyinglai3 local]#  . /etc/profile.d/mysql.sh
[root@yanyinglai3 local]#  echo $PATH
/usr/local/mysql/bin:/usr/local/apache/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

// 建立數據存放目錄
  [root@yanyinglai3 local]#  mkdir /opt/data
[root@yanyinglai3 local]# chown -R mysql.mysql /opt/data/
[root@yanyinglai3 local]# ll /opt/
總用量 0
drwxr-xr-x. 2 mysql mysql 6 8月  21 17:20 data

//初始化數據庫
 [root@yanyinglai3 local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/

//請注意,這個命令會生成一個臨時密碼,此處密碼是Rhq?VqUTS5.t
 一定要記住這個密碼,因爲一會登錄時會用到。

/usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql     62524  62346  2 17:49 pts/0    00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr//配置mysql
  [root@yanyinglai3 local]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
"/usr/local/include/mysql" -> "/usr/local/mysql/include/"

[root@yanyinglai3 local]#  echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf

[root@yanyinglai3 local]#  ldconfig -v

//生成配置文件
 [root@yanyinglai3 ~]# 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@yanyinglai3 ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@yanyinglai3 ~]#  sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@yanyinglai3 ~]#  sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld

//啓動mysql
  [root@yanyinglai3 ~]#  service mysqld start
Starting MySQL.Logging to '/opt/data/yanyinglai3.err'.
. SUCCESS!

[root@yanyinglai3 ~]#  ps -ef|grep mysql
root      62346      1  0 17:49 pts/0    00:00:00 /bin/sh /local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=yanyinglai3.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root      62556   1703  0 17:50 pts/0    00:00:00 grep --color=auto mysq

[root@yanyinglai3 ~]# 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     :::80                  :::*                  
LISTEN      0      128     :::22                  :::*                  
LISTEN      0      100    ::1:25                  :::*                  
LISTEN      0      80      :::3306                :::*                  

//  修改密碼
//  使用臨時密碼登錄
   [root@yanyinglai3 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

// 設置新密碼
  mysql>  set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.06 sec)

mysql> quit
Bye

安裝php
// 配置yum源
 [root@yanyinglai3 ~]# cd /etc/yum.repos.d/
[root@yanyinglai3 yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo

[root@yanyinglai3 ~]#  sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@yanyinglai3 ~]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@yanyinglai3 ~]#  yum -y install epel-release

// 安裝依賴包
 [root@yanyinglai3 ~]#  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-develreadline readline-devel libxslt libxslt-devel mhash mhash-devel

//下載php
[root@yanyinglai3 ~]#  cd /usr/src/
[root@yanyinglai3 src]#  wget http://cn.php.net/distributions/php-7.2.8.tar.xz

//編譯安裝php
  [root@yanyinglai3 src]#  tar xf php-7.2.8.tar.xz
[root@yanyinglai3 src]# cd php-7.2.8
[root@yanyinglai3 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@yanyinglai3 php-7.2.8]#make -j $(cat /proc/cpuinfo |grep processor|wc -l)

[root@yanyinglai3 php-7.2.8]#  make install

//安裝後配置
[root@yanyinglai3 ~]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@yanyinglai3 ~]#  source /etc/profile.d/php7.sh
[root@yanyinglai3 ~]# which php
/usr/local/php7/bin/php
[root@yanyinglai3 ~]#  php -v
PHP 7.2.8 (cli) (built: Aug 21 2018 19:43:38) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

//配置php-fpm
[root@yanyinglai3 php-7.2.8]# cp php.ini-production /etc/php.ini
[root@yanyinglai3 php-7.2.8]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@yanyinglai3 php-7.2.8]# chmod +x /etc/rc.d/init.d/php-fpm
[root@yanyinglai3 php-7.2.8]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@yanyinglai3 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@yanyinglai3 ~]#  vim /usr/local/php7/etc/php-fpm.conf
[root@yanyinglai3 ~]#  tail /usr/local/php7/etc/php-fpm.conf
; files from a glob(3) pattern. This directive can be used everywhere in the
; file.
; Relative path can also be used. They will be prefixed by:
;  - the global prefix if it's been set (-p argument)
;  - /usr/local/php7 otherwise
include=/usr/local/php7/etc/php-fpm.d/*.conf
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8

//啓動php-fpm
[root@yanyinglai3 ~]#  service php-fpm start
Starting php-fpm  done

[root@yanyinglai3 ~]# 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                :::*                  

[root@yanyinglai3 ~]#  ps -ef|grep php
root      84624      1  0 20:02 ?        00:00:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf)
nobody    84625  84624  0 20:02 ?        00:00:00 php-fpm: pool www
nobody    84626  84624  0 20:02 ?        00:00:00 php-fpm: pool www
nobody    84627  84624  0 20:02 ?        00:00:00 php-fpm: pool www
nobody    84628  84624  0 20:02 ?        00:00:00 php-fpm: pool www
nobody    84629  84624  0 20:02 ?        00:00:00 php-fpm: pool www
root      84632  31791  0 20:03 pts/1    00:00:00 grep --color=auto php

//配置apache
啓用httpd的相關模塊
[root@yanyinglai3 ~]#  sed -i '/proxy_module/s/#//g' /etc/httpd24/httpd.conf
[root@yanyinglai3 ~]# sed -i '/proxy_fcgi_module/s/#//g' /etc/httpd24/httpd.conf

//配置虛擬主機
創建虛擬主機目錄並生成php測試頁面
[root@yanyinglai3 ~]#  mkdir /usr/local/apache/htdocs/yanyinglai.com
[root@yanyinglai3 ~]#  cat > /usr/local/apache/htdocs/yanyinglai.com/index.php <<EOF
> <?php
> phpinfo();
> ?>
> EOF
[root@yanyinglai3 ~]#  chown -R apache.apache /usr/local/apache/htdocs/
[root@yanyinglai3 ~]# ll /usr/local/apache/htdocs/ -d
drwxr-xr-x. 3 apache apache 46 8月  21 20:08 /usr/local/apache/htdocs/

[root@yanyinglai3 ~]#  vim /etc/httpd24/httpd.conf
//在配置文件的最後加入以下內容
<VirtualHost *:80>
    DocumentRoot "/usr/local/apache/htdocs/yanyinglai.com"
    ServerName www.yanyinglai.com
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/yanyinglai.com/$1
    <Directory "/usr/local/apache/htdocs/yanyinglai.com">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
</VirtualHost>

[root@yanyinglai3 ~]# 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@yanyinglai3 ~]#  sed -i '/ DirectoryIndex/s/index.html/index.php index.html/g' /etc/httpd24/httpd.conf
//重啓apache服務
[root@yanyinglai3 ~]# apachectl stop
[root@yanyinglai3 ~]#  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     :::22                  :::*                  
LISTEN      0      100    ::1:25                  :::*                  
LISTEN      0      80      :::3306                :::*            

[root@yanyinglai3 ~]# apachectl start
[root@yanyinglai3 ~]# 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                :::*                  

驗證
1.修改/etc/hosts文件,添加域名與ip的映射
在windows電腦上修改, C:\Windows\System32\drivers\etc 文件

2.在瀏覽器上使用域名訪問,若看到以下界面則表示lamp架構搭建成功,否則請檢查你的操作。

lamp架構

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章