这几天在学习一些web环境的东西,本来是打算在Windows server上搭建PHP+IIS+MySQL,但是因为服务器配置不是很高,图形化界面不是很流畅,再加上Windows系统本身的一些不方便,果断转入Linux,在阿里云上其实是有LAMP的公共镜像的,但是一键安装根本没有技术可言,所以就手工搭了个LAMP。
- 操作系统:CentOS 7.3 64 位
- Apache:2.4
- MySQL:5.6
- PHP:7.0.12
需要用到XShell工具
准备工作
因为我的服务器是用的云服务器,所以在开始之前需要先修改一下安全组的策略,也就是放开21(FTP)22(SSH)/80(HTTP)/3306(MySQL)以及其他需要的端口
通过XShell连接服务器并通过XShell输入以下指令
关闭firewalld
systemctl stop firewalld.service禁止firewalld开机自启
systemctl disable firewalld.service安装vim和unzip
yum install -y vim unzip
搭建LAMP
编译安装apache 2.4
安装依赖包
yum install -y gcc gcc-c++ autoconf libtool安装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
安装 pcre
安装 apr-util
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
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 make && make install安装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修改httpd.conf配置文件参数
切换到 /etc/httpd/ 目录
cd /etc/httpd/vim打开httpd.conf之后按 i 键进入编辑模式
vim httpd.conf找到 Directory 参数,注释掉 Require all denied,添加 Require all granted
也就是由之前的
<Directory /> AllowOverride none Require all denied </Directory>改成
<Directory /> AllowOverride none Require all granted #Require all denied </Directory>然后找到ServerName 参数,加上 ServerName localhost:80(也就是自己加上红色部分)
ServerAdmin [email protected] # # ServerName gives the name and port that the server uses to identify itself. # This can often be determined automatically, but we recommend you specify # it explicitly to prevent problems during startup. # # If your host doesn't have a registered DNS name, enter its IP address here. # #ServerName www.example.com:80 ServerName localhost:80 # # Deny access to the entirety of your server's filesystem. You must # explicitly permit access to web content directories in other然后再文件末尾添加 PidFile路径,也就是在加上
PidFile "/var/run/httpd.pid"修改完之后Esc :wq保存
启动Apache
cd /usr/local/apache/bin/ ./apachectl start此时在浏览器中输入服务器的公网IP,如果出现It works,则说明Apache安装成功
之后设置开机自启动
vim /etc/rc.d/rc.local按 i 进入编辑模式之后添加
/usr/local/apache/bin/apachectl start之后wq保存文件
设置环境变量
vim /root/.bash_profile按 i 进入编辑模式
在PATH=$PATH:$HOME/bin之后添加(不要换行 ! ! ! )
:/usr/local/apache/binEsc退出编辑模式:wq保存
输入
source /root/.bash_profile
重新执行文件
安装MySQL 5.6
rpm下载
rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm安装MySQL
yum install mysql-community-server在安装过程中会出现几次询问,只需要输入几次Y之后回车就可以
安装完成之后启动MySQL的守护进程
systemctl start mysqld启动之后,输入设置命令进行一些基本信息的设置
mysql_secure_installation这时候需要一个输密码的环节,输入当前用户root的密码后回车对MySQL进行基本设置(第一次设置时密码为空,只需要回车即可)
将会有一下几个设置,第一个需要输入Y之后输入两遍新密码
-
Set root password? [Y/n]
是否设置root用户的密码 -
Remove anonymous users? [Y/n]
是否删除匿名用户 -
Disallow root login remotely? [Y/n]
是否禁止root远程登录 -
Remove test database and access to it? [Y/n]
是否删除database数据库 -
Reload privilege tables now? [Y/n]
是否重新加载授权信息
编译安装PHP 7.0.12
依次运行以下命令安装依赖
yum 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 -y 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 \ --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=/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依次运行以下命令复制配置文件
cd php-7.0.12 cp php.ini-production /etc/php.ini编辑 Apache 配置文件 httpd.conf,以 Apache 支持 PHP
运行
vim /etc/httpd/httpd.conf
按 i 进入编辑模式
在最后添加
AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps找到DirectoryIndex index.html将其修改为
DirectoryIndex index.php index.htmlEsc退出编辑模式, :wq 保存并退出
重启Apache
/usr/local/apache/bin/apachectl restart测试php是否能够正常解析
cd /usr/local/apache/htdocs/ vim index.php按 i 进入编辑模式并添加
<?php phpinfo(); ?>
Esc退出编辑模式 :wq保存并退出
重启Apache
/usr/local/apache/bin/apachectl restart
之后再本机浏览器输入http://服务器公网ip/index.php
若正常显示phpinfo界面,则说明解析成功