LAMP 搭建练习

Posted ddz-linux

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LAMP 搭建练习相关的知识,希望对你有一定的参考价值。

LAMP 搭建

1:CentOS 7, lamp (module);

(1) 三者分离于两台主机

(2) 一个虚拟主机提供phpMyAdmin;另一个虚拟主机提供wordpress

(3) xcache

(4) 为phpMyAdmin提供https虚拟主机

http + php + phpMyAdmin + wordpress 192.168.1.7

yum install http php 
yum install php-mbstring

unzip phpMyAdmin-4.0.10.20-all-languages.zip
mv phpMyAdmin-4.0.10.20-all-languages /var/www/html/phpMyAdmin
cd /var/www/html/phpMyAdmin/
cp config.sample.inc.php config.inc.php

vim config.inc.php
$cfg['blowfish_secret'] = 'a8asdasdfgh11946d';
$cfg['Servers'][$i]['host'] = '192.168.1.10';


tar xvf wordpress-4.9.4-zh_CN.tar.gz -C /var/www/html/
cd /var/www/html/wordpress
cp wp-config-sample.php wp-config.php
vim wp-config.php

/** WordPress数据库的名称 */
define('DB_NAME', 'wpdb');
/** mysql数据库用户名 */
define('DB_USER', 'http');
/** MySQL数据库密码 */
define('DB_PASSWORD', 'centos');
/** MySQL主机 */
define('DB_HOST', '192.168.1.10');

访问wordpress页面安装

systemctl restart httpd

配置虚拟主机

xcache

参考 LAMP PHP 详解 编译安装xcache 文章

phpMyAdmin提供https虚拟主机

mysql 192.168.1.10

yum install mysql
mysql_secure_installation

grant all on *.* to [email protected]'192.168.1.%' identified by 'centos';

2:CentOS 7, lamp (php-fpm)

(1) 三者分离于三台主机

(2) 一个虚拟主机提供phpMyAdmin;另一个虚拟主机提供wordpress

(3) xcache

见独立笔记

3:CentOS 6, lamp (编译安装,模块或php-fpm)

注意:在没有nfs的情况下,如果把php和http分离,需要把网页在php服务器和html服务器,各放一份。!!

主机IP 系统 部署服务
192.168.1.14 Centos6 apache
192.168.1.12 Centos6 php-fpm
192.168.1.13 Centos6 mysql5.6

(1) 三者分离于两台或三台主机

(2) 一个虚拟主机提供phpMyAdmin;另一个虚拟主机提供wordpress

(3) xcache

(4) 尝试mpm为非prefork机制

搭建 apache / 192.168.1.14 (centos6)

#编译环境
yum groupinstall Development -y
yum install pcre-devel openssl-devel expat-devel -y

# apache 账户
groupadd -r -g 80 apache
useradd -r -s /sbin/nologin -g 80 -u 80 apache

#解压包
 cd /alidata/
 tar xvf httpd-2.4.33.tar.bz2
 tar xvf apr-util-1.6.1.tar.gz
 tar xvf apr-1.6.3.tar.gz
 
 mv apr-1.6.3 httpd-2.4.33/srclib/apr
 mv apr-util-1.6.1 httpd-2.4.33/srclib/apr-util
 
 cd httpd-2.4.33
 #编译
 ./configure --prefix=/alidata/httpd --with-mpm=prefork --enable-so --enable-rewrite --enable-mods-shared=all --enable-nonportable-atomics=yes --disable-dav --enable-deflate --enable-cache --enable-disk-cache --enable-mem-cache --enable-file-cache --enable-ssl --with-included-apr --enable-modules=all  --enable-mods-shared=all --enable-cgi
 
 #这条命令可以查看 CPU数
 cat /proc/cpuinfo |grep processor |wc -l
 make -j 12 
 make install
 
 #添加环境变量和启动脚本
 echo PATH=/alidata/httpd/bin:$PATH > /etc/profile.d/http.sh
 cp /alidata/httpd/bin/apachectl /etc/init.d/httpd
 
 #配置主配置文件 (表示要更改和新增的行) 主要是启动模块,修改默认目录,主页
 vim /alidata/httpd/conf/httpd.conf

