LAMP服务搭建详解

一、LAMP简介

LAMP:Linux Apache MySQL PHP 的简写,即把Apache、MySQL以及PHP安装在Linux系统上,组成一个环境来运行PHP的脚本语言,通常是网站。

Apache是最常用的web服务软件,而MySQL是比较小型的数据库软件,这两个软件以及PHP都可以安装在一台机器上,也可以分开安装,当Apache和PHP可以安装在同一台机器上,PHP是作为Apache的一个模块存在的apxs是httpd的一个工具,因为有它才会自动把PHP模块安装到httpd的modules目录下,也就是说PHP将会以一个模块的形式和httpd结合在一起工作。也可以安装在不同台机器上,此时PHP是作为服务器的一个反向代理存在的。

       a: apache (httpd):接收用户的web请求;静态资源则直接响应;动态资源为php脚本,对此类资源的请求将交由php来运行;

       m: mysql, mariadb:数据管理系统

       p: php, perl, python:运行php程序

http与php结合的方式:

       FastCGI (独立作为服务器)

       modules (把php编译成为httpd的模块)

二、实践作业:部署lamp,以虚拟主机安装wordpress, phpwind, discuz; (centos7为例)

准备工作:
<1.软件安装全部使用yum

Modules:程序包,httpd, php, php-mysql, mariadb-server

FastCGI:程序包,httpd, php-fpm, php-mysql, mariadb-server

<2.关闭所有主机的iptables和selinux,防止影响实验顺利进行,执行如下命令:

[root@centos7 ~]# systemctl stop firewalld
[root@centos7 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@centos7 ~]# setenforce 0
[root@centos7 ~]# vim /etc/selinux/config 
SELINUX=disabled

1、基于lamp搭建wordpress blog

httpd-server主机:

<1. 配置httpd配置文件

【注意】CentOS7下,无需修改httpd的主配置文件,因为CentOS7中,当虚拟主机存在时会默认禁用httpd主机的配置文件(CentOS6下需要禁用掉httpd主机,才可以启用虚拟主机功能,方式是注释掉httpd主配置文件的DocumentRoot一行),所以可以直接进入/etc/httpd/conf.d/目录下新建并编辑虚拟主机配置文件即可

[root@centos7 ~]# yum -y install httpd php mariadb-server php-mysql
[root@centos7~]# systemctl stop firewalld; setenforce 0

新建并编辑虚拟主机配置文件

[root@centos7 ~]# vim /etc/httpd/conf.d/vhost.conf 
</VirtualHost>
<VirtualHost *:80>
ServerName  blog.magedu.com
DocumentRoot "/app/blog/htdocs"
CustomLog "logs/blog.magedu.com_access_log" combined
ErrorLog "logs/blog.magedu.com_error_log" 
<Directory "/app/blog/htdocs">
Require all granted
</Directory>
</VirtualHost>

重启服务

[root@centos7~]# systemctl restart httpd

编辑mysql主配置文件

[root@centos7 ~]# vim /etc/my.cnf
[mysqld]
skip_name_resolve                       //跳过域名解析

重启数据库服务

[root@centos7 ~]# systemctl restart mariadb

创建共享资源目录

[root@centos7 blog]# mkdir -pv /app/blog

安装wordpress程序文件至此目录并设定其配置文件

[root@centos7 blog]# tar xvf wordpress-4.9.4-zh_CN.tar.gz -C /app/blog/
[root@centos7 blog]# mv wordpress wordpress-4.9.4

创建软链接

[root@centos7(nanyibo) blog]# ln -sv wordpress-4.9.4 htdocs
[root@centos7(nanyibo) blog]# ll
total 4
lrwxrwxrwx. 1 root   root        15 Oct 19 15:32 htdocs -> wordpress-4.9.4
drwxr-xr-x. 5 nobody nfsnobody 4096 Feb  8  2018 wordpress-4.9.4

设置共享目录的权限

[root@centos7 ~]# cd /app/blog/
[root@centos7 blog]# setfacl -m u:apache:rwx htdocs/

注意:因为在httpd虚拟主机配置文件中,是根据客户端请求的资源的文件名后缀是否为" .php " 而决定是否使用php-fpm代理的,与后缀为" .php "的文件的内容无关,所以无需修改wordpress文件中的配置文件,index.php文件中也无需有内容,只需在php-fpm代理服务器端处在对应的文件中指定内容即可。

 进入mysql客户端,建立用户、数据库、以及表和字段

[root@centos7 ~]# mysql
MariaDB [(none)]> create database wpdb;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all on wpdb.* to 'wpuser'@'192.168.153.%' identified by 'wppass';
Query OK, 0 rows affected (0.02 sec)

在浏览器输入域名blog.magedu.com开始安装

blob.png

注意:mysql配置文件中不要加额外的东西。我在做mysql主从实验的时候加了read_only,结果在安装的时候报错,是因为数据库不能只读,需要注释掉那一行

2、基于php-fpm模式的lamp搭建phpmyadmin及discuz

[root@centos7 ~]# yum -y install httpd php-fpm php-mysql mariadb-server

