Ubuntu 搭建LAMP開發環境

我的服務器環境爲:Ubuntu 14.04 64位

安裝 Apache2:
    sudo apt-get install apache2
安裝PHP模塊:
    sudo apt-get install php5
安裝Mysql
    sudo apt-get install mysql-server

其他模塊安裝:
    sudo apt-get install libapache2-mod-php5
    sudo apt-get install libapache2-mod-auth-mysql
    sudo apt-get install php5-mysql
    sudo apt-get install php5-gd
前兩個很容易明白,要想apache能夠解析PHP,就需要藉助這兩個模塊來找到php engine。
第三個在php操作mysql數據庫時用到,大多數人都有數據庫編程經驗,所以這就不用多解釋了。
第四個GD庫。

apache2相關配置:
安裝完上述的各個模塊後,實際上基本配都OK了,只是一些小的細節問題。基本上多數的配置都在/etc/apache2目錄及其子目錄下完成,故一定要搞清楚這個目錄結構。

在Windows下,Apache的配置文件通常只有一個,就是httpd.conf。但在Ubuntu Linux上用apt-get install apache2命令安裝了Apache2後,沒發現httpd.conf配置文件,Ubuntu下Apache的配置文件是 /etc/apache2/apache2.conf,它把各個設置項分在了不同的配置文件中,看起來複雜,但仔細想想設計得確實很合理。

apache2.conf內容如下:
# This is the main Apache server configuration file.  It contains the
# configuration directives that give the server its instructions.

# It is split into several files forming the configuration hierarchy outlined
# below, all located in the /etc/apache2/ directory:
#
#	/etc/apache2/
#	|-- apache2.conf
#	|	`--  ports.conf
#	|-- mods-enabled
#	|	|-- *.load
#	|	`-- *.conf
#	|-- conf-enabled
#	|	`-- *.conf
# 	`-- sites-enabled
#	 	`-- *.conf
#
#
# * apache2.conf is the main configuration file (this file). It puts the pieces
#   together by including all remaining configuration files when starting up the
#   web server.
#
# * ports.conf is always included from the main configuration file. It is
#   supposed to determine listening ports for incoming connections which can be
#   customized anytime.
#
# * Configuration files in the mods-enabled/, conf-enabled/ and sites-enabled/
#   directories contain particular configuration snippets which manage modules,
#   global configuration fragments, or virtual host configurations,
#   respectively.
#
#   They are activated by symlinking available configuration files from their
#   respective *-available/ counterparts. These should be managed by using our
#   helpers a2enmod/a2dismod, a2ensite/a2dissite and a2enconf/a2disconf. See
#   their respective man pages for detailed information.
#
# * The binary is called apache2. Due to the use of environment variables, in
#   the default configuration, apache2 needs to be started/stopped with
#   /etc/init.d/apache2 or apache2ctl. Calling /usr/bin/apache2 directly will not
#   work with the default configuration.


# Global configuration
#

#
# ServerRoot: The top of the directory tree under which the server's
# configuration, error, and log files are kept.
#
# NOTE!  If you intend to place this on an NFS (or otherwise network)
# mounted filesystem then please read the Mutex documentation (available
# at <URL:http://httpd.apache.org/docs/2.4/mod/core.html#mutex>);
# you will save yourself a lot of trouble.
#
# Do NOT add a slash at the end of the directory path.
#
#ServerRoot "/etc/apache2"

#
# The accept serialization lock file MUST BE STORED ON A LOCAL DISK.
#
Mutex file:${APACHE_LOCK_DIR} default

#
# PidFile: The file in which the server should record its process
# identification number when it starts.
# This needs to be set in /etc/apache2/envvars
#
PidFile ${APACHE_PID_FILE}

#
# Timeout: The number of seconds before receives and sends time out.
#
Timeout 300

#
# KeepAlive: Whether or not to allow persistent connections (more than
# one request per connection). Set to "Off" to deactivate.
#
KeepAlive On