#去掉下面两行注释启动模块
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
#修改主机名和默认目录
ServerName www.ddz.com:80
#DocumentRoot "/alidata/httpd/htdocs"
DirectoryIndex index.php index.html
 
#添加解析 
AddType application/x-httpd-php .php
Addtype application/x-httpd-php-source .phps

#关闭正向代理,转发到php服务器,这两行不用,因为后面要配置虚拟主机,所以写在虚拟主机的配置里
#ProxyRequests Off 
#ProxyPassMatch ^/(.*.php)$fcgi://192.168.1.13:9000/app/httpd24/htdocs/$1

#允许主配置文件使用虚拟主机配置 取消该行注释
# Virtual hosts
Include conf/extra/httpd-vhosts.conf

配置phpmyadmin 和 wordpress 的虚拟主机配置

#先创建目录
mkdir -pv /alidata/vhosts/{phpmyadmin,wordpress}
 mkdir -pv /var/log/httpd

#配置文件
vim /alidata/httpd/conf/extra/httpd-vhosts.conf

<VirtualHost *:80>
    DocumentRoot "/alidata/vhosts/phpmyadmin"
    ServerName www.phpmyadmin.com
    ErrorLog "/var/log/httpd/phpmyadmin-error_log"
    CustomLog "/var/log/httpd/phpmyadmin-access_log" common
    ProxyRequests Off
    ProxyPassMatch ^/(.*.php)$ fcgi://192.168.1.12:9000/alidata/vhosts/phpmyadmin/$1
    <Directory "/alidata/vhosts/phpmyadmin">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/alidata/vhosts/wordpress"
    ServerName www.blog.com
    ErrorLog "/var/log/httpd/wordpress-error_log"
    CustomLog "/var/log/httpd/wordpress-access_log" common
    ProxyRequests Off
    ProxyPassMatch ^/(.*.php)$ fcgi://192.168.1.12:9000/alidata/vhosts/wordpress/$1
    <Directory "/alidata/vhosts/wordpress">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
</VirtualHost>

至此,apache服务配置完成,重启即可

apachectl restart
service httpd restart

搭建 mysql (5.6) / 192.168.1.14 (centos6)

 hostname mysql
 
 yum groupinstall Development -y
 yum -y install bison bison-devel zlib-devel libcurl-devel libarchive-devel boost-devel gcc gcc-c++ cmake ncurses-devel gnutls-devel libxml2-devel openssl-devel libevent-devel libaio-devel (epel 源)
 
 #准备数据目录
 mkdir /alidata/mysqldb
 chown mysql.mysql /alidata/mysqldb/
 
 #准备包
 cd /alidata/
 wget https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.37.tar.gz
 tar xvf mysql-5.6.37.tar.gz

#准备账户
 groupadd -r mysql  -g 80
 useradd -r -s /sbin/nologin -g 80 -u 80 mysql

#编译安装
cmake . -DCMAKE_INSTALL_PREFIX=/alidata/mysql -DMYSQL_DATADIR=/alidata/mysqldb/ -DSYSCONFDIR=/etc -DMYSQL_USER=mysql -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITHOUT_MROONGA_STORAGE_ENGINE=1 -DWITH_DEBUG=0 -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_LIBWRAP=0 -DENABLED_LOCAL_INFILE=1 -DMYSQL_UNIX_ADDR=/alidata/mysqldb/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci

make -j 12
make install
 
 #添加变量
  echo 'PATH=/alidata/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
 . /etc/profile.d/mysql.sh
 
 cd /alidata/mysql
 #初始化数据库
 scripts/mysql_install_db --datadir=/alidata/mysqldb/ --user=mysql
 
 #做启动服务
 cp support-files/mysql.server /etc/init.d/mysqld
 cd /etc/init.d/
 chkconfig --add mysqld

 
 #编辑数据库配置文件
 vim /etc/my.cnf
 [mysqld]
