手机版

Ubuntu 17.10 用 apt 搭建 lamp 环境、安装 phpmyadmin、redis 服务+扩展、mysql 扩展、开启错误提示、配置虚拟主机

时间:2020-05-13 来源:互联网 编辑:宝哥软件园 浏览:

一、最终环境

Ubuntu17.10、Apache2.4.27、MySQL5.7.20、PHP7.1.8

二、安装 apache

官方源有,直接安装:

sudo apt-get install apache2

三、安装 mysql

官方源有,直接安装:

sudo apt-get install mysql-server

安装期间会提示设置 MySQL administrator 的密码

============================================

PS:需要什么软件或包,直接用 apt-cache 搜索

apt-cache search <关键词>

确认包名后,直接用 apt-get install 安装。

四、安装 php

官方源有 php7.1,直接安装:

sudo apt-get install php7.1 php7.1-dev

注意带上版本号,不然默认安装 7.x。php7.1 是主程序,php7.1-dev 是 7.1 版的开发包(有 phpize、php-config 等等,phpize 可以为 已编译好的 php 加载外挂模块,php-config 可以获得 php 的详细配置)。

============================================

如果要装 php5.6 的话,推荐这个 PPA 源:ppa:ondrej/php。这个源有 php5.6 和 php7.x 以及绝大多数的 php 扩展,包括 redis、memcache、mongodb 等等。

添加 ppa:ondrej/php 源:

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update

安装 php5.6:

sudo apt-get install php5.6 php5.6-dev

五、重启 apache

sudo /etc/init.d/apache2 restart

更多选项:

Usage: apache2 {start|stop|graceful-stop|restart|reload|force-reload}

六、检查 apache

访问 http://localhost/。这是 apache 服务器的默认页在 /var/www/html 下,里面还介绍了 apche 的相关配置文件。

Ubuntu 17.10 用 apt 搭建 lamp 环境、安装 phpmyadmin、redis 服务+扩展、mysql 扩展、开启错误提示、配置虚拟主机

七、检查 mysql

终端输入 mysql,跟着打两个 Tab,看到所有有关 mysql 的命令包:

mingc@mingc-GE60-2PL:~$ mysql
mysql                      mysql_install_db
mysqladmin                 mysqloptimize
mysqlanalyze               mysql_plugin
mysqlbinlog                mysqlpump
mysqlcheck                 mysqlrepair
mysql_config_editor        mysqlreport
mysqld                     mysql_secure_installation
mysqld_multi               mysqlshow
mysqld_safe                mysqlslap
mysqldump                  mysql_ssl_rsa_setup
mysqldumpslow              mysql_tzinfo_to_sql
mysql_embedded             mysql_upgrade
mysqlimport               

输入 mysql -u<你的账号> -p ,回车,输入密码,进入 mysql:

mingc@mingc-GE60-2PL:~$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 73
Server version: 5.7.20-0ubuntu0.17.10.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql>

八、检查 php

php -v

不出意外的话,应该显示 php 的版本

============================================

也可以查看 apache 和 mysql 版本:

apache2 -v
mysql -V

九、创建 php 探针

sudo vim /var/www/html/info.php

添加如下内容:

<?php
phpinfo();

改变所有者:

sudo chown www-data:www-data /var/www/html/info.php

访问 http://localhost/info.php,得到有关 PHP 的详细页面。

十、安装 phpmyadmin

sudo apt-get install phpmyadmin

安装期间,

询问要连接的服务器,选择 apache2;

询问创建 phpmyadmin 的数据库,选择“是”;

询问设置登录 phpmyadmin 的用户和密码。

然后浏览器访问:http://localhost/phpmyadmin

apt

十一、安装 redis 服务和 redis 扩展

官方源有,直接安装:

(1)redis 服务器

sudo apt-get install redis-server

期间,一些需要的工具包也会自动安装。安装好后,默认自启动、后台运行,配置文件在 /etc/redis 目录下。

(2)php 的 redis 扩展模块

sudo apt-get install php-redis

其他的扩展也可以这么装,利用 apt-cache search 搜索包名,再用 apt-get install 安装。

重启 apche, sudo /etc/init.d/apache2 restart

访问 http://localhost/info.php,确认 redis 扩展:

 edis

测试一下:

终端输入 redis-,跟着打两个 Tab 键,看到有关 redis 的命令和工具:

mingc@mingc-GE60-2PL:~$ redis-
redis-benchmark  redis-check-rdb  redis-server    
redis-check-aof  redis-cli

redis-cli 是 访问 redis-server 的客户端接口,执行 redis-cli 即可登录到 redis 服务器:

mingc@mingc-GE60-2PL:~$ redis-cli
127.0.0.1:6379>

更多 redis-cli 的命令选项,使用 redis-cli –help 查看。

再写一段 php 脚本测试一下, vim /var/www/html/test_redis.php

内容如下:

<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "Connection to server sucessfully";
echo "Server is running: " . $redis->ping(); 

十二、安装 mysql 扩展

在上面的 http://localhost/info.php 里可以看到,没有 mysql 扩展。虽说 php7 弃用了 mysql 扩展,但有些旧项目还是需要的。装一下 mysql 扩展。

这里选择编译安装(通过 apt 安装的 php-mysql 扩展,如果是 php7.x,那就是装 pdo 和 pdo_mysql 扩展)

(1)直接从 PECL 官方站 搜索 mysql,找到 MySQL 扩展的 页面 ,点击页面的 [ Browse Source ],选择最新的 commit、下载 tar.gz 包。我下载后的包名为 mysql-fae9884.tar.gz。

(2)编译安装:

tar -xf mysql-fae9884.tar.gz
cd mysql-fae9884
phpize
./configure --with-php-config=/usr/bin/php-config
sudo make && sudo make install

前面装 php7.1 的时候,也装了 php7.1-dev,这里面就有 phpize 和 php-config。而 –with-php-config 是 php-config 程序(命令)的位置,可以使用 whereis php-config 查看。

编译安装成功后,会有成功提示 和 mysql 扩展模块(.so 文件)的路径:

Build complete.
Don't forget to run 'make test'.

Installing shared extensions:     /usr/lib/php/20160303/  

然后,编辑 php 用于 apache 的配置文件 /etc/php/7.1/apache2/php.ini,在最后添加一行:

extension=mysql.so

重启 apache, sudo /etc/init.d/apache2 restart

访问 http://localhost/info.php,确认下有了 mysql 扩展。

 未分类

写一段 php 脚本测试: vim /etc/www/html/test_mysql.php

内容如下:

<?php
$mysql = mysql_connect('127.0.0.1', 'root', 'root');
if(!$mysql) {
    die(mysql_error($mysql));
}
echo 'Ok' . "
";

由于 mysql 模块是手动编译安装的,所以默认不会进入 php 的命令行配置。

下面把 mysql 模块加入到 php 的 cli(命令行) 配置,让 php 的命令行模式也支持 mysql 模块:

sudo vim /etc/php/7.1/mods-available/mysql.ini

编辑内容如下:

extension=mysql.so

建立符号链接:

sudo ln -s /etc/php/7.1/mods-available/mysql.ini /etc/php/7.1/cli/conf.d/mysql.ini

检查一下,确认 php 命令行模式启用了 mysql 扩展模块:

php -m | grep mysql  

十三、开启 php 和 apache 的错误提示

默认不显示 php 错误,下面开启。

(1)修改 php 的配置文件,在 /etc/php/7.1/apache2 下,打开 php.ini。

(2)搜索 display_errors = Off,修改为 On

(3)搜索 error_reporting = E_ALL & ~E_NOTICE,修改为 E_ALL | E_STRICT(搜不到的话就搜短一点:“error_reporting =”)。

(4)修改 apache 的配置文件,在 /etc/apache2 下,打开 apache.conf。

(5)文件最后添加两行:

php_flag display_errors        on
php_value error_reporting       2039

(6)重启 apache:

sudo /etc/init.d/apache2 restart

十四、创建虚拟主机

创建虚拟主机自然用 apache 配置,了解一下 apache 的配置目录:

/etc/apache2
├── apache2.conf      # 主配置文件,其他的一些配置文件通过 Include 指令包含进来
├── conf-available    # 所有可用的配置文件(里面的 *.conf 文件内容几乎都被默认注释了)
├── conf-enabled      # 可用的配置文件中,启用了哪些,一般都是符号链接、指向上面的 conf-available 目录里每个 *.conf 文件
├── envvars           # 环境变量
├── magic
├── mods-available    # 所有可用的模块
├── mods-enabled      # 哪些模块被启用了
├── ports.conf        # 定义端口监听
├── sites-available   # 重点来了:所有可用的站点
└── sites-enabled     # 哪些站点被启用了

步骤简单,就是在 sites-available 目录定义站点配置文件,然后在 sites-enabled 里建立指向这个配置文件的符号链接。

(1)建配置

cd /etc/apache2/sites-available/
sudo cp 000-default.conf my.site.conf
vim my.site.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 my.site
    # 域名别名,可以设置多个,空格隔开
    ServerAlias my.site

    ServerAdmin webmaster@localhost

    # 站点根目录
    DocumentRoot /var/www/my.site
    <Directory "/var/www/my.site/">
        # 启用符号链接
        Options FollowSymLinks

        DirectoryIndex index.php index.html index.htm

        # 注意这个配置,会影响本地目录下的 .htaccess 的启用
        AllowOverride All

        Order deny,allow
        Allow from All

        # 限制访问目录,多个目录用冒号隔开
        # php_admin_value open_basedir "/var/www/my.site/:/tmp:/usr/lib/php/"
    </Directory>

    # 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>

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

精简版(去掉了多余注释):

<VirtualHost *:80>
    ServerName my.site
    ServerAlias my.site
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/my.site
    <Directory "/var/www/my.site/">
        Options FollowSymLinks
        DirectoryIndex index.php index.html index.htm
        AllowOverride All
        Order deny,allow
        Allow from All
        # php_admin_value open_basedir "/var/www/my.site/:/tmp:/usr/lib/php/"
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

(2)建符号链接

sudo ln -s /etc/apache2/sites-available/my.site.conf /etc/apache2/sites-enabled/my.site.conf

(3)检查语法

终端执行:

apachectl configtest

(4)添加 hosts 解析

vim /etc/hosts ,添加一行:

127.0.0.1   my.site

(5)重启 apache

sudo /etc/init.d/apache2 restart

然后,按照配置的站点目录 /etc/www/my.site,创建目录和测试文件:

sudo makedir -p /etc/www/my.site
cd /etc/www/my.site/
sudo echo "<h2>Welcome to my.site!</h2>" > index.html

浏览器访问 http://my.site,

 phpMyAdmin

OK,完成~~

十五、相关链接

PHP 及扩展的 PPA 源:https://launchpad.net/~ondrej/+archive/ubuntu/php/+index?batch=75&memo=75&start=75

版权声明:Ubuntu 17.10 用 apt 搭建 lamp 环境、安装 phpmyadmin、redis 服务+扩展、mysql 扩展、开启错误提示、配置虚拟主机是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。

相关文章推荐