树莓派raspberry搭建web服务(基于LAMP)

本文永久地址:https://my.oschina.net/bysu/blog/1550212

1.安装apache

sudo apt-get install apache2 php-gd php

 

安装完之后,怎么确认是否安装成功了呢?可以通过以下几种方式确认。

a.可以查看是否已有相应的服务

ps -ef | grep apache

会看到4条服务,其中主进程服务的用户为root 

b.可以在浏览器里面输入

查ip地址

ifconfig
http://查出来的ip地址
譬如我查出来的ip地址为192.168.31.46
即是:
http://192.168.31.46

c.这时有人或许会说,我的树莓派是通过xshell以shh方式远程连接进去的,怎么查看呢?这也难不倒劳动人民。终端输入:

curl http://192.168.31.46 >apache_result.txt
说明一下:
1.这个ip地址更换成你自己的即可;
2.apache_result.txt这个文件名随便命名,就是把请求的响应写到这个文本文件里面而已,方便查找

通过ls命令查找当前目录,可以看到有一个apache_result.txt的文件

ls

这是可以通过vi或者less或者more之类的命令打开文本文件,这里我通过less打开,并进行相应的操作

less apache_result.txt
打开之后,输入
?works
然后回车,可以通过n或者N来查找,如果看到It works!那就证明成功了。
如果需要退出,那么就输入
q
回车即可

d.或者可以通过命令

给/var/www/html/目录赋权限
sudo chmod a+w -R /var/www/html/

向目录html中创建一个display.html的文件,并写入内容:Hello,appche!
sudo echo "Hello,apache!" > /var/www/html/display.html

curl http://192.168.31.46/display.html
回车之后,终端显示
Hello,appche!

apache2的配置文件:

主配置文件为/etc/apache2/apache2.conf

还有一个重要的配置,就是默认目录的更改。默认的目录其实是记录在:/etc/apache2/sites-available/000-default.conf 文件当中(在/etc/apache2/sites-enable/中也有一个指向他的链接)

 

如果修改了Apache的配置文件,则可通过命令,不用重启计算机即可生效

sudo /etc/init.d/apache2 reload
对apache2服务的操作,还有restart,start,stop

查看Apache服务器日志文件,可以到/var/init.d/apache2/这个目录下面

错误日志在error.log中,服务器访问的日志在access.log中

查看apache2服务的状态

systemctl status apache2.service

2.安装MySQL

命令如下:

sudo apt-get install mysql-server

启动Mysql的守护进程和Apache

service mysqld start

设为开机启动

update -rc.d mysqld enable
update -rc.d httpd enable

或者用一下方式,我之前用archlinux,习惯了这种方式设置开机启动
systemctl enable mariadb
systemctl enable apache2

然后对安装进行设置.在这里划重点了.网上和很多书本的所有教材基本都没说到这一步,害我百度了一个下午都没弄好.因为安装Mysql的过程没有让设置root用户的密码,导致通过mysql -uroot -p来登录,怎么都登录不进去,通过忘记root密码的方式来修改也不行.虽然以前知道可以通过命令来设置root,但是苦于没找对命令的目录.<<树莓派实战秘籍>>作者:[美]Ruth Suehle Tom Callaway 感谢这本书的作者

/usr/bin/mysql_secure_installation   #必须以root账户运行

运行上述命令之后,会被要求设置一个root密码(使用root用户,输入root用户的密码)以及以下几个事项:

 a)为root用户设置密码
  b)删除匿名账号
  c)取消root用户远程登录
  d)删除test库和对test库的访问权限
  e)刷新授权表使修改生效

下面这段代码摘自自:http://www.jb51.net/article/47727.htm

 mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, 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 MySQL
root user without the proper authorisation.
Set root password? [Y/n] <– 是否设置root用户密码,输入y并回车或直接回车
New password: <– 设置root用户的密码
Re-enter new password: <– 再输入一次你设置的密码
Password updated successfully!
Reloading privilege tables..
… Success!
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL 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] <– 是否删除匿名用户,生产环境建议删除,所以直接回车
… 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] <–是否禁止root远程登录,根据自己的需求选择Y/n并回车,建议禁止
… Success!
By default, MySQL 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] <– 是否删除test数据库,直接回车
- 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] <– 是否重新加载权限表,直接回车
… Success!
Cleaning up…
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!

登录Mysql数据库

mysql -uroot -p

输入上面那一步设置好的密码,回车就大功告成了.

