简介:
使用yum方式安装LAMP(CentOS + Apache + Mariadb/MySQL + PHP)
安装环境及版本:
<!-- wp:heading -->
<h2>一、安装环境及版本</h2>
<!-- /wp:heading -->
<!-- wp:paragraph -->
<p>系统环境:CentOS Linux release 7.5.1804 (Core)<br>Apache版本:apr-1.5.0.tar.gz、apr-util-1.5.3.tar.gz、pcre-8.38.tar.gz、httpd-2.4.23.tar.gz<br>MySQL版本:mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz</p>
<!-- /wp:paragraph -->
一、安装CentOS 操作系统
1.1 安装操作系统
在VMware上安装操作系统:CentOS Linux release 7.5.1804,具体安装方式参考《VMware创建系统(CentOS6.5)》
1.2 安装VMTools
参考《VMware安装VMTools(CentOS)》
1.3 快照
参考《VMware快照》
1.4 配置IP地址
参考《配置IP地址(CentOS)》
1.5 配置主机名
参考《配置主机名(CentOS)》
1.6 关闭防火墙和禁用SELINUX
参考《关闭防火墙及SELINUX(CentOS)》
二、安装依赖包
命令:yum -y install unzip wget
[root@localhost ~]# yum -y install unzip wget
Loaded plugins: fastestmirror
Determining fastest mirrors
................
Installed:
wget.x86_64 0:1.14-18.el7_6.1
Updated:
unzip.x86_64 0:6.0-21.el7
Complete!
命令:yum -y install gcc gcc-c++ autoconf libtool
[root@localhost ~]# yum -y install gcc gcc-c++ autoconf libtool
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
.....
Updated:
gcc.x86_64 0:4.8.5-39.el7 gcc-c++.x86_64 0:4.8.5-39.el7
Dependency Updated:
cpp.x86_64 0:4.8.5-39.el7 gcc-gfortran.x86_64 0:4.8.5-39.el7 libgcc.x86_64 0:4.8.5-39.el7 libgfortran.x86_64 0:4.8.5-39.el7 libgomp.x86_64 0:4.8.5-39.el7 libquadmath.x86_64 0:4.8.5-39.el7
libquadmath-devel.x86_64 0:4.8.5-39.el7 libstdc++.x86_64 0:4.8.5-39.el7 libstdc++-devel.x86_64 0:4.8.5-39.el7
Complete!
三、安装Apache
具体参考《Apache安装》中的make安装方式
3.1 安装apr
cd /usr/local/src/
wget http://oss.aliyuncs.com/aliyunecs/onekey/apache/apr-1.5.0.tar.gz
tar zxvf apr-1.5.0.tar.gz
cd apr-1.5.0
./configure –prefix=/usr/local/apr #安装目录
make && make install
3.2 安装apr-util
cd /usr/local/src/
wget http://oss.aliyuncs.com/aliyunecs/onekey/apache/apr-util-1.5.3.tar.gz
tar zxvf apr-util-1.5.3.tar.gz
cd apr-util-1.5.3
./configure –prefix=/usr/local/apr-util –with-apr=/usr/local/apr #安装目录 及apr安装路径
make && make install
3.3 安装pcre
cd /usr/local/src/
wget http://zy-res.oss-cn-hangzhou.aliyuncs.com/pcre/pcre-8.38.tar.gz
tar zxvf pcre-8.38.tar.gz
cd pcre-8.38
./configure –prefix=/usr/local/pcre #安装目录
make && make install
3.4 编译安装Apache
cd /usr/local/src/
wget http://zy-res.oss-cn-hangzhou.aliyuncs.com/apache/httpd-2.4.23.tar.gz
tar zxvf httpd-2.4.23.tar.gz
cd httpd-2.4.23
./configure –prefix=/usr/local/apache –sysconfdir=/etc/httpd –enable-so –enable-cgi –enable-rewrite –with-zlib –with-pcre=/usr/local/pcre –with-apr=/usr/local/apr –with-apr-util=/usr/local/apr-util –enable-mods-shared=most –enable-mpms-shared=all –with-mpm=event
make && make install
3.4.1 配置 vi /etc/httpd/httpd.conf
找到Directory参数,注释掉Require all denied添加Require all granted;
找到ServerName参数,添加ServerName localhost:80;
设置PidFile路径,在文件末尾添加PidFile “/var/run/httpd.pid”
3.4.2 开机自启动
在vi /etc/rc.d/rc.local添加/usr/local/apache/bin/apachectl start
3.4.3 配置环境变量
修改vi /root/.bash_profile文件中的PATH为:PATH=$PATH:$HOME/bin:/usr/local/apache/bin
并生效:source /root/.bash_profile
3.5 验证
浏览器输入:http://ip
四、安装Mariadb
详细参考《MySQL安装》,此处只描述过程
4.1 检查是否已安装,如果有需要先删除后再编译安装
rpm -qa | grep mysql #由下至上依次卸载
rpm -qa | grep mariadb
rpm -e xxx #一般使用此命令即可卸载成功
rpm -e –nodeps xxx #卸载不成功时使用此命令强制卸载
4.2 安装
命令:yum install -y libaio-* #安装依赖
命令:mkdir -p /usr/local/mysql
命令:cd /usr/local/src
命令:wget http://zy-res.oss-cn-hangzhou.aliyuncs.com/mysql/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
命令:tar -xzvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
命令:mv mysql-5.7.17-linux-glibc2.5-x86_64/* /usr/local/mysql/
4.2.1 建立mysql组和用户
命令:groupadd mysql
命令:useradd -g mysql -s /sbin/nologin mysql
4.2.2 初始化mysql数据库
命令:/usr/local/mysql/bin/mysqld –initialize-insecure –datadir=/usr/local/mysql/data/ –user=mysql
4.2.3 更改mysql安装目录的属主属组
命令:chown -R mysql:mysql /usr/local/mysql
命令:chown -R mysql:mysql /usr/local/mysql/data/
4.2.4 设置开机自启
命令:cd /usr/local/mysql/support-files/
命令:cp mysql.server /etc/init.d/mysqld
命令:chmod +x /etc/init.d/mysqld # 添加执行权限
命令:vi /etc/rc.d/rc.local
添加/etc/init.d/mysqld start到rc.local文件中,然后输入:wq保存退出。
4.2.5 设置环境变量
命令:vi /root/.bash_profile
在PATH=PATH:PATH:HOME/bin添加参数为:
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin:/usr/local/mysql/lib
命令:source /root/.bash_profile
[root@localhost ~]# cat /root/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin:/usr/local/apache/bin:/usr/local/mysql/bin:/usr/local/mysql/lib
export PATH
4.2.6 启动数据库
命令:/etc/init.d/mysqld start
4.2.7 修改Mysql的root用户密码
mysqladmin -u root password ‘xxxx’
4.2.8 设置远程连接
命令:grant all privileges on . to ‘root’@’%’ identified by ‘pyrk-0811’ with grant option;
4.3 验证登录
命令登录:mysql -uroot -p
[root@localhost support-files]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.17 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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.
还可以通过客户端登录
五、安装PHP
详细参考《PHP安装》中make安装部分
5.1 安装依赖包
命令:yum install -y epel-release
命令:yum install -y libmcrypt-devel
命令:yum -y install php-mcrypt libmcrypt libmcrypt-devel libxml2-devel openssl-devel libcurl-devel libjpeg.x86_64 libpng.x86_64 freetype.x86_64 libjpeg-devel.x86_64 libpng-devel.x86_64 freetype-devel.x86_64 libjpeg-turbo-devel libmcrypt-devel mysql-devel
5.2 安装PHP
进入目录:/usr/local/src
下载安装文件:wget http://zy-res.oss-cn-hangzhou.aliyuncs.com/php/php-7.0.12.tar.gz
解压缩安装文件:tar zxvf php-7.0.12.tar.gz
进入解压缩目录:cd php-7.0.12
检查环境:./configure –prefix=/usr/local/php –with-mysql=mysqlnd –with-openssl –with-mysqli=mysqlnd –enable-mbstring –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-libxml-dir=/usr –enable-xml –enable-sockets –with-apxs2=/usr/local/apache/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
5.2.1 配置
命令:cp php.ini-production /etc/php.ini
编辑:/etc/httpd/httpd.conf支持PHP
163 <IfModule dir_module>
164 DirectoryIndex index.html index.php
165 </IfModule>
283 AddType application/x-compress .Z
284 AddType application/x-gzip .gz .tgz
后面添加如下内容
AddType application/x-httpd-php-source .phps
AddType application/x-httpd-php .php
重启apache服务即可:/usr/local/apache/bin/apachectl restart
5.3 Pthread安装
为了让PHP支持多线程,需要安装Pthread
进入/usr/local/src目录:cd /usr/local/src/
下载文件:wget https://github.com/krakjoe/pthreads/archive/v3.1.6.tar.gz
解压缩下载文件:tar zxvf v3.1.6.tar.gz
并进入文件目录:cd pthreads-3.1.6
生成configure:/usr/local/php/bin/phpize
检查安装环境:./configure –with-php-config=/usr/local/php/bin/php-config
编译及安装:make && make install
5.3.1 PHP支持
注意这里面有个坑,不要在php.ini下配置pthreads扩展,不然php-fpm无法启动,因为pthreads v3版本只能运行在cli下。
php.ini目录下复制一份,命名为php-cli.ini
命令:cp /etc/php.ini /etc/php-cli.ini
编辑php-cli.ini文件:vi /etc/php-cli.ini,添加
851 extension_dir = “/usr/local/php/lib/php/extensions/no-debug-zts-20151012/”
852 extension=pthreads.so
重启apache服务:/usr/local/apache/bin/apachectl restart
查看pthread扩展是否装上:/usr/local/php/bin/php -m | grep thread