datadir=/alidata/mysqldb
socket=/alidata/mysqldb/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-bin
port=3306

skip-name-resolve


[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/alidata/mysqldb/mysqld.pid

#启动服务
service mysqld start

#安全初始化
mysql_secure_installation

#创建管理账户
grant all on *.* to [email protected]'192.168.1.%' identified by 'centos';

搭建php-fpm (php 7.1.18) / 192.168.1.12 (centos6)

#编译环境
yum groupinstall Development -y
yum install libxml2-devel bzip2-devel libmcrypt-devel -y
yum install pcre-devel openssl-devel expat-devel -y

#账户准备
groupadd -r -g 80 php
useradd -r -s /sbin/nologin -g 80 -u 80 php

cd /alidata/
tar xvf php-7.1.18.tar.bz2
cd php-7.1.18

#编译安装
./configure --prefix=/alidata/php --enable-mysqlnd --with-mysqli=mysqlnd --with-openssl --with-pdo-mysql=mysqlnd --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --enable-fpm --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --enable-maintainer-zts --disable-fileinfo

make -j 12 
make install

#环境配置
echo 'PATH=/alidata/php/bin:$PATH' > /etc/profile.d/php.sh
. /etc/profile.d/php.sh

#创建配置文件
cp php.ini-production /etc/php.ini

#创建启动服务
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on

#创建配置文件
cd /alidata/php/etc/
cp php-fpm.conf.default php-fpm.conf
cp php-fpm.d/www.conf.default php-fpm.d/www.conf

#修改配置文件
vim /alidata/php/etc/php-fpm.d/www.conf
#修改应用使用者
user = php
group = php

#监听端口和允许访问者
listen = 192.168.1.12:9000
listen.allowed_clients = 192.168.1.14

#看需求改
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 5

#看需求改
vim /etc/php.ini
memory_limit = 1024M

使用phpmyadmin 和 wordpress

phpmyadmin

#创建存放该服务的目录
mkdir -pv /alidata/vhosts/{phpmyadmin,wordpress}
mkdir -pv /var/log/httpd

cd /alidata/
unzip phpMyAdmin-4.8.1-all-languages.zip
mv phpMyAdmin-4.8.1-all-languages/* vhosts/phpmyadmin/

#创建配置文件
cd vhosts/phpmyadmin/
mv config.sample.inc.php config.inc.php

#生成随机字符串
openssl rand -base64 18
vim config.inc.php
$cfg['Servers'][$i]['host'] = '192.168.1.13';

#由于没有nfs 所以复制一份一毛一样的去apache 服务器
scp -r phpmyadmin/* [email protected]:/alidata/vhosts/phpmyadmin/

wordpress

cd /alidata/
tar xvf wordpress-4.9.4-zh_CN.tar.gz -C /alidata/vhosts/

cd /alidata/vhosts
chown -R root.root wordpress/

#设置配置文件
cd wordpress/
cp wp-config-sample.php wp-config.php

vim wp-config.php
define('DB_NAME', 'wpdb');
define('DB_USER', 'http');
define('DB_PASSWORD', 'centos');
define('DB_HOST', '192.168.1.13');


scp -r wordpress [email protected]:/alidata/vhosts/
setfacl -R -m u:php:rwx wordpress/

以上是关于LAMP 搭建练习的主要内容,如果未能解决你的问题,请参考以下文章

Centos系统下Lamp环境的快速搭建(超详细,转)

LAMP的搭建(rpm源代码安装)+

织梦lamp环境搭建

lamp环境搭建

linux运维实战练习-2016年3月4日-3月19日课程作业

linux运维实战练习-2016年3月4日-3月19日课程作业