编译安装LAMP
版本信息:
CentOS7(1804)
mysql-community-5.7.25
php-7.3.2.tar.gz
httpd-2.4.38.tar
mysql5.7编译安装和初始化
[root@localhost ~]# cd /usr/local/src/
[root@localhost ~]# yum install -y wget
[root@localhost ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.25.tar.gz
增加mysql用户、mysql组
[root@localhost src]# groupadd mysql
[root@localhost src]# useradd -r -g mysql mysql
安装必要软件
[root@localhost src]# yum install gcc-c++ ncurses-devel cmake make -y
[root@localhost src]# ll
total 78772
-rw-r--r-- 1 root root 9187294 Aug 14 09:55 httpd-2.4.38.tar.gz
-rw-r--r-- 1 root root 52063298 Aug 14 09:55 mysql-5.7.25.tar.gz
-rw-r--r-- 1 root root 19409040 Aug 14 09:56 php-7.3.2.tar.gz
解压并进入解压目录
[root@localhost src]# tar zxvf mysql-5.7.25.tar.gz
[root@localhost src]# cd mysql-5.7.25
开始编译安装
[root@localhost mysql-5.7.25]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql5.7.25 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_DATADIR=/usr/local/mysql5.7.25/data -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/mysql5.7.25
编译安装需要boost 库,下载boost的源码包很慢,最好下载含boost的源码包
[root@localhost src]# wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.25.tar.gz
[root@localhost src]# tar zxvf mysql-5.7.25.tar.gz
[root@localhost src]# cd mysql-5.7.25
[root@localhost src]# mv boost/ /usr/local/boost
编译时指定boost路径即可
[root@localhost mysql-5.7.25]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql5.7.25 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_DATADIR=/usr/local/mysql5.7.25/data -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost
也可单独下载boost软件,与MySQL5.7相对应的版本是boost_1_59_0,我是用这种方法安装的
[root@localhost src]# wget http://downloads.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
[root@localhost src]# tar zxvf boost_1_59_0.tar.gz -C /usr/local/
[root@localhost local]# mv boost_1_59_0/ boost
重新开始编译安装,要把原来的目录删除,重新解压
[root@localhost src]# rm -rf mysql-5.7.25
[root@localhost src]# tar zxvf mysql-5.7.25.tar.gz
[root@localhost src]# cd mysql-5.7.25
[root@localhost mysql-5.7.25]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql5.7.25 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_DATADIR=/usr/local/mysql5.7.25/data -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost
官网编译参数解释网址:https://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html
DCMAKE_INSTALL_PREFIX:指定MySQL程序的安装目录,默认/usr/local/mysql
DEFAULT_CHARSET:指定服务器默认字符集,默认latin1
DEFAULT_COLLATION:指定服务器默认的校对规则
MYSQL_DATADIR:数据文件目录
MYSQL_UNIX_ADDR:socket文件路径,默认/tmp/mysql.sock
DDOWNLOAD_BOOST=1:下载boost ,下载好了可以不用加这个参数
-DWITH_BOOST:指定路径
代表成功
[root@localhost mysql-5.7.25]# echo $?
0
[root@localhost mysql-5.7.25]# make ,要等很久
[root@localhost mysql-5.7.25]# echo $?
0
[root@localhost mysql-5.7.25]# make install
[root@localhost mysql-5.7.25]# echo $?
0
开始初始化mysql5.7
创建数据存储目录
[root@localhost mysql5.7.25]# mkdir /usr/local/mysql5.7.25/data
修改文件属组属性
chown -R mysql:mysql /usr/local/mysql5.7.25
初始化数据库
--datadir 指定MySQL的数据库文件路径
--basedir 指定MySQL的安装路径
[root@localhost bin]# ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql5.7.25 --datadir=/usr/local/mysql5.7.25/data
2020-02-09T06:42:33.561456Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-02-09T06:42:33.955047Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-02-09T06:42:34.017196Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-02-09T06:42:34.079436Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 5a580779-4b07-11ea-8d4b-000c298c979c.
2020-02-09T06:42:34.080508Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-02-09T06:42:34.081864Z 1 [Note] A temporary password is generated for root@localhost: xV=a!ISCb1iz 记住密码
(重点:初始化完成后要检查data目录下文件的属性)
修改数据库配置文件为下面所示
vim /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql5.7.25/data
socket=/tmp/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
启动服务
/usr/local/mysql5.7.26/bin/mysqld_safe &
报错,文件不存在,去创建,记得更改文件属组
2020-02-09T06:46:47.728730Z mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
[root@localhost mysql5.7.25]# cd /var/log/
[root@localhost log]# mkdir mariadb
[root@localhost log]# touch mariadb/mariadb.log
[root@localhost log]# chown -R mysql:mysql mariadb/
创建/var/mariadb/mariadb.pid文件
[root@localhost mysql5.7.25]# cd /var/
[root@localhost var]# mkdir mariadb
[root@localhost var]# touch mariadb/mariadb.pid
[root@localhost var]# chown -R mysql:mysql mariadb/mariadb.pid
启动成功,关闭服务命令/usr/local/mysql5.7.25/bin/mysqladmin shutdown -uroot -p
[root@localhost local]# /usr/local/mysql5.7.25/bin/mysqld_safe &
2020-02-09T07:03:04.723671Z mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
2020-02-09T07:03:04.770957Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql5.7.25/data
[root@localhost bin]# ps -ef | grep mysqld
root 29690 6970 0 15:03 pts/1 00:00:00 /bin/sh /usr/local/mysql5.7.25/bin/mysqld_safe
mysql 29813 29690 0 15:03 pts/1 00:00:00 /usr/local/mysql5.7.25/bin/mysqld --basedir=/usr/local/mysql5.7.25 --datadir=/usr/local/mysql5.7.25/data --plugin-dir=/usr/local/mysql5.7.25/lib/plugin --user=mysql --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/mariadb/mariadb.pid --socket=/tmp/mysql.sock
root 29842 6846 0 15:03 pts/0 00:00:00 grep --color=auto mysqld
登录数据库
[root@localhost ~]# /usr/local/mysql5.7.25/bin/mysql -uroot -p
Enter password: 输入上面的随机密码
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.25
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
更改数据库密码
mysql> alter user 'root'@'localhost' identified by '1qaz@WSX';
Query OK, 0 rows affected (0.00 sec)
到此,数据库编译安装完成
apache2.4编译安装
安装依赖
[root@localhost src]# yum install -y gcc gcc++ zlib zlib-devel expat-devel pcre-devel
Apache2.4以后的版本编译依赖apr,所以,编译之前需要先安装apr及apr-util。
wget 下载下面三个包,apr和apr-util是一个通用的函数库,它让httpd可以不关心底层的操作系统平台,可以很方便的从linux移植到windos
httpd: https://www.apache.org/dist/httpd/httpd-2.4.38.tar.gz
apr: http://archive.apache.org/dist/apr/apr-1.6.5.tar.gz
apr-util:http://archive.apache.org/dist/apr/apr-util-1.6.1.tar.gz
[root@localhost src]# tar zxvf apr-1.6.5.tar.gz
[root@localhost src]# cd apr-1.6.5
#./configure --prefix=/usr/local/apr
#make
#make install
root@localhost src]# tar zxvf apr-util-1.6.1.tar.gz
[root@localhost src]# cd apr-util-1.6.1
#./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
#make
#make install
[root@localhost src]# tar zxvf httpd-2.4.38.tar.gz
[root@localhost src]# cd httpd-2.4.38
#./configure --prefix=/usr/local/apache \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util \
--enable-so \
--enable-mods-shared=most
启用so 模块,用来启用DSO支持的,是apache 核心模块(模块的动态加载通过DSO(Dynamic Shared Object) 实现) shared指动态编译,most指动态加载大多数模块,也就是启动的时候根本不会加载这个模块, 而是给你一个module.so 文件,你一定要使用 loadmodule 这个语法来加载,这个模块才有效。 静态编译的时候所有的模块自己编译进httpd 这个文件中,启动的时候这些模块就已经加载进来了,也就是可以使用了,通常:<ifmodule> </ifmodule> 来配置。 所以大家看到的配置都是<ifmodule module.c> ,很显然,module.c这个东西已经存在httpd这个文件中了。菜单。。c
#make && make install
开启httpd服务,先修改配置文件
# vim /usr/local/apacheconf/httpd.conf
把#ServerName www.example.com:80注释去掉
# /usr/local/apache/bin/apachectl -k start
去浏览器可以正常访问
关闭httpd服务开始安装php
# /usr/local/apache/bin/apachectl -k stop
php7.3编译安装
安装php依赖组件,一行一行复制
#yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel \
libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel \
readline readline-devel libxslt libxslt-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel \
ncurses curl gdbm-devel db4-devel libXpm-devel libX11-devel gd-devel gmp-devel expat-devel \
xmlrpc-c xmlrpc-c-devel libicu-devel libmcrypt-devel libmemcached-devel libzip
# cd /usr/local/src/
[root@localhost src]# tar zxvf php-7.3.2.tar.gz
[root@localhost src]# cd php-7.3.2
开始编译
#./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-apxs2=/usr/local/apache/bin/apxs \
--with-mysql-sock=/tmp/mysql.sock \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
-with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-xpm-dir \
--with-zlib \
--with-iconv \
--enable-libxml \
--enable-xml \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--enable-opcache \
--enable-mbregex \
--enable-fpm \
--enable-mbstring \
--enable-ftp \
--with-openssl \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--without-pear \
--with-gettext \
--with-gmp \
--with-snmp \
--enable-session \
--with-ldap=shared
遇到的错误及解决方法
checking for libzip... not found
configure: error: Please reinstall the libzip distribution
移除旧的libzip:
# yum remove libzip
安装新版本libzip:
# wget https://libzip.org/download/libzip-1.6.1.tar.gz
#tar -zxvf libzip-1.6.1.tar.gz
#cd libzip-1.6.1
#mkdir build
#cd build
#cmake .. 注意:如果提示cmake版本过低,需新版本,则需要重新安装cmake,安装见下面
#make && make install
安装新版本的cmake
#wget https://cmake.org/files/v3.12/cmake-3.12.2-Linux-x86_64.tar.gz
#tar zxvf cmake-3.12.2-Linux-x86_64.tar.gz
创建软链接
注: 文件路径是可以指定的, 一般选择在/opt 或 /usr 路径下, 这里选择/opt
#mv cmake-3.12.2-Linux-x86_64 /opt/cmake-3.12.2
#ln -sf /opt/cmake-3.12.2/bin/* /usr/bin/
#cmake --version 安装完成
cmake version 3.12.2
configure: error: off_t undefined; check your library configuration
#vim /etc/ld.so.conf
#添加如下几行
/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64
# ldconfig -v # 使之生效
#make
#make install
验证安装的模块
/usr/local/php/bin/php -m
拷贝配置文件
#cp /root/lamp/php-7.3.2/php.ini-production /usr/local/php/etc/php.ini
修改 php.ini 相关参数:
#vi /usr/local/php/etc/php.ini
date.timezone = Asia/Shanghai ;时区设置
#vim /usr/local/apache/conf/httpd.conf
修改httpd.conf,载入PHP模块,如httpd.conf中有下列代码则直接去掉前面#即可,没有则加入
LoadModule php7_module modules/libphp7.so
#在底部加入以下代码使得Apache可以解析php文件
<IfModule mod_php7.c>
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
</IfModule>
#找到如下代码,在index.html前面加入index.php
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
DirectoryIndex index.php index.html
#此处注意,php文件必须放前面,优先读取
启动数据库
# /usr/local/mysql5.7.25/bin/mysqld_safe &
启动httpd
# /usr/local/apache/binapachectl -k start
测试lamp环境方法:
1. vi /usr/local/apache/htdocs/index.php,浏览器访问该页面可以看到mysqli模块
<?php
phpinfo();
?>
浏览器访问正常
测试数据库连接 ,浏览器访问该页面可以看到数据库连接成功的提示。
vi /usr/local/apache/htdocs/mysql.php
<?php
$mysqli=mysqli_connect("localhost", "root", "1qaz@WSX", "mysql");
if($mysqli) {
echo "SUCCESSFUL!";
}else{
echo "ERROR!";
}
?>
到此,lamp编译安装成功结束