• 欢迎访问金笔头博客,这是一个菜鸟(伪)程序员的自留地,欢迎访问我的github:点击进入

Ubuntu 16.04安装AMP(Apache2,MariaDB,PHP7)

Ubuntu eason 3337次浏览 0个评论 扫描二维码

1.安装和配置MariaDB

MariaDB 是 MySQL 的作者 Monty Widenius 所维护的 MySQL 分支版本。MariaDB 兼容 MySQL,并且增加了功能,提高了性能。运行下面的命令来安装 MariaDB-server 和 client:
$ sudo apt-get install mariadb-server mariadb-client
安装完成以后需要设置MariaDB的root密码,默认为空
$ sudo mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we’ll need the current
password for the root user. If you’ve just installed MariaDB, and
you haven’t set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): #直接回车
OK, successfully used password, moving on…
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
… Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
… Success!
Normally, root should only be allowed to connect from ‘localhost’. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y
… Success!
By default, MariaDB comes with a database named ‘test’ that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
– Dropping test database…
… Success!
– Removing privileges on test database…
… Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
… Success!
Cleaning up…
All done! If you’ve completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
使用mysql的命令进行登录测试,检查是否安装成功
$ mysql -h127.0.0.1 -uroot -pXXXXXXXXX
ERROR 1698 (28000): Access denied for user ‘root’@’localhost’
登录报错,好吧,得想办法解决先
$sudo mysql -h127.0.0.1 -uroot -pXXXXXXXXX
以root权限登录mysql发现没有问题,查看一下user表,
Ubuntu 16.04安装AMP(Apache2,MariaDB,PHP7)
错误的起因就是在这里, root的plugin被修改成了unix_socket,用密码登陆的plugin应该是mysql_native_password。执行如下sql:
MariaDB [(none)]> update mysql.user set authentication_string=PASSWORD(‘your password’), plugin=’mysql_native_password’ where user=’root’;
Ubuntu 16.04安装AMP(Apache2,MariaDB,PHP7)
重启服务,再次尝试登录,成功!
Ubuntu 16.04安装AMP(Apache2,MariaDB,PHP7)

2.安装Apache2

$sudo apt-get install apache2
安装完以后启动Apache2服务,访问测试
$sudo service apache2 start
Ubuntu 16.04安装AMP(Apache2,MariaDB,PHP7)
apache 默认文章根目录是 /var/www/html,其主要配置文件为:/etc/apache2/apache2.conf。其配置系统的说明文档在 /usr/share/doc/apache2/README.Debian.gz.
关于Apache2的一些安全性配置
$ sudo vi  /etc/apache2/conf-enabled/security.conf 
ServerTokens Prod
ServerSignature Off
TraceEnable Off
激活Mod_rewrite模块以使页面支持伪静态(非常重要!!!
$ sudo a2enmod rewrite
$ sudo service apache2 start

3.安装 PHP 7

安装 PHP 7 和 Apache PHP 模块:
$ sudo apt-get install php7.0 libapache2-mod-php7.0
$ sudo systemctl restart apache2
测试 PHP 获得一些细节信息,默认web站点的文档根目录是 /var/www/html ,我在该目录创建一个getinfo.php文件,然后在浏览器中访问它。这个文件能够显示有关PHP安装的详细信息。
$ sudo touch /var/www/html/getinfo.php
$ sudo vi /var/www/html/getinfo.php
<?php
phpinfo();
?>
改变getinfo.php文件的所有者为 www-data 用户和组
$chown www-data:www-data /var/www/html/getinfo.php
启用 MySQL / MariaDB 支持,为了使 PHP 支持 MySQL,可以安装 php7.0-mysql 包。同时也可以安装其他所需要的 PHP 模块
$ sudo apt-get -y install php7.0-mysql php7.0-curl php7.0-gd php7.0-intl php-pear php-imagick php7.0-imap php
7.0-mcrypt php-memcache php7.0-pspell php7.0-recode php7.0-sqlite3 php7.0-tidy php7.0-xmlrpc php7.0-xsl php7.0-mbstring php-gettext
重启Apache2
$ sudo systemctl restart apache2
安装 APCu PHP 缓存来加速 PHP 运行。APCu 是一个免费的 PHP opcode cacher 用来缓存和优化 PHP 中间代码,推荐安装它来加速PHP速度
$ sudo apt-get install php-apcu
安装完成后重启 Apache:
$ sudo systemctl restart apache2
待所有php需要的模块都安装完成以后,为提升安全性,我们需要删除getinfo.php以防止他人获取服务器过多信息
$ sudo rm -f /var/www/html/getinfo.php
编辑php.ini文件隐藏 PHP 版本
$ sudo vi /etc/php/7.0/apache2/php.ini
expose_php On
改成
expose_php Off

 


金笔头博客, 版权所有丨如未注明 , 均为原创, 转载请注明Ubuntu 16.04安装AMP(Apache2,MariaDB,PHP7)
喜欢 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址