linux下配置PHP开发环境

这里安装典型的LAMP框架。

前期准备

安装APR,下载地址

$ wget http://mirrors.hust.edu.cn/apache//apr/apr-1.5.2.tar.gz
$ tar -zxvf apr-1.5.2.tar.gz
$ cd apr-1.5.2
$ ./configure
$ make
$ sudo make install

$ wget http://mirrors.hust.edu.cn/apache//apr/apr-util-1.5.4.tar.gz
$ tar -zxvf apr-util-1.5.4.tar.gz
$ ./configure --with-apr=/usr/local/apr
$ make
$ sudo make install

$ http://mirrors.hust.edu.cn/apache//apr/apr-iconv-1.2.1.tar.gz
$ tar -zxvf apr-iconv-1.2.1.tar.gz
$ ./configure --with-apr=/usr/local/apr
$ make
$ sudo make install

安装libxml2

$ wget ftp://rpmfind.net/linux/centos/7.2.1511/updates/x86_64/Packages/libxml2-2.9.1-6.el7_2.3.x86_64.rpm
$ wget ftp://rpmfind.net/linux/centos/7.2.1511/updates/x86_64/Packages/libxml2-devel-2.9.1-6.el7_2.3.x86_64.rpm
$ sudo rpm -ivh libxml2-2.9.1-6.el7_2.3.x86_64.rpm
$ sudo rpm -ivh libxml2-devel-2.9.1-6.el7_2.3.x86_64.rpm

安装其他工具

$ sudo yum install gd gd-devel php-gd
$ sudo yum install libjpeg-devel
$ sudo yum install freetype-devel

安装Apache

Apache下载地址
下载Apache

$ wget http://mirrors.hust.edu.cn/apache/httpd/httpd-2.4.23.tar.gz

安装apache

$ wget http://mirrors.hust.edu.cn/apache//httpd/httpd-2.4.23.tar.gz
$ tar -xzvf httpd-2.4.23.tar.gz
$ cd httpd-2.4.23/
$ ./configure -enable-module=so
$ make
$ sudo make install

启动与停止

$ cd /usr/local/apache2/bin
$ sudo ./apachectl start  # 启动
$ sudo ./apachectl stop  # 停止
$ sudo ./apachectl restart  # 重启

启动后访问:http://114.215.93.126/
出现:It works!

安装MySQL

mysql下载地址 

安装mysql

$ tar -xvf mysql-5.7.16-1.el7.x86_64.rpm-bundle.tar 
$ sudo rpm -ivh mysql-community-libs-5.7.16-1.el7.x86_64.rpm
$ sudo rpm -ivh mysql-community-client-5.7.16-1.el7.x86_64.rpm
$ sudo rpm -ivh mysql-community-common-5.7.16-1.el7.x86_64.rpm
$ sudo rpm -ivh mysql-community-server-5.7.16-1.el7.x86_64.rpm
$ sudo rpm -ivh mysql-community-devel-5.7.16-1.el7.x86_64.rpm

安装完成以后修改登录密码

$ service mysqld start  # 启动
$ sudo grep 'temporary password' /var/log/mysqld.log  # 此步骤将显示一个临时密码,用这个密码登录
$ mysql -u root -p
$ ALTER USER 'root'@'localhost' IDENTIFIED BY 'password!';  # 修改密码

安装php

$ wget http://cn2.php.net/distributions/php-7.0.10.tar.gz
$ tar -zxvf php-7.0.10.tar.gz
$ cd php-7.0.10
$ ./configure --with-apxs2=/usr/local/apache2/bin/apxs  \
--with-mysql \
--with-mysqli \
--with-pdo-mysql \
--enable-mysqlnd \
--enable-mbstring \
--with-gd \
--with-jpeg-dir \
--with-freetype-dir
$ make
$ sudo make install

ps:如果遇到php找不到某些模块,则需要重新编译,在重新编译之前,需要运行: make clean,并编译安装完成之后重启Apache。

安装完成后,修改httpd.conf文件

...
<Directory "/usr/local/apache2/htdocs">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   AllowOverride FileInfo AuthConfig Limit
    #
    AllowOverride All

    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>
...
# 修改
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule php7_module        modules/libphp7.so
...
# 修改,这里可不修改,这里我改成了当前的登录用户
User jipeng
Group jipeng
...
#ServerName www.example.com:80
# 修改
ServerName host:80
...
#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
# 修改
<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>
...

<IfModule mime_module>
    #
    # TypesConfig points to the file containing the list of mappings from
    # filename extension to MIME-type.
    #
    TypesConfig conf/mime.types

    #
    # AddType allows you to add to or override the MIME configuration
    # file specified in TypesConfig for specific file types.
    #
    #AddType application/x-gzip .tgz
    # 添加
    AddType application/x-httpd-php .php .phtml
    AddType application/x-httpd-php-source .phps
    #
    # AddEncoding allows you to have certain browsers uncompress
    # information on the fly. Note: Not all browsers support this.
    #
    #AddEncoding x-compress .Z
    #AddEncoding x-gzip .gz .tgz
    #
    # If the AddEncoding directives above are commented-out, then you
    # probably should define those extensions to indicate media types:
    #
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    #
    # AddHandler allows you to map certain file extensions to "handlers":
    # actions unrelated to filetype. These can be either built into the server
    # or added with the Action directive (see below)
    #
    # To use CGI scripts outside of ScriptAliased directories:
    # (You will also need to add "ExecCGI" to the "Options" directive.)
    #
    #AddHandler cgi-script .cgi

    # For type maps (negotiated resources):
    #AddHandler type-map var

    #
    # Filters allow you to process content before it is sent to the client.
    #
    # To parse .shtml files for server-side includes (SSI):
    # (You will also need to add "Includes" to the "Options" directive.)
    #
    #AddType text/html .shtml
    #AddOutputFilter INCLUDES .shtml
</IfModule>