環境:CentOS 7 最小化安裝 采用Putty連接
方法:采用YUM安裝方法
目的:搭建Apache+Mysql+PHP環境
1,安裝Apache
yum install httpd //默認情況下,選擇Y,進行安裝 安裝成功后,默認情況下,是禁止外部IP訪問的,需要進行設置 vi /etc/httpd/conf/httpd.conf //進入配置文件 找到 <Directory /> AllowOverride none Require all denied </Directory > 修改為: <Directory /> AllowOverride none Require all granted </Directory > systemctl start httpd.service //啟動 systemctl restart httpd.service //停止 systemctl status httpd.service //查看狀態 systemctl restart httpd.service //重啟
systemctl enable httpd.service //開機啟動
訪問服務器IP,如果顯示測試界面,則安裝成功:
2、安裝PHP
yum install php //默認選擇Y安裝 在Apche的目錄下面新建一文件test.php cd /var/www/html vi test.php 可以鍵入相關PHP代碼,以輸入hello world為例, <?php echo "hello world"; ?> 訪問網站192.168.199.235/test.php,如果正常解析,則說明PHP環境完成。
3、安裝Mysql
在CentOS7中,mariadb代替了Mysql,其實mariadb只是一個M有sql的一個分支,由於Mysql舊部員工不滿Oracle收購Mysql導致更新速度變慢,又重新開發了和Mysql類似的開源數據庫。來應對Oracle的Mysql。
yum install mariadb maridb-server //默認安裝 安裝成功后,root用戶默認密碼為空且僅限本機登陸 mysqladmin -u root --password 'password' //修改root用戶密碼 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY 'password' WITH GRANT OPTION; //配置任意IP遠程訪問 flush privileges; //刷新權限 systemctl restart mariadb.service //重啟服務
這樣任意IP就可以通過mysql -h 192.168.199.235 -u root -p 訪問服務器數據庫了。當然為了安全起見,是指定特定主機訪問數據庫服務器。
默認情況下,PHP不支持mariadb,需要安裝php擴展。
yum install php-mysql 在網站目錄下新建測試數據庫文件,例如 <?php $con = mysql_conncet('localhost','root','root'); if(!$con){ die("connet mysql failed".mysql.error()); } echo "connet mysql successful"; ?> 默認情況下,php沒有打開錯誤調試,需要在/etc/php.ini中將錯誤調試打開,即 ;dispaly_errors ;error_reporting 前面的;去掉。
至此,簡單的LAMP環境搭建成功!
總的感覺,之前使用的是集成環境,總是聽說配置環境還是挺復雜的,今天看來還是挺輕松的。對於簡單的使用,目前的環境就就足夠了,需要安裝什么擴展,后續安裝即可。