摘自:https://help.aliyun.com/document_detail/50774.html?spm=5176.doc44620.6.623.HqVrsz
更新時間:2017-03-14 11:22:11
簡介
LAMP指Linux+Apache+Mysql/MariaDB+Perl/PHP/Python是一組常用來搭建動態網站或者服務器的開源軟件,本身都是各自獨立的程序,但是因為常被放在一起使用,擁有了越來越高的兼容度,共同組成了一個強大的Web應用程序平台。
部署及環境
系統平台:CentOS 7.2
Apache版本:2.4.23
Mysql 版本:5.7.17
Php版本:7.0.12
安裝前准備
CentOS 7.2系統默認開啟了防火牆,需關閉后外部才可訪問本機的80、21等端口。
關閉防火牆:
systemctl stop firewalld.service
關閉防火牆開機自啟動:
systemctl disable firewalld.service
安裝vim及unzip:
yum install -y vim unzip
編譯安裝apache准備
編譯安裝apache前需要安裝apr、apr-util和pcre軟件包和相關依賴包。
安裝編譯環境
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.gztar zxvf apr-1.5.0.tar.gzcd apr-1.5.0./configure --prefix=/usr/local/apr #安裝目錄make && make install
安裝apr-util
cd /usr/local/src/wget http://oss.aliyuncs.com/aliyunecs/onekey/apache/apr-util-1.5.3.tar.gztar zxvf apr-util-1.5.3.tar.gzcd apr-util-1.5.3./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr #安裝目錄 及apr安裝路徑make && make install
安裝pcre
cd /usr/local/src/wget http://zy-res.oss-cn-hangzhou.aliyuncs.com/pcre/pcre-8.38.tar.gztar zxvf pcre-8.38.tar.gzcd pcre-8.38./configure --prefix=/usr/local/pcre #安裝目錄make && make install
編譯安裝Apache
cd /usr/local/src/wget http://zy-res.oss-cn-hangzhou.aliyuncs.com/apache/httpd-2.4.23.tar.gztar zxvf httpd-2.4.23.tar.gzcd 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 \ #pcre所在目錄--with-apr=/usr/local/apr \ #apr所在目錄--with-apr-util=/usr/local/apr-util \ #apr-util所在目錄--enable-mods-shared=most --enable-mpms-shared=all \--with-mpm=eventmake && make install
修改httpd.conf配置文件參數
cd /etc/httpd/vim httpd.conf
1.找到Directory參數,注釋掉Require all denied添加Require all granted。

2.找到ServerName參數,添加ServerName localhost:80 然后,按Esc鍵后輸入:wq保存退出。

設置PidFile路徑
vim /etc/httpd/httpd.conf
在配置文件最后添加以下內容:
PidFile "/var/run/httpd.pid"
啟動Apache服務並驗證
cd /usr/local/apache/bin/./apachectl startnetstat -tnlp #查看服務是否開啟

在本地瀏覽器中輸入雲服務器的公網IP地址驗證,出現下圖表示安裝成功。

設置開機自啟
在rc.local文件中添加/usr/local/apache/bin/apachectl start,然后輸入:wq保存退出。
vim /etc/rc.d/rc.local

設置環境變量
vi /root/.bash_profile
在PATH=$PATH:$HOME/bin添加參數為:
PATH=$PATH:$HOME/bin:/usr/local/apache/bin
然后輸入:wq保存退出,執行:
source /root/.bash_profile
編譯安裝MySQL前預准備
首先檢查系統中是否存在使用rpm安裝的mysql或者mariadb,如果有需要先刪除后再編譯安裝。
rpm -qa | grep mysql #由下至上依次卸載rpm -qa | grep mariadbrpm -e xxx #一般使用此命令即可卸載成功rpm -e --nodeps xxx #卸載不成功時使用此命令強制卸載
卸載完以后用 rpm -qa|grep mariadb 或者 rpm -qa|grep mysql 查看結果。
安裝mysql
yum install -y libaio-* #安裝依賴mkdir -p /usr/local/mysqlcd /usr/local/srcwget http://zy-res.oss-cn-hangzhou.aliyuncs.com/mysql/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gztar -xzvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gzmv mysql-5.7.17-linux-glibc2.5-x86_64/* /usr/local/mysql/
建立mysql組和用戶,並將mysql用戶添加到mysql組
groupadd mysqluseradd -g mysql -s /sbin/nologin mysql
初始化mysql數據庫
/usr/local/mysql/bin/mysqld --initialize-insecure --datadir=/usr/local/mysql/data/ --user=mysql
更改mysql安裝目錄的屬主屬組
chown -R mysql:mysql /usr/local/mysqlchown -R mysql:mysql /usr/local/mysql/data/chown -R mysql:mysql /usr/local/mysql
設置開機自啟
cd /usr/local/mysql/support-files/cp mysql.server /etc/init.d/mysqldchmod +x /etc/init.d/mysqld # 添加執行權限vim /etc/rc.d/rc.local
添加/etc/init.d/mysqld start到rc.local文件中,然后輸入:wq保存退出。
設置環境變量
vi /root/.bash_profile
在PATH=$PATH:$HOME/bin添加參數為:
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin:/usr/local/mysql/lib
然后輸入:wq保存退出,輸入:
source /root/.bash_profile
啟動MySQL數據庫
/etc/init.d/mysqld start

修改Mysql的root用戶密碼
初始化后mysql為空密碼可直接登錄,為了保證安全性需要修改mysql的root用戶密碼。
mysqladmin -u root password 'xxxx'
測試登錄MySQL數據庫
mysql -uroot -p密碼 #-p和密碼之間無空格

編譯安裝php
依賴安裝:
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 -ywget http://zy-res.oss-cn-hangzhou.aliyuncs.com/php/php-7.0.12.tar.gztar zxvf php-7.0.12.tar.gzcd 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-fileinfomake && make install
復制配置文件
cd php-7.0.12cp php.ini-production /etc/php.ini
編輯apache配置文件httpd.conf,以apache支持php
vim /etc/httpd/httpd.conf
在配置文件最后添加如下二行:
AddType application/x-httpd-php .phpAddType application/x-httpd-php-source .phps
定位到 DirectoryIndex index.html
修改為:
DirectoryIndex index.php index.html
重啟apache服務
/usr/local/apache/bin/apachectl restart
測試是否能夠正常解析PHP
cd /usr/local/apache/htdocs/vim index.php #添加如下內容<?phpphpinfo();?>
訪問雲服務器的公網IP/index.php,出現如下頁面表示解析成功。

安裝phpmyadmin
mkdir -p /usr/local/apache/htdocs/phpmyadmincd /usr/local/src/wget http://oss.aliyuncs.com/aliyunecs/onekey/phpMyAdmin-4.1.8-all-languages.zipunzip phpMyAdmin-4.1.8-all-languages.zipmv phpMyAdmin-4.1.8-all-languages/* /usr/local/apache/htdocs/phpmyadmin
訪問ip/phpmyadmin即可訪問到phpmyadmin登錄頁面,輸入mysql的用戶名以及密碼即可登錄。

