歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> Linux安裝LAMP開發環境及配置文件管理

Linux安裝LAMP開發環境及配置文件管理

日期:2017/3/3 12:53:23   编辑:Linux技術

Linux安裝LAMP開發環境及配置文件管理

Linux主要分為兩大系發行版,分別是RedHat和Debian,lamp環境的安裝和配置也會有所不同,所以分別以CentOS 7.1和Ubuntu 14.04做為主機(L)
Linux下安裝軟件,最常見有源碼安裝方式、RPM/deb安裝方式、yum/apt-get安裝方式等,在這裡使用yum/apt-get安裝LAMP開發環境

CentOS 7.1 下安裝LAMP開發環境及配置文件管理

安裝並配置Apache
[root@localhost ~]# yum install httpd

默認安裝的Apache版本為httpd-2.4.6-31.el7.centos.x86_64,默認配置文件主目錄位於/etc/httpd下,/etc/httpd/conf/httpd.conf是Apache的主配置文件,Apache模塊位於/usr/lib64/httpd/modules目錄下,Apache模塊的配置文件位於/etc/httpd/conf.modules.d目錄下,設置禁用或開啟模塊可以通過修改該目錄下每個文件,Web根目錄位於/var/www/html目錄下,日志文件位於/var/log/httpd目錄下。
重點關注/etc/httpd目錄。這個目錄下,有個conf.d目錄,默認情況下/etc/httpd/conf.d目錄下所有的“.conf”結尾的文件都會被讀取。因此,很多情況下不需要修改主配置文件/etc/httpd/conf/httpd.conf,而是在/etc/httpd/conf.d目錄下新建一個以“.conf”結尾的文件來完成各種配置。
將Apache設置為開機自啟動模式
[root@localhost wuxiwei]# systemctl enable httpd

關閉Apache服務
[root@localhost wuxiwei]# systemctl stop httpd

開啟Apache服務
[root@localhost wuxiwei]# systemctl start httpd

重新加載httpd
[root@localhost wuxiwei]# systemctl reload httpd

安裝並配置MariaDB(MYSQL)
[root@localhost wuxiei]# yum install mariadb-server mariadb

MariaDB完全兼容MYSQL,包括API和命令行。CentOS 從7.x開始默認使用MariaDB。
通過內置的安全配置腳本可實現對數據庫的安全保護
[root@localhost wuxiwei]# /usr/bin/mysql_secure_installation

將MariaDB設置為開機啟動
[root@localhost wuxiwei]# systemctl enable mariadb

開啟MariaDB服務
[root@localhost wuxiwei]# systemctl start mariadb

關閉MariaDB服務
[root@localhost wuxiwei]# systemctl stop mariadb

安裝並配置PHP
[root@localhost wuxiei]# yum install php php-cli php-pear php-pdo php-mysqlnd php-gd php-mbstring php-mcrypt php-xml

CentOS 7.1版本中,默認安裝PHP為PHP5.4版本,其中php-mysqlnd是PHP源碼提供的MYSQL驅動數據庫。
很多時候會對PHP環境要求校新的版本,例如PHP5.6環境,記錄一種通過yum工具安裝最新PHP版本的方法。首先,需要在系統上安裝一個擴展yum源,即epel源。可從http://fedoraproject.org/wiki/EPEL 網站下載並安裝。
[root@localhost wuxiwei]# wget' target='_blank'>http://mirrors.neusoft.edu.cn/epel/7/x86_64/e/epel-release-7-5.noarch.rpm[/code] 
[root@localhost wuxiwei]# rpm -ivh epel-release-7-5.noarch.rpm

接著,還需要一個REMI源,這個yum源提供了最新的PHP版本的下載和安裝,它的官網http://rpms.famillecollet.com/ 。安裝REMI源的過程如下。
[root@localhost wuxiwei]# rpm --import' target='_blank'>http://rpms.famillecollet.com/RPM-GPG-KEY-remi[/code] 
[root@localhost wuxiwei]# wget' target='_blank'>http://rpms.famillecollet.com/enterprise/remi-release-7.rpm[/code] 
[root@localhost wuxiwei]# rpm -ivh remi-release-7.rpm

