首页 > 代码库 > linux架构学习第二十六天之LAMP架构原理及搭建详解
linux架构学习第二十六天之LAMP架构原理及搭建详解
内容:
1、何为LAMP
2、LAMP的架构以及通信过程
3、LAMP的搭建过程(rpm包)
4、基于LAMP搭建wordpress博客
一、何为LAMP
LAMP(Linux-Apache-MySQL-PHP)网站架构是目前国际流行的Web框架,该框架包括:Linux操作系统,Apache网络服务器,MySQL数据库,Perl、PHP或者Python编程语言,所有组成产品均是开源软件,是国际上成熟的架构框架,很多流行的商业应用都是采取这个架构,LAMP具有通用、跨平台、高性能、低价格的优势,因此LAMP无论是性能、质量还是价格都是企业搭建网站的首选平台。
二、LAMP的架构以及通信过程
LAMP的架构:
LAMP是一个多C/S架构的平台,最初级为web客户端基于TCP/IP通过http协议发起传送,这个请求可能是动态的,也可能是静态的。
所以web服务器通过发起请求的后缀来判断,如果是静态的资源就由web服务器自行处理,然后将资源发给客户端。如果是动态这时web服务器会通过CGI(Common Gateway interface)协议发起给php。
这里但是如果php是以模块形式与Web服务器联系。那么他们是通过内部共享内存的方式。如果是php单独的放置与一台服务器,那么他们是通过sockets套接字监听的方式通信(这又是一个C/S架构)。
这时php会相应的执行一段程序,如果在执行程序时,需要用到数据。那么php就会通过mysql协议发送给mysql服务器(也可以看作是一个C/S架构)。由mysql服务器处理,将数据供给php程序。
lamp流程:
1. 用户发送http请求到达httpd服务器
2. httpd解析url获取需要的资源的路径,通过内核空间读取硬盘资源,如是静态资源,则构建响应报文,发回给用户
3. 如果是动态资源,将资源地址发给php解析器,解析php程序文件,解析完毕将内容发回给httpd,httpd构建响应报文,发回给用户
4. 如果涉及到数据库操作,则利用php-mysql驱动,获取数据库数据,返回给PHP解析器。
A,M,P是怎么联动起来工作的呢:
1、apache + php结合的方式大概几种:
第一种:把php编译时直接编译成apache的模块、module模块化的方式进行工作(apahce默认的这种方式)。
第二种:CGI、通用网关接口、apache基于CGI跟hph通信
第三种:fastcgi、他也是一种协议、在这种模块下他们两个是这样结合的:
本来php是做为一个模块或都是php解析器运行的,不是监听在某个套接字上接收别人的请求的,而是让别人调用为一个进程使用的,可能是做为别人的子进程在运行,但是工作在fastcgi这种模块下的hph自行启用为一个服务进程,
他监听在某个套接字上,随时可以接受来自客户端的请求的,他也是有一个主进程的,为了可以响应多个用户的请求,他会启用多个子进程,这些子进程我们也可以称为工作进程,
他也是有空闲进程的,一但有客户请求他马上使用空闲的进程响应客户端的请求,将结果返回给前端的调用者,在php5.3.3版本之前他是没有这个能力了,只能工作在模块和CGI的方式下,而在5.3.3之后这个模块直接被收进php模块中,这种模块就叫php-fpm。
所以在以后编译php时,要想跟apache结合,就要编译成php-fpm,这是基于fastcgi工作的模式,并启动这服务进程,也就意味着他是通过套接字跟前端的调用者通信,既然基于套按字通信了,那么前端的web服务器和后面的php服务器完全可以工作在不同的主机上,实现了所谓的分层机制。
apache不会跟数据库打交道,他是个静态web服务器,跟数据库打交道的是应用程序,作为应用程序的源驱动能够基于某个API跟服务器之间建立会话,而后他会通过我们的mysql语句发送给数据库,数据库再将结果返回给应用程序,不是php进程,而是php进程中所执行的代码。
2、php + mysql的通信:
PHP跟mysql怎么整合起来呢,php又怎么被httpd所调用呢
首先httpd并不具备解析代码的能力,他要依赖于php的解析器,接着php本身不依赖于mysql,他只是一个解析器,能执行代码就OK了,那他什么时候用到mysql呢,如果要在mysql中存数据时才用到mysql,只是当php中有运行mysql语句时才用到mysql。
php语言要想联系mysql,通常用到php的驱动,rpm包的叫php_mysql,php跟mysql没有一点关系,只有程序员在php中编写mysql语句时才连接mysql来执行sql语句的。
基于php-mysql去连接mysql只使用一个函数mysql_connect();而mysql_connect()正是php-mysql提供的一个API,只要指明要连接的服务器即可。
三、LAMP搭建过程(基于rpm包):
(1)安装配置apache,基于虚拟主机模式的https协议
(2)安装配置mysql
(3)安装配置php,以及php-mysql模块
(4)基于LAMP搭建wordpress测试
设置虚拟主机配置:
[14:09 root@centos6.8/etc/httpd/ssl]# cat /etc/httpd/conf.d/www.hillpress.com.conf namevirtualhost *:80 <virtualhost *:80> documentroot "/var/www/html/www.hillpress.com" servername www.hillpress.com errorlog logs/error_log_www.hillpress.com loglevel warn customlog logs/access_log_www.hillpress.com combined </virtualhost>
申请签署证书(我的CA服务和web服务在同一主机):
[14:06 root@centos6.8/etc/httpd/ssl]# openssl req -new -days 365 -key private/hillpress.key -out /etc/httpd/ssl/hillpresscsr You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter ‘.‘, the field will be left blank. ----- Country Name (2 letter code) [XX]:CN State or Province Name (full name) []:BEIJING Locality Name (eg, city) [Default City]:BEIJING Organization Name (eg, company) [Default Company Ltd]:nihao Organizational Unit Name (eg, section) []:www.hillpress.com Common Name (eg, your name or your server‘s hostname) []:www.hillpress.com Email Address []:hill@hill.com Please enter the following ‘extra‘ attributes to be sent with your certificate request A challenge password []: An optional company name []: [14:07 root@centos6.8/etc/httpd/ssl]# openssl ca -in hillpress.csr -out hillpress.crt Using configuration from /etc/pki/tls/openssl.cnf Check that the request matches the signature Signature ok Certificate Details: Serial Number: 3 (0x3) Validity Not Before: Sep 30 06:07:43 2016 GMT Not After : Sep 30 06:07:43 2017 GMT Subject: countryName = CN stateOrProvinceName = BEIJING organizationName = nihao organizationalUnitName = www.hillpress.com commonName = www.hillpress.com emailAddress = hill@hill.com X509v3 extensions: X509v3 Basic Constraints: CA:FALSE Netscape Comment: OpenSSL Generated Certificate X509v3 Subject Key Identifier: 88:05:22:19:70:36:13:D6:6B:43:A4:51:C3:8D:02:66:00:6A:D8:C0 X509v3 Authority Key Identifier: keyid:5B:E9:DA:5B:0B:3F:76:99:E6:96:FA:C0:E4:41:CD:7B:C4:21:7A:4A Certificate is to be certified until Sep 30 06:07:43 2017 GMT (365 days) Sign the certificate? [y/n]:y 1 out of 1 certificate requests certified, commit? [y/n]y Write out database with 1 new entries Data Base Updated
安装mod_ssl模块支持ssl协议:
[14:11 root@centos6.8/etc/httpd/ssl]#rpm -ql mod_ssl /etc/httpd/conf.d/ssl.conf /usr/lib64/httpd/modules/mod_ssl.so /var/cache/mod_ssl /var/cache/mod_ssl/scache.dir /var/cache/mod_ssl/scache.pag /var/cache/mod_ssl/scache.sem
配置https的ssl:/etc/httpd/conf.d/ssl.conf,主要是配置根目录、证书的位置
[14:15 root@centos6.8/etc/httpd/ssl]# grep "^[^#]" /etc/httpd/conf.d/ssl.conf LoadModule ssl_module modules/mod_ssl.so Listen 443 SSLPassPhraseDialog builtin SSLSessionCache shmcb:/var/cache/mod_ssl/scache(512000) SSLSessionCacheTimeout 300 SSLMutex default SSLRandomSeed startup file:/dev/urandom 256 SSLRandomSeed connect builtin SSLCryptoDevice builtin <VirtualHost _default_:443> DocumentRoot "/var/www/html/www.hillpress.com" ServerName www.mypress.com ErrorLog logs/ssl_error_log TransferLog logs/ssl_access_log LogLevel warn SSLEngine on SSLProtocol all -SSLv2 SSLCipherSuite DEFAULT:!EXP:!SSLv2:!DES:!IDEA:!SEED:+3DES SSLCertificateFile /etc/httpd/ssl/hillpress.crt SSLCertificateKeyFile /etc/httpd/ssl/private/hillpress.key <Files ~ "\.(cgi|shtml|phtml|php3?)$"> SSLOptions +StdEnvVars </Files> <Directory "/var/www/cgi-bin"> SSLOptions +StdEnvVars </Directory> SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0 CustomLog logs/ssl_request_log "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b" </VirtualHost>
检查语法,重载服务:
[14:15 root@centos6.8/etc/httpd/ssl]# httpd -t Syntax OK [14:16 root@centos6.8/etc/httpd/ssl]# service httpd reload Reloading httpd: [14:16 root@centos6.8/etc/httpd/ssl]#
(2)安装mysql:
[16:37 root@centos6.8/misc/cd]# yum install -y mysql-server Loaded plugins: fastestmirror, refresh-packagekit, security Setting up Install Process Repository ‘base‘ is missing name in configuration, using id Loading mirror speeds from cached hostfile Resolving Dependencies --> Running transaction check ---> Package mysql-server.x86_64 0:5.1.73-7.el6 will be installed --> Processing Dependency: mysql = 5.1.73-7.el6 for package: mysql-server-5.1.73-7.el6.x86_64 --> Processing Dependency: perl-DBI for package: mysql-server-5.1.73-7.el6.x86_64 --> Processing Dependency: perl-DBD-MySQL for package: mysql-server-5.1.73-7.el6.x86_64 --> Processing Dependency: perl(DBI) for package: mysql-server-5.1.73-7.el6.x86_64 --> Running transaction check ---> Package mysql.x86_64 0:5.1.73-7.el6 will be installed ---> Package perl-DBD-MySQL.x86_64 0:4.013-3.el6 will be installed ---> Package perl-DBI.x86_64 0:1.609-4.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ========================================================================================================================= Package Arch Version Repository Size ========================================================================================================================= Installing: mysql-server x86_64 5.1.73-7.el6 base 8.6 M Installing for dependencies: mysql x86_64 5.1.73-7.el6 base 894 k perl-DBD-MySQL x86_64 4.013-3.el6 base 134 k perl-DBI x86_64 1.609-4.el6 base 705 k Transaction Summary ========================================================================================================================= Install 4 Package(s) Total download size: 10 M Installed size: 29 M Downloading Packages: ------------------------------------------------------------------------------------------------------------------------- Total 12 MB/s | 10 MB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : perl-DBI-1.609-4.el6.x86_64 1/4 Installing : perl-DBD-MySQL-4.013-3.el6.x86_64 2/4 Installing : mysql-5.1.73-7.el6.x86_64 3/4 Installing : mysql-server-5.1.73-7.el6.x86_64 4/4 Verifying : mysql-server-5.1.73-7.el6.x86_64 1/4 Verifying : mysql-5.1.73-7.el6.x86_64 2/4 Verifying : perl-DBD-MySQL-4.013-3.el6.x86_64 3/4 Verifying : perl-DBI-1.609-4.el6.x86_64 4/4 Installed: mysql-server.x86_64 0:5.1.73-7.el6 Dependency Installed: mysql.x86_64 0:5.1.73-7.el6 perl-DBD-MySQL.x86_64 0:4.013-3.el6 perl-DBI.x86_64 0:1.609-4.el6 Complete! [16:38 root@centos6.8/misc/cd]# service mysqld start Initializing MySQL database: Installing MySQL system tables... OK Filling help tables... OK To start mysqld at boot time you have to copy support-files/mysql.server to the right place for your system PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER ! To do so, start the server, then issue the following commands: /usr/bin/mysqladmin -u root password ‘new-password‘ /usr/bin/mysqladmin -u root -h centos6.8 password ‘new-password‘ Alternatively you can run: /usr/bin/mysql_secure_installation which will also give you the option of removing the test databases and anonymous user created by default. This is strongly recommended for production servers. See the manual for more instructions. You can start the MySQL daemon with: cd /usr ; /usr/bin/mysqld_safe & You can test the MySQL daemon with mysql-test-run.pl cd /usr/mysql-test ; perl mysql-test-run.pl Please report any problems with the /usr/bin/mysqlbug script! [ OK ] Starting mysqld: [ OK ]
(3)安装php,以及php-mysql模块:
[16:40 root@centos6.8/misc/cd]# yum -y install php php_mysql Loaded plugins: fastestmirror, refresh-packagekit, security Setting up Install Process Repository ‘base‘ is missing name in configuration, using id Loading mirror speeds from cached hostfile No package php_mysql available. Resolving Dependencies --> Running transaction check ---> Package php.x86_64 0:5.3.3-47.el6 will be installed --> Processing Dependency: php-common(x86-64) = 5.3.3-47.el6 for package: php-5.3.3-47.el6.x86_64 --> Processing Dependency: php-cli(x86-64) = 5.3.3-47.el6 for package: php-5.3.3-47.el6.x86_64 --> Running transaction check ---> Package php-cli.x86_64 0:5.3.3-47.el6 will be installed ---> Package php-common.x86_64 0:5.3.3-47.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ========================================================================================================================= Package Arch Version Repository Size ========================================================================================================================= Installing: php x86_64 5.3.3-47.el6 base 1.1 M Installing for dependencies: php-cli x86_64 5.3.3-47.el6 base 2.2 M php-common x86_64 5.3.3-47.el6 base 530 k Transaction Summary ========================================================================================================================= Install 3 Package(s) Total download size: 3.8 M Installed size: 13 M Downloading Packages: ------------------------------------------------------------------------------------------------------------------------- Total 8.8 MB/s | 3.8 MB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : php-common-5.3.3-47.el6.x86_64 1/3 Installing : php-cli-5.3.3-47.el6.x86_64 2/3 Installing : php-5.3.3-47.el6.x86_64 3/3 Verifying : php-common-5.3.3-47.el6.x86_64 1/3 Verifying : php-cli-5.3.3-47.el6.x86_64 2/3 Verifying : php-5.3.3-47.el6.x86_64 3/3 Installed: php.x86_64 0:5.3.3-47.el6 Dependency Installed: php-cli.x86_64 0:5.3.3-47.el6 php-common.x86_64 0:5.3.3-47.el6 Complete! [20:44 root@centos6.8~]# yum install -y php-mysql Loaded plugins: fastestmirror, refresh-packagekit, security Setting up Install Process Repository ‘base‘ is missing name in configuration, using id Loading mirror speeds from cached hostfile Resolving Dependencies --> Running transaction check ---> Package php-mysql.x86_64 0:5.3.3-47.el6 will be installed --> Processing Dependency: php-pdo(x86-64) for package: php-mysql-5.3.3-47.el6.x86_64 --> Running transaction check ---> Package php-pdo.x86_64 0:5.3.3-47.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ========================================================================================================================= Package Arch Version Repository Size ========================================================================================================================= Installing: php-mysql x86_64 5.3.3-47.el6 base 86 k Installing for dependencies: php-pdo x86_64 5.3.3-47.el6 base 80 k Transaction Summary ========================================================================================================================= Install 2 Package(s) Total download size: 166 k Installed size: 384 k Downloading Packages: ------------------------------------------------------------------------------------------------------------------------- Total 261 kB/s | 166 kB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : php-pdo-5.3.3-47.el6.x86_64 1/2 Installing : php-mysql-5.3.3-47.el6.x86_64 2/2 Verifying : php-mysql-5.3.3-47.el6.x86_64 1/2 Verifying : php-pdo-5.3.3-47.el6.x86_64 2/2 Installed: php-mysql.x86_64 0:5.3.3-47.el6 Dependency Installed: php-pdo.x86_64 0:5.3.3-47.el6 Complete!
php配置文件不需要额外修改,但要修改httpd配置文件,添加支持php的MIME类型:
# vim /etc/httpd22/httpd.conf //找到AddType application/x在下面添加 AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps DirectoryIndex index.php index.html/找到 DirectoryIndex index.html 添加index.php
测试apache与php的连通性:
将首页index.html改成index.php,并且编辑首页内容,添加php的测试语句
[17:18 root@centos6.8/var/www/html/www.hillpress.com]# cat !$ cat index.php welcome to mypage <?php phpinfo(); ?>
测试php与mysql的连通性:
[17:45 root@centos6.8/var/www/html/www.hilldiscuz.com]# cat nihao.php <?php $link = mysql_connect(‘localhost‘,‘root‘,‘nihao‘); if ($link) echo "Success..."; else echo "Failure..."; mysql_close(); ?>
(4)基于LAMP搭建wordpress测试:
1)讲SElinux关闭,或者添加SElinux安全上下文策略
2)把下载的wordprss源码包解压到web的服务根目录,并且修改文件的属组属主
3)修改wordprss的配置文件:wp-config-sample.php,并将名字改为wp-config.php,修改数据库的位置以及登录的帐号密码
[22:26 root@centos6.8/var/www/html/www.hillpress.com]# cat wp-config.php <?php /** * WordPress基础配置文件。 * * 这个文件被安装程序用于自动生成wp-config.php配置文件, * 您可以不使用网站,您需要手动复制这个文件, * 并重命名为“wp-config.php”,然后填入相关信息。 * * 本文件包含以下配置选项: * * * MySQL设置 * * 密钥 * * 数据库表名前缀 * * ABSPATH * * @link https://codex.wordpress.org/zh-cn:%E7%BC%96%E8%BE%91_wp-config.php * * @package WordPress */ // ** MySQL 设置 - 具体信息来自您正在使用的主机 ** // /** WordPress数据库的名称 */ define(‘DB_NAME‘, ‘myDB‘); /** MySQL数据库用户名 */ define(‘DB_USER‘, ‘wpuser‘); /** MySQL数据库密码 */ define(‘DB_PASSWORD‘, ‘wppasswd‘); /** MySQL主机 */ define(‘DB_HOST‘, ‘localhost‘); /** 创建数据表时默认的文字编码 */ define(‘DB_CHARSET‘, ‘utf8‘); /** 数据库整理类型。如不确定请勿更改 */ define(‘DB_COLLATE‘, ‘‘); /**#@+ * 身份认证密钥与盐。 * * 修改为任意独一无二的字串! * 或者直接访问{@link https://api.wordpress.org/secret-key/1.1/salt/ * WordPress.org密钥生成服务} * 任何修改都会导致所有cookies失效,所有用户将必须重新登录。 * * @since 2.6.0 */ define(‘AUTH_KEY‘, ‘put your unique phrase here‘); define(‘SECURE_AUTH_KEY‘, ‘put your unique phrase here‘); define(‘LOGGED_IN_KEY‘, ‘put your unique phrase here‘); define(‘NONCE_KEY‘, ‘put your unique phrase here‘); define(‘AUTH_SALT‘, ‘put your unique phrase here‘); define(‘SECURE_AUTH_SALT‘, ‘put your unique phrase here‘); define(‘LOGGED_IN_SALT‘, ‘put your unique phrase here‘); define(‘NONCE_SALT‘, ‘put your unique phrase here‘); /**#@-*/ /** * WordPress数据表前缀。 * * 如果您有在同一数据库内安装多个WordPress的需求,请为每个WordPress设置 * 不同的数据表前缀。前缀名只能为数字、字母加下划线。 */ $table_prefix = ‘wp_‘; /** * 开发者专用:WordPress调试模式。 * * 将这个值改为true,WordPress将显示所有用于开发的提示。 * 强烈建议插件开发者在开发环境中启用WP_DEBUG。 * * 要获取其他能用于调试的信息,请访问Codex。 * * @link https://codex.wordpress.org/Debugging_in_WordPress */ define(‘WP_DEBUG‘, false); /** * zh_CN本地化设置:启用ICP备案号显示 * * 可在设置→常规中修改。 * 如需禁用,请移除或注释掉本行。 */ define(‘WP_ZH_CN_ICP_NUM‘, true); /* 好了!请不要再继续编辑。请保存本文件。使用愉快! */ /** WordPress目录的绝对路径。 */ if ( !defined(‘ABSPATH‘) ) define(‘ABSPATH‘, dirname(__FILE__) . ‘/‘); /** 设置WordPress变量和包含文件。 */ require_once(ABSPATH . ‘wp-settings.php‘);
4)为wordpress创建专门的数据库用户,并测试成功
MariaDB [(none)]> create database myDB; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> GRANT ALL ON myDB.* TO ‘wpuser‘@‘%‘ IDENTIFIED BY ‘wppasswd‘; Query OK, 0 rows affected (0.00 sec)
本文出自 “6638225” 博客,转载请与作者联系!
linux架构学习第二十六天之LAMP架构原理及搭建详解