设置代理配置文件

[root@centos7~]# vim /etc/php-fpm.d/www.conf
listen = 0.0.0.0:9000                                      此处填写本主机的IP地址(不能写127.0.0.1),端口为必填项
listen.allowed_clients = 172.18.254.202 ==>                     此处填写被代理的httpd服务的主机的IP地址
[root@centos7 ~]# mkdir /var/lib/php/session

修改权限

[root@centos7 ~]# chown apache.apache /var/lib/php/session

重启服务

[root@centos7 ~]# systemctl restart php-fpm
[root@centos7 ~]# ss -ntl |grep 9000
LISTEN     0      128          *:9000                     *:*

编辑修改配置文件

[root@centos7 ~]# vim /etc/httpd/conf.d/pma.conf
DirectoryIndex index.php                
<VirtualHost *:80>                      
ServerName pma.magedu.com
DocumentRoot /vhosts/pam/htdocs
ProxyRequests Off                                //关闭正向代理
ProxyPassMatch ^/(.*\.php)$  fcgi://172.18.254.202:9000/vhosts/pam/htdocs/$1          / /模式匹配其内容
<Directory "/vhosts/pam/htdocs">
Options None                                            
AllowOverride None                                      
Require all granted                                     
</Directory>                                    
</VirtualHost>       
[root@centos7 ~]# cd /vhosts/pam/

安装phpMyAdmin程序文件至此目录并设定其配置文件

[root@centos7 pam]# unzip phpMyAdmin-4.0.10.20-all-languages.zip
[root@centos7 pam]# ln -sv phpMyAdmin-4.0.10.20-all-languages htdocs
‘htdocs’ -> ‘phpMyAdmin-4.0.10.20-all-languages’
[root@centos7 pam]# ll
total 4
lrwxrwxrwx. 1 root root   34 Oct 19 17:27 htdocs -> phpMyAdmin-4.0.10.20-all-languages
drwxr-xr-x. 9 root root 4096 Mar 28  2017 phpMyAdmin-4.0.10.20-all-languages
[root@centos7 htdocs]# cp config.sample.inc.php config.inc.php 
[root@centos7 htdocs]# vim config.inc.php
$cfg['Servers'][$i]['host'] = '172.18.254.202';
[root@centos7 htdocs]# yum -y install php-mbstring
[root@centos7 htdocs]# systemctl restart php-fpm
[root@centos7 htdocs]# mysql
MariaDB [(none)]> grant all on *.* to 'root'@'172.18.254.202' identified by 'magedu' with grant option;                
Query OK, 0 rows affected (0.00 sec)

注意:with grant option:授权用户可以再次授权

通过浏览器输入pma.magedu.com可访问到phpmyadmin并用以上创建的帐户来登录。

blob.png

blob.png

安装discuz

[root@centos7 ~]# cd /etc/httpd/conf.d
[root@centos7 conf.d]# cp pma.conf bbs.conf
[root@centos7 conf.d]# vim bbs.conf 
DirectoryIndex index.php
<VirtualHost *:80>
ServerName bbs.magedu.com
DocumentRoot /vhosts/bbs/htdocs
ProxyRequests Off                           //关闭正向代理
ProxyPassMatch ^/(.*\.php)$  fcgi://172.18.254.202:9000/vhosts/bbs/htdocs/$1       //模式匹配其内容
<Directory "/vhosts/bbs/htdocs">
Options None
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

在windows的hosts中修改

172.18.254.202 bbs.magedu.com

[root@centos7 conf.d]# mkdir /vhosts/bbs -pv
mkdir: created directory ‘/vhosts/bbs’
[root@centos7 ~]# mkdir /vhosts/bbs/discuz-x3.3
[root@centos7 ~]# mv Discuz_X3.3_SC_UTF8.zip /vhosts/bbs/discuz-x3.3
[root@centos7~]# cd /vhosts/bbs/discuz-x3.3
[root@centos7 discuz-x3.3]# unzip Discuz_X3.3_SC_UTF8.zip 
[root@centos7 ~]# cd /vhosts/bbs/

共享的论坛资源都在upload目录,所以只需将它放在此目录下即可

[root@centos7 bbs]# ln -s discuz-x3.3/upload htdocs
[root@centos7 bbs]# ll
total 0
drwxr-xr-x. 5 root root 80 Oct 19 17:41 discuz-x3.3
lrwxrwxrwx. 1 root root 18 Oct 19 17:42 htdocs -> discuz-x3.3/upload
[root@centos7 bbs]# setfacl -R -m u:apache:rwx htdocs/

通过phpmyadmin为discuz创建数据库及用户并授权。然后在浏览器上输入bbs.magedu.com进行安装discuz

blob.png

编辑windows的hosts

win+r  打开运行:notepad C:\Windows\System32\drivers\etc\hosts

172.18.254.202 bbs.magedu.com

在浏览器输入域名bbs.magedu.com开始安装,登录。

blob.png

blob.png

猜你喜欢

转载自blog.51cto.com/13869470/2307086