LAMP
—-LAMP是 Linux + apache (httpd) + MySQL (Mariadb )+ PHP (prel Python) 一组常用来搭建动态网站或者服务器的开源软件,主流的还有LNMP 即 Linux + Nginx +MySQL (Mariadb )+ PHP (prel Python)
实验 – 编译安装LAMP 应用到Wordpress
实验环境:
- 两台CentOS 7
- 192.168.3.3 搭建 Apache httpd PHP Wordpress
- 192.168.3.10 搭建 Mysql
- 需要的软件包
- PHP – php-7.1.10.tar
- Mariadb – mariadb-10.2.8-linux-x86_64.tar
- httpd – httpd-2.4.27.tar
- apr – apr-util-1.6.0.tar , apr-1.6.2.tar
- Wordpress – wordpress-4.8.1-zh_CN.tar
步骤
1.编译安装 httpd
httpd 依赖有几个组件和一个组包需要事先安装好
yum groupinstall "development tools"
yum install openssl-devel expat-devel pcre-devel
我们事先把该需要的软件放至到/app的目录下了
cd /app
tar xvf apr-1.6.2.tar.gz
tar xvf apr-util-1.6.0.tar.gz
tar xvf httpd-2.4.27.tar.bz2
因为httpd依赖apr的两个组件,所以我们事先要把这两个组件放至对应的目录下(其实yum 源里自带的apr版本是够的,只是我们想全部编译安装最新的。centos 6 如果要编辑2.4的httpd 自身的yum 源里apr的版本太低 )
cp -r apr-1.6.2 httpd-2.4.27/srclib/apr
cp -r apr-util-1.6.0 httpd-2.4.27/srclib/apr-util
接下来我们编译安装 httpd
cd httpd-2.4.27/
./configure --prefix=/app/httpd24 \
--sysconfdir=/etc/httpd24 \
--enable-so --enable-ssl \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-included-apr \ 这里的apr因为是我们之前已放至到对应的目录里了,所以不需要指明路径
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork mpm的模式可以是其它模式
make -j 4 && make install 如果你CPU给力 可以使用 -j 4 来进行并行安装
vim /etc/profile.d/lamp.sh 创建个PATH变量路径让开机自动启动,因为我们还需要使用httpd的命令
PATH=/app/httpd24/bin/:$PATH
. /etc/profile.d/lamp.sh 重新加载
ss -tnl 查看下httpd服务端口开了没 端口是80端口
apachectl 使用这个命令开启服务 apachectl stop 是关闭服务
2.安装mariadb
切换至 另一台数据库服务器
因为我们之前把mariadb的包已下载好所以直接解压至指定的/usr/local 目录里即可 (/usr/local 官方建意放至到此目录下)
tar xvf mariadb-10.2.8-linux-x86_64.tar.gz -C /usr/local/
cd /usr/local
ln -s mariadb-10.2.8-linux-x86_64/ mysql 因为mariadb是mysql的一个分支,而且内置的一些配置都是基
于mysql这个目录名进行路径定义的,所以我们避免麻烦直接创
建个软连接
useradd -r -m -d /app/mysqldb -s /sbin/nologin mysql 创建个数据库用户,指定家目录为单独的目录下是有意义
的,因为之后所搭建的数据表都会放至到这个目录
cd mysql/
scripts/mysql_install_db --datadir=/app/mysqldb --user=mysql 指明数据库目录路径和用户
mkdir /etc/mysql
cp support-files/my-large.cnf /etc/mysql/my.cnf 创建mariadb配置文件,还需要进行编辑
vim /etc/mysql/my.cnf 在mysqld下加三条配置
[mysqld]
datadir = /app/mysqldb
innodb_file_per_table = ON 这条配置是指明一个表一个文件
skip_name_resolve = ON
cp support-files/mysql.server /etc/init.d/mysqld 创建开机启动项
chkconfig --add mysqld
chkconfig --list
chkconfig mysqld on
mkdir /var/log/mariadb 创建日志文件,因为是编辑安装的,日志文件需要自己手动
创建,还要给权限
chown mysql /var/log/mariadb/
service mysqld start
vim /etc/profile.d/lamp.sh 创建个PATH变量的脚本
PATH=/usr/local/mysql/bin/:$PATH
. /etc/profile.d/lamp.sh
mysql_secure_installation 初始化mysql
[root@localhost mysql]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n
... skipping.
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
mysql -uroot -pcentos 进入数据库,-u -p 是指定user和password的 不要加空格
create database wpdb; 创建一个wordpress的数据库,
grant all on wpdb.* to wpuser@'192.168.25.%' identified by 'centos'; 创建用户并授权 wpuser
grant all on wpdb.* to wpuser@'127.%' identified by 'centos';
grant all on wpdb.* to wpuser@'localhost' identified by 'centos';
3.在192.168.3.3这台服务器源码编译安装php
yum install libxml2-devel bzip2-devel libmcrypt-devel (epel源) PHP有依赖的组件,事先装好
tar xvf php-7.1.10.tar.xz
cd php-7.1.10/
./configure \
--prefix=/app/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 \
--with-apxs2=/app/httpd24/bin/apxs \
--with-mcrypt \
--with-config-file-path=/etc \
--with-config-file-scan-dir=/etc/php.d \
--enable-maintainer-zts \
--disable-fileinfo
make && make install
cp php.ini-production /etc/php.ini
vim /etc/httpd24/httpd.conf
在文件尾部加两行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
修改下面行
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
apachectl stop
apachectl
4.测试php和mariadb的连接
vim /app/httpd24/htdocs/index.php
<html><body><h1> LAMP</h1></body></html>
<?php
$mysqli=new mysqli("localhost","root","centos");
if(mysqli_connect_errno()){
echo "连接数据库失败!";
$mysqli=null;
exit;
}
echo "连接数据库成功!";
$mysqli->close();
phpinfo();
?>
5.配置wordpress
tar xvf wordpress-4.8.1-zh_CN.tar.gz -C /app/httpd24/htdocs
cd /app/httpd24/htdocs
mv wordpress/ blog/
cd /app/httpd24/htdocs/blog/
cp wp-config-sample.php wp-config.php
vim wp-config.php
define('DB_NAME', 'wpdb');
/** MySQL数据库用户名 */
define('DB_USER', 'wpuser');
/** MySQL数据库密码 */
define('DB_PASSWORD', 'centos');
/** MySQL主机 */
define('DB_HOST', 'localhost');
6.登录测试
实验 – 编译安装LAMP 应用Wordpress
实验环境:
- 两台CentOS 6
- 192.168.3.3 搭建 Apache httpd PHP Wordpress
- 192.168.3.10 搭建 Mysql
- 需要的软件包
- php-5.6.31.tar.xz
- Mariadb – mariadb-10.2.8-linux-x86_64.tar
- httpd – httpd-2.4.27.tar
- apr – apr-util-1.6.0.tar , apr-1.6.2.tar
- Wordpress – wordpress-4.8.1-zh_CN.tar
- xcache-3.2.0.tar.gz
步骤
1.编译安装 httpd
[root@localhost app]#tar xvf apr-1.6.2.tar.gz
[root@localhost app]#tar xvf apr-util-1.6.0.tar.gz
[root@localhost app]#tar xvf httpd-2.4.27.tar.bz2
[root@localhost app]#ls
apr-1.6.2 apr-util-1.6.0 httpd-2.4.27 php-7.1.10.tar.xz
apr-1.6.2.tar.gz apr-util-1.6.0.tar.gz httpd-2.4.27.tar.bz2 wordpress-4.8.1-zh_CN.tar.gz
[root@localhost app]#cp -r apr-1.6.2 httpd-2.4.27/srclib/apr
[root@localhost app]#cp -r apr-util-1.6.0 httpd-2.4.27/srclib/apr-util
[root@localhost app]#ls httpd-2.4.27/srclib/
apr apr-util Makefile.in
cd httpd-2.4.27/
./configure --prefix=/app/httpd24 \
--enable-so \
--enable-ssl \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-included-apr \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork
make && make install
Press ENTER or type command to continue
PATH=/app/httpd24/bin/:$PATH
[root@localhost httpd-2.4.27]#. /etc/profile.d/lamp.sh
[root@localhost httpd-2.4.27]#vim /etc/init.d/httpd
1 #!/bin/bash
2 #
3 # httpd Startup script for the Apache HTTP Server
4 #
5 # chkconfig: - 85 15
6 # description: The Apache HTTP Server is an efficient and extensible \
7 # server implementing the current HTTP standards.
8 # processname: httpd
9 # config: /etc/httpd/conf/httpd.conf
10 # config: /etc/sysconfig/httpd
11 # pidfile: /var/run/httpd/httpd.pid
12 #
13 ### BEGIN INIT INFO
14 # Provides: httpd
15 # Required-Start: $local_fs $remote_fs $network $named
16 # Required-Stop: $local_fs $remote_fs $network
17 # Should-Start: distcache
18 # Short-Description: start and stop Apache HTTP Server
19 # Description: The Apache HTTP Server is an extensible server
20 # implementing the current HTTP standards.
21 ### END INIT INFO
22
23 # Source function library.
24 . /etc/rc.d/init.d/functions
25
26 if [ -f /etc/sysconfig/httpd ]; then
27 . /etc/sysconfig/httpd
28 fi
29
30 # Start httpd in the C locale by default.
31 HTTPD_LANG=${HTTPD_LANG-"C"}
32
33 # This will prevent initlog from swallowing up a pass-phrase prompt if
34 # mod_ssl needs a pass-phrase from the user.
35 INITLOG_ARGS=""
36
37 # Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
38 # with the thread-based "worker" MPM; BE WARNED that some modules may not
39 # work correctly with a thread-based MPM; notably PHP will refuse to start.
40
41 # Path to the apachectl script, server binary, and short-form for messages.
42 apachectl=/app/httpd24/bin/apachectl ----最主要的是这几项
43 httpd=${HTTPD-/app/httpd24/bin/httpd}
44 prog=httpd
45 pidfile=${PIDFILE-/app/httpd24/logs/httpd.pid}
46 lockfile=${LOCKFILE-/var/lock/subsys/httpd}
47 RETVAL=0
48 STOP_TIMEOUT=${STOP_TIMEOUT-10}
49
50 # The semantics of these two functions differ from the way apachectl does
51 # things -- attempting to start while running is a failure, and shutdown
52 # when not running is also a failure. So we just do it the way init scripts
53 # are expected to behave here.
54 start() {
55 echo -n $"Starting $prog: "
56 LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
57 RETVAL=$?
58 echo
59 [ $RETVAL = 0 ] && touch ${lockfile}
60 return $RETVAL
61 }
62
63 # When stopping httpd, a delay (of default 10 second) is required
64 # before SIGKILLing the httpd parent; this gives enough time for the
65 # httpd parent to SIGKILL any errant children.
66 stop() {
67 status -p ${pidfile} $httpd > /dev/null
68 if [[ $? = 0 ]]; then
69 echo -n $"Stopping $prog: "
70 killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd
71 else
72 echo -n $"Stopping $prog: "
73 success
74 fi
75 RETVAL=$?
76 echo
77 [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
78 }
79
80 reload() {
81 echo -n $"Reloading $prog: "
82 if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
83 RETVAL=6
84 echo $"not reloading due to configuration syntax error"
85 failure $"not reloading $httpd due to configuration syntax error"
86 else
87 # Force LSB behaviour from killproc
88 LSB=1 killproc -p ${pidfile} $httpd -HUP
89 RETVAL=$?
90 if [ $RETVAL -eq 7 ]; then
91 failure $"httpd shutdown"
92 fi
93 fi
94 echo
95 }
96
97 # See how we were called.
98 case "$1" in
101 ;;
102 stop)
103 stop
104 ;;
105 status)
106 status -p ${pidfile} $httpd
107 RETVAL=$?
108 ;;
109 restart)
110 stop
111 start
112 ;;
113 condrestart|try-restart)
114 if status -p ${pidfile} $httpd >&/dev/null; then
115 stop
116 start
117 fi
118 ;;
119 force-reload|reload)
120 reload
121 ;;
122 graceful|help|configtest|fullstatus)
123 $apachectl $@
124 RETVAL=$?
125 ;;
126 *)
127 echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful
|help|configtest}"
128 RETVAL=2
129 esac
130
131 exit $RETVAL
[root@localhost ~]#chmod 655 /etc/init.d/httpd
[root@localhost ~]#service httpd start
Starting httpd: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
[ OK ]
[root@localhost ~]#chkconfig --add httpd24
[root@localhost ~]#service httpd24 start
2.二进制安装mariadb
tar xvf mariadb-10.2.8-linux-x86_64.tar.gz -C /usr/local/ && cd /usr/local
[root@localhost local]#ln -s mariadb-10.2.8-linux-x86_64/ mysql
[root@localhost local]#getent passwd mysql
mysql:x:498:498::/app/mysqldb:/sbin/nologin
[root@localhost local]#cd mysql/
[root@localhost local]#scripts/mysql_install_db --datadir=/app/mysqldb --user=mysql
[root@localhost local]#mkdir /etc/mysql
[root@localhost local]#cp support-files/my-large.cnf /etc/mysql/my.cnf
[root@localhost local]#vim /etc/mysql/my.cnf
[mysqld]
datadir = /app/mysqldb
innodb_file_per_table = ON
skip_name_resolve = ON
[root@localhost mysql]#cp support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql]#chkconfig --add mysqld
[root@localhost mysql]#chkconfig --list |grep mysql
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@localhost mysql]#touch /var/log/mysqld.log
[root@localhost mysql]#chown mysql /var/log/mysqld.log
[root@localhost mysql]#service mysqld start
[root@localhost mysql]#vim /etc/profile.d/lamp.sh
PATH=/app/httpd24/bin/:/usr/local/mysql/bin/:$PATH
[root@localhost mysql]#. /etc/profile.d/lamp.sh
[root@localhost mysql]#mysql_secure_installation
[root@localhost mysql]#mysql -uroot -pcentos
MariaDB [(none)]> create database wpdb;
MariaDB [(none)]> grant all on wpdb.* to wpuser@'192.168.25.%' identified by 'centos';
MariaDB [(none)]> grant all on wpdb.* to wpuser@'127.%' identified by 'centos';
MariaDB [(none)]> grant all on wpdb.* to wpuser@'localhost' identified by 'centos';
3.源码编译安装php
切换至192.168.3.4
[root@localhost yum.repos.d]#yum install libxml2-devel bzip2-devel libmcrypt-devel
[root@localhost yum.repos.d]#cd /app
[root@localhost app]#tar xvf php-5.6.31.tar.xz
[root@localhost app]#cd php-5.6.31
[root@localhost php-5.6.31]#./configure --prefix=/app/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 \
--with-apxs2=/app/httpd24/bin/apxs \
--with-mcrypt \
--with-config-file-path=/etc \
--with-config-file-scan-dir=/etc/php.d \
--enable-maintainer-zts \
--disable-fileinfo
[root@localhost php-5.6.31]#vim /etc/profile.d/lamp.sh
PATH=/app/httpd24/bin/:/app/php/bin/:$PATH
[root@localhost php-5.6.31]#. /etc/profile.d/lamp.sh
[root@localhost php-5.6.31]#cp php.ini-production /etc/php.ini
vim /app/httpd24/conf/httpd.conf
<IfModule dir_module> 修改下面行
DirectoryIndex index.php index.html
</IfModule>
AddType application/x-httpd-php .php 在文件尾部加两行
AddType application/x-httpd-php-source .phps
[root@localhost httpd-2.4.27]#service httpd restart
4.测试
[root@localhost httpd-2.4.27]#vim /app/httpd24/htdocs/index.php
1 <html><body><h1>It works!</h1></body></html>
2 <?php
3 $mysqli=new mysqli("192.168.3.5","wpuser","centos");
4 if(mysqli_connect_errno()){
5 echo "连接数据库失败!";
6 $mysqli=null;
7 exit;
8 }
9 echo "连接数据库成功!";
10 $mysqli->close();
11
12
13 $conn = mysql_connect('localhost','root','centos');
14 if ($conn)
15 echo "OK";
16 else
17 echo "Failure";
18 mysql_close();
19
20
21 ?>
5.配置wordpress
tar xvf wordpress-4.8.1-zh_CN.tar.gz -C /app/httpd24/htdocs
cd /app/httpd24/htdocs
mv wordpress/ blog/
cd /app/httpd24/htdocs/blog/
cp wp-config-sample.php wp-config.php
vim wp-config.php
define('DB_NAME', 'wpdb');
/** MySQL数据库用户名 */
define('DB_USER', 'wpuser');
/** MySQL数据库密码 */
define('DB_PASSWORD', 'centos');
/** MySQL主机 */
define('DB_HOST', '192.168.3.5');
6.网页测试及性能测试
http://192.168.3.4/wordpress
测试性能
ab -c 10 -n 100 http://192.168.3.4/wordpress
使用xcache进行PHP的加速
xcache最高只支持php 5.6
[root@localhost app]#tar xvf xcache-3.2.0.tar.gz
[root@localhost app]#cd xcache-3.2.0
[root@localhost xcache-3.2.0]#phpize
[root@localhost xcache-3.2.0]#./configure --enable-xcache --with-php-config=/app/php/bin/php-config
[root@localhost xcache-3.2.0]#make && make install
[root@localhost xcache-3.2.0]#mkdir /etc/php.d
[root@localhost xcache-3.2.0]#cp xcache.ini /etc/php.d/
[root@localhost xcache-3.2.0]#vim /etc/php.d/xcache.ini
1 ;; this is an example, it won't work unless properly configured into php.ini
2 [xcache-common]
3 ;; non-Windows example:
4 extension = xcache.so
5 ;; Windows example:
6 extension = /app/php/lib/php/extensions/no-debug-non-zts-20131226/xcache.so
[root@localhost xcache-3.2.0]#service httpd restart
测试
ab -c 10 -n 100 http://192.168.3.4/wordpress – ab命令的使用需要安装httpd-tools包
- 上图是没加速的测试
- 下图是加速的测试