CentOS7搭建LAMPR环境完整教程

最近有几台新的服务器,需要自己进行环境搭建,刚好久不操作又可以熟悉一下,顺带写个详细教程分享一下。
本教程通过centos 7.2(1511)、7.3(1611)版本测试可用。

1 使用的软件版本

centos : centos-7-x86_64-1611(7.3.1611)
apache : 2.4.6
mysql : mysql-5.7
php : php-7.1.8
redis : 4.0.1

2 前置工作

2.1 设置yum源
安装wget
yum install wget

将原/etc/yum.repo.d/目录下的文件清除(备份亦可)
rm –f /etc/yum.repo.d/*

设置国内yum源wget -O CentOS7-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
使用163的源,速度更快,资源较多。

从mysql官网下载并安装mysql yum源https://dev.mysql.com/downloads/repo/yum/

下载得到的包mysql57-community-release-el7-11.noarch.rpm

安装rpm –ivh mysql57-community-release-el7-11.noarch.rpm

清除原yum缓存信息并使用新的yum源重建
yum clean all
yum makecache

2.2 安装软件包

2.2.1 可yum安装的包
下列可以安装的都装上。
yum install httpd-devel
yum install gd
yum install zlib
yum install libcurl-devel
yum install openssl-devel
yum install libxml2-devel
yum install openjpeg
yum install openjpeg-devel
yum install curl-devel
yum install openjpeg-libs
yum install libjpeg
yum install libpng
yum install libpng-devel
yum install freetype
yum install freetype-devel
yum install gcc
yum install bzip2
yum install bzip2-devel
yum install libjpeg-devel
yum groupinstall “Development Tools”
yum install libxslt
yum install libxslt-devel
yum install mod_ssl
yum install openssl
yum install openssl-devel

2.2.2 需编译安装的包
wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.7.tar.gz
tar -zxf
./configure –prefix=/usr/local/
make && make install

2.2.3 配置文件修改
配置文件:/etc/ld.so.conf
添加:
/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64
保存后执行:
ldconfig –v

3 安装

3.1 安装apache
通过yum info httpd可以获知apache包的版本为2.4.6,所以可以直接使用yum进行安装。

apache的配置保存目录:
/etc/httpd/

apache的具体配置文件:
/etc/httpd/conf/httpd.conf

修改httpd.conf配置文件:
ServerName localhost:80

centos默认开启防火墙firewalld,需要关闭后才可以访问到http://ip,关闭防火墙:
systemctl stop firewalld.service
systemctl disable firewalld.service //禁止开机启动

apache的启停操作:
/sbin/httpd –k [start| stop | restart]

3.2 安装mysql
通过yum repolist all | grep mysql可以看到mysql的信息。
yum install mysql-community-server

启动mysql服务:
systemctl start mysqld

找到mysql默认的初始密码:
cat /var/log/mysqld.log | grep “temporary password”

首次登录后,必须首先修改密码才可以进行其它操作。需要使用复杂密码(大小写字母、数字,符号,推荐16位)。
set password for root@localhost=password(‘**’);

3.3 安装php
php没有yum包可以使用,需要手动编译安装,依赖的软件包在2.2节已经进行安装。
下载源码包: wget http://cn2.php.net/get/php-7.1.12.tar.gz/from/this/mirror

解压后进入目录:
tar –zxf php-7.1.8.tar.gz
cd php-7.1.8/

进行配置:
./configure –with-apxs2=/usr/bin/apxs –with-bz2 –with-curl –with-freetype-dir –with-gd –with-gettext –with-iconv-dir –with-jpeg-dir –with-mysql-sock=/var/lib/mysql/mysql.sock –with-mysqli –with-mcrypt –with-mhash –with-openssl –with-png-dir –with-pdo-mysql –with-pear –with-pcre-regex –with-xsl –with-zlib –enable-bcmath –enable-exif –enable-ftp –enable-fpm –with-fpm-user=apache –with-fpm-group=apache –enable-gd-native-ttf –enable-mbstring –enable-mysqlnd –enable-pcntl –disable-rpath –enable-soap –enable-sysvsem –enable-sockets –enable-zip

编译安装:
make
make install

安装完成后,配置php.ini配置文件:
cp /root/php-7.1.8/php.ini-production /usr/local/lib/php.ini

修改/etc/httpd/conf/httpd.conf配置文件,使apache支持php:
AddType后增加一行:AddType application/x-httpd-php .php

增加index.php

   <IfModule dir_module>
    DirectoryIndex index.html index.php
   </IfModule>

3.4 安装redis
从redis官网http://www.redis.cn/download.html下载源码包:
redis-4.0.1.tar.gz

解压后,进入目录编译:
tar -zxf redis-4.0.1.tar.gz
cd redis-4.0.1
make

修改配置文件:
redis.conf
修改:
daemonize yes //让redis在后台启动运行
appendonly yes //开启redis持久化存储

将redis配置文件与启动入口分别放置:
mkdir /etc/redis
cp redis.conf /etc/redis/
cp src/redis-server /sbin/
cp src/redis-cli /sbin/

启动redis服务:
/sbin/redis-server /etc/redis/redis.conf (启动时指定配置文件,则按照指定的配置文件配置启动)

启动redis client测试:
/sbin/redis-cli

3.5 安装php-redis扩展
https://github.com/phpredis/phpredis下载php-redis压缩包:
phpredis-develop.zip

解压:
unzip phpredis-develop.zip
cd phpredis-develop

配置并编译:
phpize
./configure –with-php-config=/usr/local/bin/php-config
make
make install

修改php.ini配置文件:
/usr/local/lib/php.ini
添加
extension=redis.so

执行测试:
php tests/TestRedis.php

4 其它功能配置

4.1 将php session放到redis
修改php.ini配置文件:
session.save_handler = redis
session.save_path = “tcp://127.0.0.1:6379”

4.2 apache多虚机端口配置
在/etc/httpd/conf.d/目录下,添加vhosts.conf配置文件。
以增加虚机9900端口,默认目录为/var/myweb/为例:
vhosts.conf配置文件中增加如下信息:

<VirtualHost *:9900>
ServerName domain.com
DocumentRoot "/var/myweb"
DirectoryIndex index.html index.php
<Directory "/var/myweb">
  Options -Indexes +FollowSymlinks
  AllowOverride All
  Require all granted
</Directory>
</VirtualHost>
/etc/httpd/conf/httpd.conf配置文件增加一行配置信息:
   Listen 9900

4.3 apache支持https
https是需要证书的,以自签名证书为例。(网盘现有的证书,可以直接拷贝到指定目录)

生成自签名证书:
生成2048位加秘私钥:
openssl genrsa -out server.key 2048
生成证书签名请求(CSR),有信息需要填写:
openssl req -new -key server.key -out server.csr
生成类型为X509的自签名证书,有奖期设置10年3650天
openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt

将证书文件复制到对应目录:
cp server.crt /etc/pki/tls/certs/
cp server.key /etc/pki/tls/private/
cp server.csr /etc/pki/tls/private/
修改/etc/httpd/conf.d/ssl.conf配置文件:
SSLCertificateFile /etc/pki/tls/certs/server.crt
SSLCertificateKeyFile /etc/pki/tls/private/server.key

修改/etc/httpd/conf.d/vhosts.conf配置文件,增加一个https虚机端口,以9901为例:

<VirtualHost *:9901>
  SSLEngine on
  SSLCertificateFile /etc/pki/tls/certs/server.crt
  SSLCertificateKeyFile /etc/pki/tls/private/server.key
  DocumentRoot "/var/myssl"
  DirectoryIndex index.html index.php
  <Directory "/var/myssl">
    Options -Indexes +FollowSymlinks
    AllowOverride All
    Require all granted
  </Directory>
</VirtualHost>

修改了apache的配置,需要重启apache之后才会生效:
/sbin/httpd –k restart

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值