LAMP簡介
LAMP指的Linux(操作系統(tǒng))、ApacheHTTP 服務器甘桑,MySQL(有時也指MariaDB,數(shù)據(jù)庫軟件) 和PHP(有時也是指Perl或Python) 的第一個字母晨缴。LAMP便成了一組常用來搭建動態(tài)網(wǎng)站或者服務器的開源軟件讨越,本身都是各自獨立的程序,但是因為常被放在一起使用裤纹,擁有了越來越高的兼容度委刘,共同組成了一個強大的Web應用程序平臺。
安裝步驟:
安裝服務
配置服務
啟動服務
測試服務
安裝服務
CentOS 6:
yum install httpd php mysql-server php-mysql
CentOS 7:
yum install httpd php php-mysql mariadb-server mariadb
(php-mysql用于支持php連接mysql的組件)
配置服務
配置httpd
Apache的主配置文件:/etc/httpd/conf/httpd.conf
主配置文件一般不去修改,其中有幾個選項可以自定義修改鹰椒。
DocumentRoot /var/www/html/ //指定默認主頁路徑
Listen 80 //httpd的監(jiān)聽端口锡移,可以設置為監(jiān)聽指定網(wǎng)卡"Listen [IP:]PORT",如果需要修改端口,還要同時修改防火墻和selinux的策略
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
DirecotryIndex index.html //默認主頁,如果有多個漆际,優(yōu)先級為由左至右
默認站點主目錄:/var/www/html/
啟動Apache服務后淆珊,Apache服務會到默認主目錄查找默認主頁文件
配置mysql
mysql和mariadb數(shù)據(jù)庫服務軟件默認提供一個安全腳本,執(zhí)行這個腳本奸汇,可以對mysql進行基本的安全配置施符,比如設置密碼、刪除匿名用戶等
mysql_secure_installation //執(zhí)行MYSQL數(shù)據(jù)庫安全腳本
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, 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):
//驗證輸入數(shù)據(jù)庫管理用戶ROOT的密碼擂找,默認為空戳吝,回車即可
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
You already have a root password set, so you can safely answer 'n'.
Change the root password? [Y/n] y
//是否更改數(shù)據(jù)庫管理員ROOT用戶的密碼,如果選Y則需要重復輸入兩遍新口令,N跳過
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB 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] y
//是否移除匿名用戶,即不輸入用戶名和密碼即可訪問,建議移除(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] n
//是否禁用root用戶遠程登錄(Y禁止骨坑,N允許)
... skipping.
By default, MariaDB 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] n
//是否刪除數(shù)據(jù)庫軟件自動生成的測試數(shù)據(jù)庫(Y刪除,N不刪除)
... skipping.
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
//是否立即重載權限表(Y重載,N不重載)
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
啟動服務
CentOS 6:
service httpd start //啟動httpd服務
service mysqld start //啟動mysql數(shù)據(jù)庫服務
CentOS 7:
systemctl start httpd //啟動httpd服務
systemctl start mariadb //啟動mariadb數(shù)據(jù)庫服務
測試服務
測試WEB運行環(huán)境
echo "WEB服務器運行環(huán)境良好" >> /var/www/html/index.html
使用瀏覽器或者curl、link等軟件訪問http://127.0.0.1/index.html,如果頁面顯示php環(huán)境運行良好欢唾,即PHP運行環(huán)境可以正常使用,否則不能正常使用且警。
測試php運行環(huán)境
echo "<?php echo "PHP環(huán)境運行良好"; ?>" >> /var/www/html/index.php
使用瀏覽器或者curl、link等軟件訪問http://127.0.0.1/index.php礁遣,如果頁面顯示php環(huán)境運行良好斑芜,即PHP運行環(huán)境可以正常使用,否則不能正常使用。
測試運行連通性
cat << EOF >/var/www/html/mysql.php
<?php
$mysqli=new mysqli("localhost","root","00000000");
if(mysqli_connect_errno()){
echo " 連接數(shù)據(jù)庫失斔罨簟:";
$mysqli=null;
exit;
}
echo " 連接數(shù)據(jù)庫成功杏头!";
$mysqli->close();
?>
EOF
//$mysqli=new mysqli("localhost","root","00000000");
//localhost為服務器地址,root為mysql用戶沸呐,00000000為mysql用戶的密碼醇王。
使用瀏覽器或者curl、link等軟件訪問http://127.0.0.1/mysql.php崭添,如果頁面顯示連接數(shù)據(jù)庫成功寓娩!即PHP運行環(huán)境可以正常使用且數(shù)據(jù)庫也可以正常訪問,否則不能正常使用。
最后編輯httpd文件讓httpd服務識別.php和.phps文件
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
至此LAMP環(huán)境搭建完成呼渣!