新增普通用户(参考自:https://my.oschina.net/bysu/blog/1572517

$ mysql -u root -p
MariaDB> CREATE USER 'monty'@'localhost' IDENTIFIED BY 'some_pass';
MariaDB> GRANT ALL PRIVILEGES ON mydb.* TO 'monty'@'localhost';
MariaDB> FLUSH PRIVILEGES;
MariaDB> quit
GRANT ALL PRIVILEGES ON *.* TO 'bysu'@'localhost' IDENTIFIED VIA unix_socket WITH GRANT OPTION;
GRANT PROXY ON ''@'%' TO 'bysu'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
 FLUSH PRIVILEGES;

显示授权信息,我是按照root的来授权的。

SHOW GRANTS FOR 'yangfan'@'localhost';

3.安装 PHP 5.6和安装apache php 模块

apt-get -y install php libapache2-mod-php

4、让 MySQL 或者 MariaDB 数据库获得 PHP 支持。

sudo apt-get install php-mysql

修改php配置文件

sudo vi  /etc/php/7.0/apache2/php.ini

取消前面的;注释符号

extension=php_mysqli.dll
extension=php_pdo_mysql.dll

搜索一下模块:apt-cache search php5

安装模块,说明一下,老的 mysql 安装 php5-mysql 包获得支持,但是我们这里安装的 mariaDB,最好安装 php5-mysqlnd,但会使老的 mysql 出错,好主意是安装其他一些模块:

apt-get -y install php5-mysqlnd php5-curl php5-gd php5-intl php-pear  php5-imap php5-mcrypt php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl

其中:

PHP-gd模块是用来支持wordPress的

php5-imagick,imagick是一个PHP的扩展,它调用ImageMagick提供的API来进行图片的操作,这个可能安装不上,附上手动安装的教程:http://blog.sina.com.cn/s/blog_3edc5e2e0102w0n5.html

php5-memcache,缓存服务的客户端,以分担数据库服务器的压力。具体说明可以参照这个链接:http://www.myexception.cn/php/443683.html。安装另行搜索引擎吧!

再重启 apache 是安装模块生效。

service apache2 restart

 

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

卸载mysql:

删除MySQL的方法
代码如下:

sudo apt-get autoremove --purge mysql-server-5.7
sudo apt-get remove mysql-server
sudo apt-get autoremove mysql-server
sudo apt-get remove mysql-common

 

上面的可能会有些是多余的,之后需要清理残余数据

 dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P

 

MySQL密码的恢复方法

有可能你的系统没有 safe_mysqld 程序(比如我现在用的 ubuntu操作系统, apt-get安装的mysql) , 下面方法可以恢复

1. 停止mysqld; 

/etc/init.d/mysql stop

(您可能有其它的方法,总之停止mysqld的运行就可以了)

2. 用以下命令启动MySQL,以不检查权限的方式启动; 

mysqld --skip-grant-tables &

3. 然后用空密码方式使用root用户登录 MySQL; 

mysql -u root

4. 修改root用户的密码; 

mysql> update mysql.user set password=PASSWORD('newpassword') where User='root'; 
mysql> flush privileges; 
mysql> quit

重新启动MySQL

/etc/init.d/mysql restart

就可以使用新密码 newpassword 登录了。

 

我在Ubuntu16.04版本中使用终端安装MySQL5.7时,按照度娘的教程,搜索如何安装,大多是如下代码:

 sudo apt-get install mysql-server
 sudo apt-get install mysql-client
  •  

检查MySQL是否运行:

 sudo netstat -tap | grep mysql
  •  

如果成功安装,我的会显示如下内容:

tcp        0      0 localhost:mysql         *:*                     LISTEN      18475/mysqld 
  •  

PS:重启/打开/关闭MySQL的方法是:sudo service mysql restart/start/stop

就这两个命令就安装好了,可是我在安装过程中并没有出现要我写用户名和密码的地方,我一脸懵逼, 
完成后在终端输入mysql -u root -p之后,要求我输入密码,可是我并不知道密码,随便输入之后,

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
  •  

百度了三五个小时,解决方案五花八门,我最后使用有效的方法是: 
打开一个文件

 sudo vim /etc/mysql/debian.cnf
  •  

在这个文件里面有着MySQL默认的用户名和用户密码, 
最最重要的是:用户名默认的不是root,而是debian-sys-maint,如下所示

 # Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
***user     = debian-sys-maint
password = Z1fVrmTiZNxxw29o***
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = Z1fVrmTiZNxxw29o
socket   = /var/run/mysqld/mysqld.sock
  •  

密码会随即给一个很复杂的,这个时候,要进入MySQL的话,就是需要在终端把root更改为debian-sys-maint,如下代码

 mysql -u debian-sys-maint -p
  •  

然后终端会提示你输入密码

 Enter password: 
  •  

这是输入文件中的密码即可成功登陆。 
当然了,这之后就要修改密码了,毕竟密码太难记。

经过度娘的指导,我所安装的版本是5.7,所以password字段已经被删除,取而代之的是authentication_string字段,所以要更改密码:

mysql> update mysql.user set authentication_string=password('password') where user='root'and Host = 'localhost';
  • 1

如果显示:

Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1
  • 1

则代表成功修改,之后需要*重启**MySQL,方可登录成功。 

未完待续

armhf架构mysql 的下载地址:https://packages.debian.org/jessie/mysql-server-5.5

http://blog.csdn.net/faryang/article/details/50788795

 

 

转载于:https://my.oschina.net/bysu/blog/1550212

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值