Linux安装Apache 2.4 + PHP 7 + Mysql笔记

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

 

 

准备

因为服务器不能连接外网,所以在安装之前,我已经将所有相关的安装包上传至服务器,文件列表如下:
 
apr-1.5.2.tar.gz
apr-util-1.5.4.tar.gz
httpd-2.4.9.tar.gz
libxml2-2.7.8.tar.gz
pcre-8.10.tar.gz
php-7.1.0.tar.gz
 
如果能连接外网,也可直接下载。
 
1. 安装PCRE
安装Apache之前需要安装Perl-Compatible Regular Expressions Library (PCRE), 否则会发生类似下面的错误:
 
apache/bin/httpd: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory  
 
1) 解压pcre
 
$ tar -zxvf pcre-8.10.tar.gz
 
2) 安装pcre
 
$ cd pcre-8.10
$ ./configure --prefix=/usr/local/pcre
$ make
$ sudo make install
2. 安装libxml2
安装PHP之前,需要先安装libxml2,否则可能会发生类似下面的错误:
 
configure error xml2-config not found. please check your libxml2 installation
 
1) 解压
 
$ tar -zxvf libxml2-2.7.8.tar.gz
 
2) 安装libxml2
 
$ cd libxml2-2.7.8
$ ./configure --prefix=/usr/local/libxml2
$ make
$ sudo make install
 
3. 安装Apache
安装Apache,需要安装APR and APR-Util。否则安装Apache时会报下面的错误:
 
configure: error: APR not found.  Please read the documentation.  
 
1) 解压
 
$ tar -zxvf apr-1.5.2.tar.gz
$ tar -zxvf apr-util-1.5.4.tar.gz
$ tar -zxvf httpd-2.4.9.tar.gz
 
2) 将解压后的apr and apr-util包复制到Apache的安装目录的srclib目录下
 
$ cp ./apr-1.5.2 ./httpd-2.4.9/srclib/apr -r
$ cp ./apr-util-1.5.4 ./httpd-2.4.9/srclib/apr-util -r
 
3) 安装Apache
 
$ cd httpd-2.4.9
$ ./configure --prefix=/usr/local/apache --with-included-apr --with-pcre=/usr/local/pcre/
$ make  
$ sudo make install
 
4. 安装GD
GD安装可以直接使用YUM进行安装
 
1) 检查系统是否有GD
 
$ rpm -qa | grep -i gd
 
如果没有则需要安装
 
2) 安装
 
$ sudo yum install gd-devel
 
如果需要还需要FreeType支持,则还需要安装FreeType
 
$ sudo yum install freetype*
1
3) 检查
 
安装完成之后,可以使用以下命令检查
 
$ rpm -qa | grep -i gd
 
5. 安装MySql
同样的,可以使用YUM安装Mysql
 
1) 检查
 
$ rpm -qa | grep -i mysql
 
mysql-server和mysql都需要,还有mysql-devel开发包也需要,不然PHP无法使用MYSQL。
 
2) 安装
 
安装系统中不存在的包
 
$ sudo yum install mysql
$ sudo yum install mysql-devel
$ yum install mysql-server
 
3) 启动
 
$ sudo /etc/init.d/mysqld start
 
# 设置密码
$ mysqladmin -u root -p password 123456;
 
6. 安装PHP
1) 解压
 
$ tar -zxvf php-7.1.0.tar.gz
 
2) 安装
 
通过Apache的模块来安装,
apxs: APache eXtenSion tool, 为编译和安装Apache模块的工具,以下为比较基础的安装
 
$ cd php-7.1.0
$ ./configure --prefix=/usr/local/php7 --with-libxml-dir=/usr/local/libxml2 --with-apxs2=/usr/local/apache/bin/apxs --with-mysql
 
$ make
$ sudo make install
 
$ cp ~/php-7.1.0/php.ini-development /usr/local/php7/lib/php.ini
 
如果是需要使用GD + FreeType,则按照下面的编译安装(推荐)
 
$ ./configure --prefix=/usr/local/php7 --with-libxml-dir=/usr/local/libxml2 --enable-mbstring --with-apxs2=/usr/local/apache/bin/apxs  --with-gd --with-mysql --with-mysqli --with-pdo-mysql --with-zlib --with-freetype-dir=/usr/include/freetype2/freetype/
 
