httpd之LAMP环境

httpd搭建lamp环境,以及discuz和wordpress的部署...

LAMP环境搭建

本实验,采用异机部署,机httpd和php,mysql的实例不在同一台主机,异机部署时,应注意httpd和php分别处理的动静内容的转发问题,通过nfs等网络共享服务能较好的解决该问题,

编译安装httpd

官方文档:http://httpd.apache.org/docs/2.4/install.html

  1. 安装依赖包,编译工具
  2. configure、make、make install 三步走
  3. 安装后配置
    1. PATH变量、
    2. man文件、头文件、库文件、[可选]
    3. 单独的网页文件存放目录,(一般单独挂载高可用的存储设备,或nfs共享)
    4. 添加apache用户
    5. 服务脚本、配置文件准备

1,安装依赖包,编译安装apr和apr-utils

# 安装依赖包,编译工具
# 包含pcre正则,expat和xml相关的包,会被httpd应用到
[root@host2 ~]# yum install -y pcre-devel expat-devel pcre gcc gcc++

# 编译安装apr
[root@host2 apr-1.6.2]# ./configure --prefix=/usr/local/apr
[root@host2 apr-1.6.2]# make && make install
[root@host2 apr-1.6.2]# ll /usr/local/apr/
total 0
drwxr-xr-x 2 root root  26 Aug 31 19:40 bin
drwxr-xr-x 2 root root 148 Aug 31 19:40 build-1
drwxr-xr-x 3 root root  19 Aug 31 19:40 include
drwxr-xr-x 3 root root 140 Aug 31 19:40 lib


