前言
LAMP 是指一組通常一起使用來運行動態網站或者服務器的自由軟件名稱首字母縮寫
L:Linux 操作系統
A:Apache(httpd) 網頁服務
M:MySQL(mariadb) 數據庫服務
P:php/perl/python/ruby 腳本編程語言
本文主要以centos 7的環境下進行安裝,centos 6兼帶部分說明
一、http2.4的安裝
Centos 7默認安裝httpd 2.4,Centos 6默認安裝httpd2.2
Centos 7:如果未安裝http2.4,則通過yum安裝
yum -y install httpd
Centos 6:只能通過編譯安裝
事先須安裝Development Tools和Server Platform Development兩個包組,同時還需要安裝prce-devel程序包,prce包可通過yum安裝
yum -y groupinstall "Development tools" yum -y groupinstall "Server Platform Development" yum -y prce-devel
httpd2.4需要1.4以上的版本apr和apr-util,故先get兩者的源碼包
apr的編譯安裝
tar xf apr-1.4.6.tar.bz2 -C /usr/local //將源碼包tar至/usr/local目錄之下 cd /usr/local/apr-1.4.6 ./configure --prefix=/usr/local/apr //對源碼進行配置,指定安裝目錄 make && make install //對源碼進行編譯安裝
apr-util的編譯安裝
tar xf apr-util-1.4.1.tar.bz2 -C /usr/local cd /usr/local/apr-util-1.4.1/ ./configure --prefix=/usr/local/apr-util make && make install
httpd的編譯安裝
tar xf httpd-2.4.10.tar.bz2 -C /usr/local cd /usr/local/httpd-2.4.10 ./configure --prefix=/usr/local/apache //指定安裝目錄 --sysconfdir=/etc/httpd24 //指定配置文件目錄 --enable-so //啓用模塊功能 --enable-ssl //啓用ssl加密功能 --enable-cgi //啓用cgi功能 --enable-rewrite //可重載 --with-zlib //使用zlib數據 --with-pcre /使用pcre數據 --with-apr=/usr/local/apr //所關聯程序apr及目錄 --with-apr-util=/usr/local/apr-util //所關聯程序apr-util及目錄 --enable-modules=most //最大程度啓用所有模塊 --enable-mpms-shared=all //分離列出所有MPM模塊 --with-mpm=perfork //使用perfork模式 make && make install
其中./configure的相關參數可以使用./configure --help查看
以上爲分列出各參數意義,故在排版上進行了分割,在配置中相關參數用空格分隔
二、mariadb的安裝
Centos 6中默認使用的爲MySQL,在Centos 7中默認使用的mariadb
mariadb的安裝
yum -y install mariadb-server.x86_64 systemctl start mariadb //啓動mariadb服務
MySQL的安裝
yum -y install mysql-server service mysqld start
mariadb配置文件:/etc/my.cnf,/etc/my.cnf.d/*.cnf
修改/etc/my.cnf文件
加入以下值
innodb_file_per_table = ON //生成數據庫列表 skip_name_resolve = ON //禁止反解
安裝完成後,運行一次mysql_secure_installation,對mariadb進行配置。
[root@chunlanyy ~]# mysql_secure_installation NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MySQL to secure it, we'll need the current password for the root user. If you've just installed MySQL, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): //初次運行直接回車 OK, successfully used password, moving on… Setting the root password ensures that nobody can log into the MySQL root user without the proper authorisation. Set root password? [Y/n] //是否設置root用戶密碼,輸入y並回車 New password: //設置root用戶的密碼 Re-enter new password: //重複輸入密碼 Password updated successfully! Reloading privilege tables.. … Success! By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users?[Y/n] //是否刪除匿名用戶,刪除 … Success! Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] //是否禁止root遠程登錄,禁止 … Success! By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] //是否刪除test數據庫,刪除 - Dropping test database… … Success! - Removing privileges on test database… … Success! Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] //是否重新加載權限表,重新加載 … Success! Cleaning up… All done! If you've completed all of the above steps, your MySQL installation should now be secure. Thanks for using MySQL!
進入mysql進行配置
相關格式:
mysql>GRANT ALL ON db_name.tbl_name To username@'host'IDENTIFIED BY "password"
GRANT ALL ON testdb.* TO 'root'@'172.16.%.%' IDENTIFIED BY "chunlanyy";
設置用戶及密碼
FLUSH PRIVILEGES; //清除權限 CREATE DATABASE testab; //創建testab數據表單
三、php的安裝
yum -y install php
編譯安裝:
四、LAMP環境測試:
因爲httpd作爲http協議實現的服務器,只能靜態的響應和處理客戶端的請求,爲了使得服務器能動態的響應客戶端的請求,有三種方法實現:
1)使用CGI協議:httpd服務器通過CGI協議將請求轉發至程序的解釋器,解釋器將運行結果返回httpd服務器,隨後解釋器銷燬
2)使用module,把php編譯成httpd的擴展模塊,通過httpd動態調用模塊來實現
3)fastCGI:通過fpm(fastcgi process manager)讓php單獨運行一個類型apache的模型服務,監聽某個套接字,並生成多個子進程來處理響應,而主進程只負責管理請求和控制子進程的運行狀況,此時http變成fastCGI的客戶端,而fastCGI變成一個簡化版的http協議使php和http進行通信使用。
php和http常用的結合方式通過編譯httpd的php處理模塊,讓httpd自己處理php程序,或者使用專門的php應用程序服務器php-fpm,由它來負責處理php程序。
a.php與httpd結合的測試
修改之前配置好的虛擬主機的數據文件
將/data/virhost/www1/index.html更改爲/data/virhost/www1/index.php
vim /data/virhost/www1/index.php
將內容修改如下php測試代碼
<php? phpinfo(); ?>
重啓httpd服務
systemctl restart httpd
通過瀏覽器訪問172.16.45.21,即可看到phpinfo頁面
其中虛擬主機的配置文件/etc/httpd/conf.d/virhost1.conf內容如下:
<VirtualHost *:80> servername www1.chunlanyy.com documentroot "/data/virhost/www1" <Directory "/data/virhost/www1"> options none allowoverride none require all granted </Directory> </VirtualHost>
[root@chunlanyy modules]# ifconfig eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.16.45.21 netmask 255.255.0.0 broadcast 172.16.255.255
b.php連接mysql的測試
在前文已經配置好mariadb的情況下
將上述文件vim /data/virhost/www1/index.php內容修改如下:
<?php $conn = mysql_connect('172.16.45.21','root','chunlanyy'); if ($conn) echo "OK"; else echo "Failure"; ?>
當啓動php服務時,通過瀏覽器訪問172.16.45.21時,顯示ok表明連接正常
systemctl stop php顯示Failure