#
# MaxKeepAliveRequests: The maximum number of requests to allow
# during a persistent connection. Set to 0 to allow an unlimited amount.
# We recommend you leave this number high, for maximum performance.
#
MaxKeepAliveRequests 100

#
# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
#
KeepAliveTimeout 5


# These need to be set in /etc/apache2/envvars
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}

#
# HostnameLookups: Log the names of clients or just their IP addresses
# e.g., www.apache.org (on) or 204.62.129.132 (off).
# The default is off because it'd be overall better for the net if people
# had to knowingly turn this feature on, since enabling it means that
# each client request will result in AT LEAST one lookup request to the
# nameserver.
#
HostnameLookups Off

# ErrorLog: The location of the error log file.
# If you do not specify an ErrorLog directive within a <VirtualHost>
# container, error messages relating to that virtual host will be
# logged here.  If you *do* define an error logfile for a <VirtualHost>
# container, that host's errors will be logged there and not here.
#
ErrorLog ${APACHE_LOG_DIR}/error.log

#
# LogLevel: Control the severity of messages logged to the error_log.
# Available values: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the log level for particular modules, e.g.
# "LogLevel info ssl:warn"
#
LogLevel warn

# Include module configuration:
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
IncludeOptional mods-available/php5.load
IncludeOptional mods-available/php5.conf
# Include list of ports to listen on
Include ports.conf


# Sets the default security model of the Apache2 HTTPD server. It does
# not allow access to the root filesystem outside of /usr/share and /var/www.
# The former is used by web applications packaged in Debian,
# the latter may be used for local directories served by the web server. If
# your system is serving content from a sub-directory in /srv you must allow
# access here, or in any related virtual host.
<Directory />
	Options FollowSymLinks
	AllowOverride None
	Require all denied
</Directory>

<Directory /usr/share>
	AllowOverride None
	Require all granted
</Directory>

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

#<Directory /srv/>
#	Options Indexes FollowSymLinks
#	AllowOverride None
#	Require all granted
#</Directory>

# AccessFileName: The name of the file to look for in each directory
# for additional configuration directives.  See also the AllowOverride
# directive.
#
AccessFileName .htaccess

#
# The following lines prevent .htaccess and .htpasswd files from being
# viewed by Web clients.
#
<FilesMatch "^\.ht">
	Require all denied
</FilesMatch>


#
# The following directives define some format nicknames for use with
# a CustomLog directive.
#
# These deviate from the Common Log Format definitions in that they use %O
# (the actual bytes sent including headers) instead of %b (the size of the
# requested file), because the latter makes it impossible to detect partial
# requests.
#
# Note that the use of %{X-Forwarded-For}i instead of %h is not recommended.
# Use mod_remoteip instead.
#
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

# Include of directories ignores editors' and dpkg's backup files,
# see README.Debian for details.