$ make
$ sudo make install
 
$ cp ~/php-7.1.0/php.ini-development /usr/local/php7/lib/php.ini
 
7. 在Apache中配置PHP
在LoadModule处添加
 
LoadModule php7_module modules/libphp7.so
 
末尾添加
 
<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>
 
8. 启动Apache
sudo /usr/local/apache/bin/apachectl start
 
或者
 
sudo /usr/local/apache/bin/apachectl -f /usr/local/apache/conf/httpd.conf
 
9. 常见问题
1) no acceptable C compiler found in $PATH
 
...
configure: error: in `/home/buyabs.corp/kt94/pcre-8.10':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details.
 
解决方法:安装开发软件包组以及gcc
 
$ yum groupinstall "Development Tools"
$ sudo yum install gcc
 
2) g++: command not found
 
./libtool: line 990: g++: command not found
make[1]: *** [pcrecpp.lo] Error 1
 
解决方法:安装g++
 
$ sudo yum install gcc-c++
 
注意:安装前先卸载老版本(mysql一定要卸载)
下载并解压
 
cd ~
yum -y install gcc-c++ wget bzip2 expat-devel
yum -y install libaio libaio-devel
yum -y install zlib-devel pcre-devel libxml2-devel curl-devel
yum -y install ncurses ncurses-devel openssl ope pcre* openssl* gd-devel*
yum -y install libcurl-devel
yum -y install openssl-devel
yum -y install libxslt-devel
yum -y install libxml2-devel
mkdir /usr/local/download
cd /usr/local/download
tar -jxvf httpd-2.4.37.tar.bz2
tar -jxvf apr-1.6.5.tar.bz2
tar -jxvf apr-util-1.6.1.tar.bz2
tar -jxvf pcre-8.42.tar.bz2
tar -xvf mysql-8.0.13-linux-glibc2.12-x86_64.tar.xz
tar xf freetype-2.9.tar.gz
tar xf zlib-1.2.11.tar.gz
tar xf libpng-1.6.36.tar.gz
tar xf jpegsrc.v9c.tar.gz
tar -zxvf libzip-1.2.0.tar.gz
tar -xf mirror
 
cd ~ 
 
安装apache 
 
cd /usr/local/download/apr-1.6.5
./configure --prefix=/usr/local/apr
make && make install
cd /usr/local/download/apr-util-1.6.1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/bin/apr-1-config
make && make install
cd /usr/local/download/pcre-8.42
./configure --prefix=/usr/local/pcre
make && make install
cd /usr/local/download/httpd-2.4.37
./configure --prefix=/usr/local/apache --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --with-pcre=/usr/local/pcre/
make && make install
cd ~
 
//修改apache配置文件 httpd.conf
/usr/local/apache/bin/httpd -V    查看httpd.conf文件所在位置
vi /usr/local/apache/conf/httpd.conf  ( #ServerName www.example.com:80(194行) 后面加 ServerName localhost:80 ) 
 
//开启apache
/usr/local/apache/bin/apachectl start
 
//防火墙开启80端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload
 
安装mysql 
 
cd ~
mkdir /data
mv /usr/local/download/mysql-8.0.13-linux-glibc2.12-x86_64 /data/mysql8
useradd -s /sbin/nologin -M mysql
mv /etc/my.cnf /etc/my.cnf.bak
cd /data/mysql8
 
配置my.cnf 
 
vi /data/mysql8/my.cnf
 
复制下面内容
[mysqld]
port = 3306
basedir = /data/mysql8
datadir = /data/mysql8/data
max_connections = 1024
character-set-server = UTF8MB4
default-storage-engine = INNODB
server_id = 1
socket = /data/mysql8/mysql.sock
pid-file = /data/mysql8/mysql.pid
slow_query_log = 1
slow_query_log_file = /data/mysql8/log/mysql-slow.log
long_query_time = 10
log-error = /data/mysql8/log/mysql-err.log
log-bin = /data/mysql8/log/mysql-bin
user = mysql
default_authentication_plugin = mysql_native_password
sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
  
[mysql]
default-character-set = UTF8MB4
  
[client]
port = 3306
socket = /data/mysql8/mysql.sock
default-character-set = UTF8MB4 
 
ln -s /data/mysql8/my.cnf /etc/my.cnf
mkdir /data/mysql8/log
mkdir /data/mysql8/data
chown -R mysql.mysql /data/mysql8
chmod 755 /data/mysql8 -R
 
 
/data/mysql8/bin/mysqld --initialize --user=mysql --basedir=/data/mysql8 --datadir=/data/mysql8/data      有临时密码(推荐)
/data/mysql8/bin/mysqld --initialize-insecure --user=mysql --basedir=/data/mysql8 --datadir=/data/mysql8/data    无密码
 
配置mysql文件 
 
cp support-files/mysql.server /etc/init.d/mysql
 
vi /etc/init.d/mysql
//修改46,47行的basedir和datadir
basedir= /data/mysql8
datadir= /data/mysql8/data
//修改63-82行,把路径改成自已的mysql安装路径
mysqld_pid_file_path=
if test -z "$basedir"
then
  basedir=/data/mysql8
  bindir=/data/mysql8/bin
  if test -z "$datadir"
  then
    datadir=/data/mysql8/data
  fi
  sbindir=/data/mysql8/bin
  libexecdir=/data/mysql8/bin
else
  bindir="$basedir/bin"
  if test -z "$datadir"
  then
    datadir="$basedir/data"
  fi
  sbindir="$basedir/sbin"
  libexecdir="$basedir/libexec"
fi
 
//启动mysql
service mysql start
 
添加环境变量
vi /etc/profile
添加如下内容:
export PATH=$PATH:/data/mysql8/bin
使配置生效
source /etc/profile
 
//登录检查mysql
 
mysql -u root -p
若前面选择无密码:mysql -u root --skip-password
 
查看临时密码 less /data/mysql8/log/mysql-err.log
若前面选择有密码:mysql -u root -p
 
修改密码
方法1mysql>ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
方法2mysql>ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码';
flush privileges;
 
创建可远程连接的用户
create user '用户名'@'主机' identified with mysql_native_password by '密码';
grant all privileges on *.* to '用户名'@'主机';
flush privileges;
 
 
 
安装PHP 
 
 
cd /usr/local/download/zlib-1.2.11
./configure --prefix=/usr/local/zlib
make test
make install
cd /usr/local/download/libpng-1.6.36
./configure --prefix=/usr/local/libpng
make
make check
make install
cd /usr/local/download/jpeg-9c
./configure --prefix=/usr/local/libjpeg
make && make install
cd /usr/local/download/freetype-2.9
./configure --prefix=/usr/local/freetype
make && make install
cd /usr/local/download/libzip-1.2.0
./configure
make && make install
cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h
cd /usr/local/download/php-7.3.0
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php --with-apxs2=/usr/local/apache/bin/apxs --with-pdo-mysql --with-mysqli --with-zlib=/usr/local/zlib --with-freetype-dir=/usr/local/freetype --with-png-dir=/usr/local/libpng --with-jpeg-dir=/usr/local/libjpeg --with-curl --with-gd --with-gettext --with-iconv-dir --with-openssl --with-pcre-regex --with-pdo-sqlite --with-pear --with-xsl --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml --enable-zip
make && make install
cp /usr/local/download/php-7.3.0/php.ini-development /usr/local/php/php.ini
cd ~
 
如果想从新配置PHP只需重复./configure...  make && make install步骤
 
检查PHP
vi /usr/local/apache/conf/httpd.conf
查看有无(153行):LoadModule php7_module        modules/libphp7.so
 
在Apache的配置文件httpd.conf的<IfModule mime_module></IfModule>块里增加一行(367行)
 
AddType application/x-httpd-php .php
 
php连接mysql
编辑php.ini(1166行)添加:mysqli.default_socket = /data/mysql8/mysql.sock
vi /usr/local/php/php.ini
 
运行apache
/usr/local/apache/bin/apachectl restart
 
添加php文件并输入 <?php phpinfo();
vi /usr/local/apache/htdocs/info.php
 
测试数据库连接
vi /usr/local/apache/htdocs/testConnectDatanase.php
<?php
$link = mysqli_connect("localhost","root","111111","mysql");
if($link){echo "successful connection";}else{echo "connection failed";}
posted @ 2020-05-09 16:37  华诺  阅读(665)  评论(0编辑  收藏  举报