# 编译安装apr-utils,需要在apr之后!
configure: error: APR could not be located. Please use the --with-apr option.
[root@host2 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
[root@host2 apr-util-1.6.1]# make && make install
[root@host2 apr-util-1.6.1]# ll /usr/local/apr-util/
total 0
drwxr-xr-x 2 root root  26 Aug 31 19:42 bin
drwxr-xr-x 3 root root  19 Aug 31 19:42 include
drwxr-xr-x 3 root root 164 Aug 31 19:42 lib

2,三步走

常见编译选项

./configure -h
...
# 查看选项,编译时应该根据实际需求选择


prefix 
程序安装路径

sysconfdir 
配置文件路径,非必须,不指定默认是prefix目录下的conf目录

#
  --enable-mpms-shared=MPM-LIST
  
  
  --with-mpm=MPM          Choose the process model for Apache to use by
                          unless --enable-mpms-shared is also specified.
单独此项,为静态编译一个mpm模型,这样mpm就选死了;
# 
--with-mpm=MPM
mpm模型三选一,如果选择是只静态编译一种mpm模型,则需要更换其他模型时,必须重新编译!
较好方式:3个都动态编译,指定一个为默认,其他2个还可以随时切换;
示例如下:意思是3种动态模型都动态编译,默认为event,安装后还可以用LoadModule指令可以切换其他2种模型
--with-mpm=event --enable-mpms-shared=all

with-z zlib
压缩启用
with-pcre 
正则启用
with-apr
with-apr-utils
启用Apache Portable Runtime:为应用提供通用运行时环境

# 编译时,动态编译了3种mpm模型后,可以在模块目录看到对应的3种动态模块
[root@host2 httpd-2.4.27]# ll /usr/local/apache/modules/ |grep mpm
-rwxr-xr-x 1 apache apache 191360 Aug 18 19:35 mod_mpm_event.so
-rwxr-xr-x 1 apache apache  88720 Aug 18 19:35 mod_mpm_prefork.so
-rwxr-xr-x 1 apache apache 137424 Aug 18 19:35 mod_mpm_worker.so


configure选项中:
--disable-xx默认是开启的模块
--enable-xx默认是关闭的模块

动态/静态编译的区别

  • 静态static编译:编译时,把模块编译到核心模块里面,随着核心模块启动而启动,也就是说和httpd进程的生命周期一致
  • 静态shared编译:把模块编译为二进制文件,但是不随着核心模块启动而启动,而是需要的时候,用LoadModule指令引用;所谓动态,即需要增减的时候,用LoadMoudule加载或注释掉需要的模块,然后发送HUP信号,重读配置文件即实现模块的动态装载或卸载

--enable-so:启用so模块,默认启用,默认静态编译,指定为动态选择不生效

[root@host2 httpd-2.4.27]# httpd -M
Loaded Modules:
 core_module (static)
 so_module (static)
 http_module (static)

一定静态编译的模块so_module,这个模块才使得httpd有了动态装载、卸载模块的能力;
DSO:dymanic shared object 动态共享模块对象;

3,开始编译

# 进入解压后目录,三步走;
# 三种mpm都动态编译,默认用event;启用pcre正则、apr跨平台、启用zlib压缩
[root@host2 httpd-2.4.27]# ./configure --prefix=/usr/local/apache --with-z --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --with-mpm=event --enable-mpms-shared=all

[root@host2 httpd-2.4.27]# make && make install

# 安装后测试
[root@host2 httpd-2.4.27]# /usr/local/apache/bin/httpd -t
Syntax OK
[root@host2 httpd-2.4.27]# /usr/local/apache/bin/httpd -k start
[root@host2 httpd-2.4.27]# curl localhost
<html><body><h1>It works!</h1></body></html>

4,编译安装后配置

# path变量配置
[root@host2 httpd-2.4.27]# cat /etc/profile.d/httpd.sh
export PATH=/usr/local/apache/bin:$PATH
[root@host2 httpd-2.4.27]# source !$
source /etc/profile.d/httpd.sh

# 改httpd的进程启动用户,默认为nobody
[root@host2 httpd-2.4.27]# useradd apache
[root@host2 httpd-2.4.27]# id apache
uid=1000(apache) gid=1000(apache) groups=1000(apache)

[root@host2 apache]# vim conf/httpd.conf 
	...
	User apache
	Group apche
	...
[root@host2 apache]# apachectl -k restart

# 服务脚本准备
实验时,从yum安装中复制过来,然后修改的服务脚本有点问题,无法关闭进程
# 解决方法:命令行方式管理httpd进程,
apachectl脚本,或httpd即可完成程序的启停;
# 再结合利用/etc/rc.local可以实现开机自启动

以下:非必须配置,但path变量一般都需要配置


--
manpath配置
echo "MANPATH /usr/local/apache/man" >> /etc/man.config

--
path配置
echo "PATH=/usr/local/apache/bin:$PATH" >> /etc/profile.d/apache.sh
source /etc/profile.d/apache.sh 


--
头文件引用?
ln -s /usr/include /usr/local/apache/include

service编写,用yum安装的修改即可;
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/local/apache/bin/httpd $OPTIONS -k start -DFOREGROUND
ExecReload=/usr/local/apache/bin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
主要改了httpd进程的路径;
能启动,能访问,但是启动、关闭,都卡住?如果是命令正常,应该是service文件问题;编译没问题;

编译php

整体步骤如下:

  1. 安装依赖包、编译工具;
  2. 三步走
    1. configure:注意:启用php-fpm、mysql连接方式一般选为mysqlnd、
    2. make
    3. make install
  3. 准备:php配置文件,php-fpm配置文件、服务脚本
  4. 配置php-fpm,允许apache主机的连接,修改监听地址;进程用户改为apache
  5. apache httpd和php-fpm同主机情况:注意静态网页根目录和动态脚本目根录同一个;
  6. apache httpd和php-fpm不同主机情况:通过nfs服务,使得网页根目录和脚本目录也为同一个;属主都为apache用户,且uid一致;(php-fpm主机上要创建apache用户,且指定uid一致)

1,安装依赖包

yum install -y bzip2-devel libmcrypt-devel openssl-devel libxml2-devel gcc gcc++ 
devel包不要写成level,写错过!

2,下载源码包,解压,进入解压后目录

[root@php src]# ll
total 17372
-rw-r--r-- 1 root root 17785731 Aug 19 15:26 php-5.5.38.tar.gz
[root@php src]# tar -xf php-5.5.38.tar.gz 
[root@php src]# cd php-5.5.38

3,三步走

#configure:启用php-fpm方式,用mysqlnd连接数据库,指定安装目录,等,

[root@php php-5.5.38]# ./configure --prefix=/usr/local/php --with-openssl --with-mbstring --enable-sockets --with-freetype-dir --with-jpeg-dir --with-png-dir --with-libxml-dir=/usr --enable-xml --with-zlib --with-mcrypt --with-bz2 --with-mhash --with-config-file-path=/etc \--with-config-file-scan-dir=/etc/php.d --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-fpm

#make

#make install

php的configure脚本一些参数说明;



SAPI modules:

  --with-apxs2=FILE       Build shared Apache 2.0 Handler module. FILE is the optional
                          pathname to the Apache apxs tool apxs
php编译为apache模式的方式时,指定,模块的路径
  --enable-fpm            Enable building of the fpm SAPI executable
php单独运行为fpm格式;

General settings:

  --with-config-file-path=PATH
                          Set the path in which to look for php.ini [PREFIX/lib]
php解释器的配置文件                        
  --with-config-file-scan-dir=PATH
                          Set the path where to scan for configuration files
php-fpm的配置文件夹

---
php连接mysql方式2种;libmysql驱动连接、mysqlnd连接
# libmysql时:需先安装mysql,mysql-devel
# --with-mysql指定mysql安装路径
# with-mysqli mysql_config 脚本路径
# with-pdo-mysql mysql安装路径
# 例如:
./configure --prefix=/usr/local/php --with-mysql=/usrlocal/mysql --with-mysqli=/usr/local/mysql/bin/mysql_cofig 
--with-pdo-mysql=/usr/local/mysql


 --with-pdo-mysql=DIR    PDO: MySQL support. DIR is the MySQL base directory
                          If no value or mysqlnd is passed as DIR, the
                         directory /usr/local/freetds
  --with-mysql=DIR        Include MySQL support.  DIR is the MySQL base
                          directory, if no DIR is passed or the value is
                          mysqlnd the MySQL native driver will be used

  --with-mysqli=FILE      Include MySQLi support.  FILE is the path
                          to mysql_config.  If no value or mysqlnd is passed
                          as FILE, the MySQL native driver will be used
                          
                          
# mysqlnd驱动连接时,不需提前安装mysql mysql-devel
mysqlnd作为上面3项的值,然后enable-mysqlnd
# 例子
./configure --prefix=/usr/local/php --enable-mysqld --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd


 --enable-mysqlnd        Enable mysqlnd explicitly, will be done implicitly
                          when required by other extensions

建议用mysqlnd

# 扩展php支持类型的
  --with-vpx-dir=DIR      GD: Set the path to libvpx install prefix
  --with-jpeg-dir=DIR     GD: Set the path to libjpeg install prefix
  --with-png-dir=DIR      GD: Set the path to libpng install prefix
  --with-zlib-dir=DIR     GD: Set the path to libz install prefix
  --with-xpm-dir=DIR      GD: Set the path to libXpm install prefix
  --with-freetype-dir=DIR GD: Set the path to FreeType 2 install prefix
加密,安装,压缩,等;
  --with-libxml-dir=DIR   LIBXML: libxml2 install prefix
  --with-openssl=DIR      Include OpenSSL support (requires OpenSSL >= 0.9.6)
  --with-kerberos=DIR     OPENSSL: Include Kerberos support
  --with-pcre-regex=DIR   Include Perl Compatible Regular Expressions support.
                          DIR is the PCRE install prefix BUNDLED
  --without-sqlite3=DIR   Do not include SQLite3 support. DIR is the prefix to
                          SQLite3 installation directory.
  --with-zlib=DIR         Include ZLIB support (requires zlib >= 1.0.9)
  --with-zlib-dir=<DIR>   Define the location of zlib install directory

4,安装后配置

php的配置文件php.ini

准备php解释器的配置文件;php的一般还有个/etc/php.d/目录

[root@php php-5.5.38]# cp php.ini-production /etc/php.ini
[root@php php-5.5.38]# pwd
/usr/local/src/php-5.5.38

php-fpmd服务脚本

准备php-fpm的服务脚本,加入执行权限

[root@php php-5.5.38]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpmd
[root@php php-5.5.38]# chmod +x /etc/init.d/php-fpmd 
[root@php php-5.5.38]# chkconfig php-fpmd on

php-fpmd的配置文件

准备php-fpm的配置文件;【和php的配置文件不一回事,类比java配置和tomcat的配置】

[root@php php-5.5.38]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

注意:/usr/local/php是编译安装的目录;根据情况修改;

修改监听端口为本机所有地址

[root@php php-5.5.38]# cd /usr/local/php/
[root@php php]# ll
total 0
drwxr-xr-x 2 root root 132 Aug 30 16:54 bin
drwxr-xr-x 2 root root  71 Aug 30 16:58 etc
drwxr-xr-x 3 root root  17 Aug 30 16:54 include
drwxr-xr-x 3 root root  17 Aug 30 16:54 lib
drwxr-xr-x 4 root root  28 Aug 30 16:54 php
drwxr-xr-x 2 root root  21 Aug 30 16:54 sbin
drwxr-xr-x 4 root root  28 Aug 30 16:54 var

[root@php php]# vim etc/php-fpm.conf

# 取消127.0.0.1,即改成监听所有地址

[root@php php]# service php-fpmd start
Starting php-fpm  done
[root@php php]# ss -nlt
State      Recv-Q Send-Q                                                 Local Address:Port                                                                Peer Address:Port              
LISTEN     0      128                                                                *:9000                                                                           *:*                  
LISTEN     0      128                                                                *:22                                                                             *:*        
# php-fpm默认监听9000端口

修改php-fpm的进程用户

httpd或nginx配置向后端的php-fpm转发时,需要2者进程用户一致,避免权限问题;**uid不同时,测试也可以,但最好保持一致**
# 将如下nginx改为apache即可,如下为lnmp时实验配置;

[root@php php]# groupadd -g 2000 nginx
[root@php php]# useradd -u 2000 nginx -g nginx
[root@php php]# id nginx
uid=2000(nginx) gid=2000(nginx) groups=2000(nginx)
# 添加nginx用户,uid最好一致,

[root@php php]# vim etc/php-fpm.conf
[root@php php]# service php-fpmd restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done

# 改为nginx用户启动

[root@php php]# ps aux|grep php
root      81399  0.0  0.4  74912  4196 ?        Ss   17:02   0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
nginx     81400  0.0  0.3  74912  3744 ?        S    17:02   0:00 php-fpm: pool www
nginx     81401  0.0  0.3  74912  3744 ?        S    17:02   0:00 php-fpm: pool www

安装xcache

php不是直接编译成二进制,而是会先编译成字节码;opcode,于是采用xcache做字节码的缓存,提高效率,不用随用随编译;
[root@host2 xcache-3.2.0]# make install
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/

问题:
yum install -y autoconf m4
无法打开htdoc目录
暂略;

二进制mysql安装

1,下载二进制压缩包

2,解压,解压后目录做软链接

[root@mysql local]# tar -xf mysql-5.7.30-el7-x86_64.tar.gz 
[root@mysql local]# ll
...
drwxr-xr-x  9 root root       129 Aug 30 17:10 mysql-5.7.30-el7-x86_64
-rw-r--r--  1 root root 727333633 Aug 20 16:16 mysql-5.7.30-el7-x86_64.tar.gz
...
[root@mysql local]# ln -sv mysql-5.7.30-el7-x86_64 mysql
‘mysql’ -> ‘mysql-5.7.30-el7-x86_64’

3,配置环境变量

[root@mysql local]# vim /etc/profile.d/mysql.sh
[root@mysql local]# source /etc/profile.d/mysql.sh
[root@mysql local]# cat /etc/profile.d/mysql.sh 
export PATH=/usr/local/mysql/bin:$PATH

4,创建数据目录

[root@mysql local]# mkdir /data/mysql -pv
mkdir: created directory ‘/data’
mkdir: created directory ‘/data/mysql’

5,创建用户

[root@mysql local]# useradd mysql
[root@mysql local]# chown -R mysql.mysql /data/mysql/

6,服务脚本

[root@mysql mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@mysql mysql]# pwd
/usr/local/mysql

7,配置文件

# 将原有配置文件备份,并修改
[root@mysql mysql]# cp /etc/my.cnf{,.bak}

[root@mysql mysql]# ll /etc/my.cnf*
-rw-r--r--. 1 root root 570 Jun  8  2017 /etc/my.cnf
-rw-r--r--  1 root root 570 Aug 30 17:27 /etc/my.cnf.bak

# 修改如下
[root@mysql mysql]# vim /etc/my.cnf
[root@mysql mysql]# cat /etc/my.cnf
[mysqld]

datadir=/data/mysql
socket=/data/mysql/mysql.sock

symbolic-links=0

skip_name_resolve=on
[mysqld_safe]
log-error=/data/mysql/mariadb.log
pid-file=/data/mysql/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

6,执行数据库初始化脚本

[root@mysql mysql]# mysql_install_db --datadir=/data/mysql --user=mysql
2020-08-30 17:31:00 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2020-08-30 17:31:00 [ERROR]   Child process: /usr/local/mysql-5.7.30-el7-x86_64/bin/mysqldterminated prematurely with errno= 32
2020-08-30 17:31:00 [ERROR]   Failed to execute /usr/local/mysql-5.7.30-el7-x86_64/bin/mysqld --bootstrap --datadir=/data/mysql --lc-messages-dir=/usr/share/mysql --lc-messages=en_US
-- server log begin --
/usr/local/mysql-5.7.30-el7-x86_64/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory


# 数据库初始化脚本会报错,需要libaio库,安装即可
[root@mysql mysql]# yum install -y libaio

# mysql_install_db脚本废弃,仍会报错,改用mysqld --initialize,会生成随机密码
[root@mysql mysql]# mysqld --initialize
...
[Note] A temporary password is generated for root@localhost: u5qu((8Cp>xs


7,本地连入,配置远程连接用户权限,建库,建用户

[root@mysql mysql]# service mysqld status
 ERROR! MySQL is not running
[root@mysql mysql]# chkconfig mysqld on

[root@mysql mysql]# service mysqld start
Starting MySQL.Logging to '/data/mysql/mariadb.log'.
 SUCCESS! 
[root@mysql mysql]# ss -nlt
                                                                       :::*        
# 连接,用初始化时,生成的随机密码;-S指定通信的socket,默认的/tmp/mysql.sock替换为了/data/mysql/mysql.sock

[root@mysql mysql]# mysql -p"u5qu((8Cp>xs" -S /data/mysql/mysql.sock

# 需先修改密码
mysql> alter user root@'localhost' identified by 'wang';
Query OK, 0 rows affected (0.01 sec)

# 建立远程连接用户
mysql> grant all on *.* to root@'192.168.80.%' identified by 'wang';

httpd配置fastcgi代理转发

1,新建一个虚拟主机,配置fastcgi的转发



[root@host2 httpd]# cat conf.d/lamp.conf 
<Directory "/lamp">
	DirectoryIndex index.php index.html
	# 安装wordpress时,没有该项时,报错,浏览器访问一直显示forbidden,
	# 查看日志发现是index文件问题,默认只找index.html,wordpress里面的index.php文件不会找,自然报错;
	# 加上该行之后,重启,即可访问,执行后续安装步骤
	Require all granted
</Directory>
<VirtualHost *:80>
	ServerName www.bbs.com
	DocumentRoot /lamp
	ProxyPassMatch "^/(.*\.php)$" "fcgi://192.168.80.103:9000/lamp/$1"
</VirtualHost>

# 错误日志如下:
[Tue Sep 01 18:07:21.854468 2020] [autoindex:error] [pid 2623] [client 192.168.80.1:12361] AH01276: Cannot serve directory /lamp/upload/: No matching DirectoryIndex (index.html) found, and server-generated directory index forbidden by Options directive

2,php-fpm配置

  • 监听本机所有ip
  • 允许httpd主机连接
  • 创建apache用户,uid和http主机的apache用户保持一致
  • 提供nfs,供httpd主机挂载;
  • 逻辑和lnmp完全一致,唯一区别是httpd和nginx代理的配置方法,注意小问题,如Directoryindex那个错误;

3,测试index.php页面

​ 向网页文件根目录,(也是php动态脚本根目录)写入测试php页面;

​ 如下:表明lamp部署成功;

image-20200901175252260

部署discuz

1,将解压后的upload目录放在网页文件根目录

2,浏览器访问http://ip/upload,进行安装,(指定数据库信息,安装中会进行创建,wordpress需要提前建库建用户)

3,期间可能会遇到目录权限问题,根据提示添加权限,刷新页面即可继续安装;

部署Wordpress

1,建库建用户

mysql> create database wd;
Query OK, 1 row affected (0.00 sec)

mysql> grant all on wd.* to wd@'192.168.80.%' identified by 'wd';
Query OK, 0 rows affected, 1 warning (0.00 sec)

2,把解压后wordpress目录放到网页文件根目录,

3,浏览器访问http://ip/wordpress,进行安装,根据页面提示即可

4,如果是wordpress下所有内容,直接放到网页文件根目录,则访问http://ip/即可安装;(建议第一种方式)

reference

lnmp实验示例

updatedupdated2020-10-192020-10-19
加载评论