# Include generic snippets of statements
IncludeOptional conf-enabled/*.conf

# Include the virtual host configurations:
IncludeOptional sites-enabled/*.conf

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

1.apache根目錄
安裝完apache2,根目錄在/var/www下,可以通過http://localhost/測試一下是否好用,當然也可以在該目錄下新建一個文件test.html來試一試http://localhost/test.html。
如何知道的呢?apache2.conf裏並沒有DocumentRoot項,經查找相關資料發現,在/etc/apache2/sites-available/000-default.conf中,有如下內容:
<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        ServerName localhost

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

2.PHP解析問題
安裝完貌似php的解析都有點問題,瀏覽php網頁會保存,apache沒有將其解析爲網頁。網上一般說的是需要在httpd.conf中添加XXXX,對其他的linux系統可能確實如此,但是Ubuntu有點特殊。
Ubuntu的apache2配置在/etc/apache2目錄下。
這個目錄下有個apache2.conf文件,這個文件通過包含其他配置文件涵蓋了所有的apache2系統配置信息。
php解析部分在的配置在/etc/apache2/mods-available下的php5.conf和php5.load中,apache2.conf文件中並沒有將這兩個文件包含進來,只要包含進去就OK了。
  *************************************************
在apache2.conf中找到
  # Include module configuration:
  Include /etc/apache2/mods-enabled/*.load
  Include /etc/apache2/mods-enabled/*.conf
在其後面添加
  Include /etc/apache2/mods-available/php5.load
  Include /etc/apache2/mods-available/php5.conf
  *************************************************
另外一種方法就是將這兩個文件鏈接到mods-enabled目錄下:
  sudo ln -s /etc/apache2/mods-available/php5.load /etc/apache2/mods-enabled/php5.load
  sudo ln -s /etc/apache2/mods-available/php5.conf /etc/apache2/mods-enabled/php5.conf
這一種方式更好一點,沒有破壞apache2本身的配置結構。
  *************************************************

3.配置apache2的開發目錄
apache2的默認目錄配置在/etc/apache2/sites-enabled/000-default文件中,找到該文件中的DocumentRoot項, 將/var/www改爲你的開發目錄就OK了。
當然,還有一種方法就是不改變默認目錄,通過配置虛擬主機的方式來滿足我們部署多個項目的需求:

在 /etc/apache2下有一個sites-available目錄和一個sites-enabled目錄,這裏面是放什麼的呢?這裏面可以配置虛擬主機,其中sites-available裏放置真正的配置文件,而sites- enabled目錄存放的只是一些指向這裏的文件的符號鏈接,通過ln命令自動生成,比如我們在sites-available中創建一個虛擬主機的配置文件001-test.conf,這個“有效”的虛擬主機真正要發揮效果的的話要放到/etc/apache2/sites-enabled 文件夾下面
<VirtualHost *:8899>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/tmpnet
    ServerName  tmpnet
    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>
    <Directory /var/www/html/tmpnet/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        #Order allow,deny
        #allow from all
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

我們可以使用ln命令來建立一對關聯文件:
    sudo ln -s /etc/apache2/sites-available/001-test.conf /etc/apache2/sites-enabled/001-test.conf
通過這種方式來啓用配置,如果apache上配置了多個虛擬主機,每個虛擬主機的配置文件都放在 sites-available下,那麼對於虛擬主機的停用、啓用就非常方便了:當在sites-enabled下建立一個指向某個虛擬主機配置文件的鏈接時,就啓用了它;如果要關閉某個虛擬主機的話,只需刪除相應的鏈接即可,根本不用去改配置文件。

[注意]鏈接文件名中不能含有“.”,否則apache2會將其當作爲一個文件試圖解析而無法達到鏈接目錄的效果。

重啓apache
虛擬主機配置文件完成後,謹慎起見,我們在重啓服務前先檢查下語法:
sudo apache2ctl configtest
沒有錯誤的話,再重啓Apache

sudo /etc/init.d/apache2 restart
service apache2 restart

新增監聽端口:
ports.conf is always included from the main configuration file. It is supposed to determine listening ports for incoming connections which can be customized anytime.
比如添加8899和8989端口,只需要在ports.conf中按如下配置即可:
# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf

Listen 8899
Listen 8989

<IfModule ssl_module>
        Listen 443
</IfModule>

<IfModule mod_gnutls.c>
        Listen 443
</IfModule>

注意事項:
1.配置文件需要以 .conf 結束;
2.你配置的虛擬主機可能還是不能正常運行,你還需要通過如下命令禁用默認配置:
sudo a2dissite 000-default.conf
然後重新加載
sudo service apache2 reload

參考文章:
http://stackoverflow.com/questions/23785896/ubuntu-14-04-apache-2-4-7-virtualhost-not-working-redirecting
http://www.cnblogs.com/ylan2009/archive/2012/02/25/2368028.html

備註:
Linux常用命令,按ESC鍵 跳到命令模式,然後:
:w   保存文件但不退出vi;
:w file 將修改另外保存到file中,不退出vi;
:w!   強制保存,不推出vi;
:wq  保存文件並退出vi;
:wq! 強制保存文件,並退出vi;
q:  不保存文件,退出vi;
:q! 不保存文件,強制退出vi;
:e! 放棄所有修改,從上次保存文件開始再編輯;
發佈了144 篇原創文章 · 獲贊 29 · 訪問量 35萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章