Ubuntu 16.04下Apache2+php7以及Nginx+php7迷醉记录

目标:Apache建站,要在jquery脚本中通过ajax调用自己的一段php代码。

达成:

jquery的ajax的error的三个参数

jquery通过在script标签中用http的方式请求到,不需要下载到本地;

php中的json_encode

php的文件格式

问题:出现parserror,经过alert发现,返回的是整个php

原因:Apache2无法对php进行解析。虽然Apache装了,php也装了,但是两者之间的交互模块没有装好。所以apache把php认识为纯文本


目标:让apache与php连接起来

过程:

网上搜到的是在apache的配置里加一句AddType就可以让apache知道php后缀的文件要在哪里处理,但是没有用

发现自己在configure php的时候,没有加入--with-apxs2选项。所以要重新编译。

加入之后,甚至连apache都重启不了了,提示线程php是线程不安全的版本。才知道php居然还分线程安全和不安全两套。

后来查了一下,编译的时候还有一个选项(这么多麻烦的选项。。。)可以设置成线程安全的。

但是编译着就提示
make: *** [sapi/cli/php] Error 1

发现自己没有iconv这个库。所以要安装一下。

但是安装的过程中又发现一个stdio.in.h里面有个地方要修改。改了之后再来编译,还通不过。


目标:重新安装apache和php

过程:

这个时候决定全部重新来一遍,且不自己编译php了。也补装了apache和php连接的库。但装到最后还是提示了需要线程安全的版本,没办法了,弃坑。


目标:转战nginx,两个小时后终于全部可以了

过程:

nginx就比apache方便许多,按照https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-in-ubuntu-16-04#step-3-install-php-for-processing的方法,完成的最后。只需要修改一下info.php开头,把第一行的空格去掉保证格式正确后就可以了。否则是空白页。

于是成功地解析并执行了.php文件,可以alert出json格式的内容了。


目标:想通过php调用本地的shell脚本

问题:执行无效

过程:bash中是一句touch。在shell客户端输入bash命令,是有效的。但是客户端调用php就发觉,虽然可以返回值,但是shell脚本没有被执行。搜到了解决方案,来自https://unix.stackexchange.com/questions/115054/php-shell-exec-permission-on-linux-ubuntu

The Apache’s user www-data need to be granted privileges to execute certain applications using sudo.


Run the command sudo visudo. Actually we want to edit the file in etc/sudoers.To do that, by using sudo visudo in terminal ,it duplicate(temp) sudoers file to edit.
At the end of the file, add the following ex:-if we want to use command for restart smokeping and php command for another action in your question,
www-data ALL=NOPASSWD: /etc/init.d/smokeping/restart, usr/bin/php


(This is assuming that you wish to run restart and php commands using super user (root) privileges.And you use php command in usr/bin/ path )


However, if you wish to run every application using super user privileges, then add the following instead of what’s above.You might not want to do that, not for ALL commands, very dangerous.


www-data ALL=NOPASSWD: ALL
3.After edit the sudoers file(by visudo we edit the temp file of sudoers so save and quit temp file(visudo) to write in sudoers file.(wq!)


4.That’s it, now use exec() or shell_exec in the following manner inside your xxx.phpscript.keep remember to use sudo before the command use in the php script.


ex:-


exec ("sudo /etc/init.d/smokeping restart 2>&1");
or


shell_exec("sudo php -v"); 
So in your problem,add the commands that you wish to use in to the step no (2.) as I add and change your php script as what you want.


here is the same problem as yours http://stackoverflow.com/a/22953339/1862107


其他的一些收获:

sudo su root切换到root账户

sudo apt-get --purge remove

visudo

cat /etc/issue查看ubuntu版本号

/var/www,对应www-data这个用户

找到教程时,要注意版本对应,配置文件都不一样

都是php5,但已经php7了

dpkg -l显示软件包

nano也是一种文本编辑器

service [] restart

/etc/init.d/[] restart


这个搞了上午两个小时下午两个小时吧,有点醉

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值