Ubuntu 搭建LAMP开发环境

15 篇文章 1 订阅
8 篇文章 1 订阅
我的服务器环境为: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! 放弃所有修改,从上次保存文件开始再编辑;
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值