默認情況下,REMI是禁用的,防止多個yum源發生沖突。可以通過命令查看REMI源是否成功安裝
[root@localhost wuxiwei]# yum repolist disabled | grep remi

搜索REMI源倉庫中可用的包
[root@localhost wuxiwei]# yum --enablerepo=remi list php

[root@localhost wuxiwei]# yum --enablerepo=remi-php56 list php

通過REMI源安裝需要的PHP版本,安裝PHP5.6版本。
[root@localhost wuxiwei]# yum --enablerepo=remi-php56 install php

Ubuntu 14.04 下安裝LAMP開發環境及配置文件管理

安裝並配置Apache
[root@localhost wuxiwei]# apt-get install apache2

重啟Apache服務
[root@localhost wuxiwei]# service apache2 restart

安裝並配置PHP5
[root@localhost wuxiwei]# apt-get install php5

查看Apache是否已經正確配置PHP5
[root@localhost wuxiwei]# cat /etc/apache2/mods_enables/libphp5.so

安裝PHP5常用擴展
[root@localhost wuxiwei]# apt-get install php5-gd curl libcurl3 libcurl3-dev php5-curl

安裝並配置MYSQL
[root@localhost wuxiwei]# apt-get install mysql-server

查看PHP5和MYSQL是否可以正常數據交互
[root@localhost wuxiwei]# cat /etc/php5.d/conf.d/mysql.ini

手動安裝PHP5對於MYSQL擴展
[root@localhost wuxiwei]# apt-get install php5-mysql

重啟MYSQL服務
[root@localhost wuxiwei]# service mysql restart

配置文件管理
Apache配置文件位於/etc/apache2目錄下,Apache加載配置首先加載/etc/apache2/apache2.conf文件,通過Include將其他配置文件載入,核心配置文件包括:mods-xxx Apache模塊;sites-xxx 虛擬主機,其中關鍵詞available表示可以使用的;enable表示已啟用的,兩者通過ln -s命令建立軟連接。
PHP5配置文件位於/etc/php5目錄下,核心配置文件php.ini。
MYSQL配置文件位於/etc/mysql目錄下,核心配置文件my.cnf,默認數據庫存儲位於/var/lin/mysql目錄下。

Apache虛擬主機配置

CentOS 7.0 在/etc/httpd/conf.d目錄下新建wuxiwei.conf文件,並重啟apache。
Ubuntu 14.04 在/etc/apache2/sites-available目錄下新建wuxiwei.conf文件,同時在/etc/apache2/sites-enabled目錄下創建軟鏈接到wuxiwei.conf文件,並重啟apache。
wuxiwei.conf文件內容基本如下。
[code]<VirtualHost *:80>
#管理員郵箱
ServerAdmin [email protected]
#訪問的主機名
ServerName wuxiwei.cn
#ServerName別名,通過別名也可以訪問這個虛擬主機
ServerAlias www.wuxiwei.cn
#主機目錄
DocumentRoot /wuxiwei

<Directory "/wuxiwei/">
#指定該目錄啟用FollowSymLinks特性,None:表示不起用任何的服務器特性,Indexes:如果輸入的網址對應服務器上的一個目錄,而此目錄中又沒有Directorylndex指令(例如:Directorylndex index.php index.html),那麼服務器就會返回由mod_autoindex模塊生成的一個格式化後的目錄列表,並列出該目錄下所有的文件。
    Options FollowSymLinks
    #允許所有都可以訪問
    AllowOverride All
    Require all granted
</Directory>

#錯誤日志
Errorlog /var/log/httpd/wuxiwei_error.log
#訪問日志
CustomLog /vat/log/httpd/wuxiwei_access.log combined
</VirtualHost>
Copyright © Linux教程網 All Rights Reserved