分类 php 下的文章

centos7安装搭建LNMP

nginx

    rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
   yum install nginx
   systemctl start nginx
   yum install links
   links 127.0.0.1
   links 192.168.208.6
   firewall-cmd --get-active-zones 
   firewall-cmd --zone=public --list-services 
   firewall-cmd --permanent --zone=public --add-service=http
   firewall-cmd --reload

安装安装成功,可以测试,出现nginx欢迎页面就ok

mysql

   rpm -ivf mysql-community-release-el7-5.noarch.rpm 
   yum search yum-utils
   yum install yum-utils
   yum-config-manager --disable mysql56-community
   yum-config-manager --enable mysql57-community-dmr
   yum search mysql-server
   yum repolist | grep 'mysql'
   yum repolist enabled | grep 'mysql'
   yum search mysql-community-server
   yum install mysql-community-server
   [root@localhost log]# systemctl start mysqld.service 
   [root@localhost log]# grep 'temporary password' /var/log/mysqld.log
2015-06-08T01:44:44.713032Z 1 [Warning] A temporary password is generated for root@localhost: ln3hb?;Jkk6w

    [root@localhost log]# mysql_secure_installation 

    Securing the MySQL server deployment.

    Enter password for root user: 这里填grep 'temporary password' /var/log/mysqld.log命令显示出的密码

    The existing password for the user account has expired. Please set a new password.
    重新设置密码
    New password: 

    Re-enter new password: 

    VALIDATE PASSWORD PLUGIN can be used to test passwords
    and improve security. It checks the strength of password
    and allows the users to set only those passwords which are
    secure enough. Would you like to setup VALIDATE PASSWORD plugin?
    密码强度插件
    Press y|Y for Yes, any other key for No: y

    There are three levels of password validation policy:

    LOW    Length >= 8
    MEDIUM Length >= 8, numeric, mixed case, and special characters
    STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

    Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
    Using existing root password.

    Estimated strength of the password: 100 
    Change the root password? (Press y|Y for Yes, any other key for No) : n

     ... skipping.
    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.
    删除anonymous用户,
    Remove anonymous users? (Press y|Y for Yes, any other key for No) : 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.
    是都禁止root远程连接
    Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n

     ... skipping.
    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.

    删除test数据库
    Remove test database and access to it? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : y
    Success.

    All done!

php

yum install epel-release
   rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
   yum install --enablerepo=remi --enablerepo=remi-php56 php php-fpm php-opcache php-devel php-mbstring php-mcrypt php-mysqlnd php-phpunit-PHPUnit php-pecl-xdebug php-pecl-xhprof

整合nginx和php
修改vi /etc/php.ini
cgi.fix_pathinfo=0
修改nginx配置文件

vim /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    root   /usr/share/nginx/html;
    location / {
        #root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        #root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
    #    root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

[root@localhost log]# systemctl start php-fpm.service
[root@localhost log]# systemctl restart nginx.service 

ok
php和mysql
新建一个文件连接mysql试试

<?php
$con = mysql_connect("192.168.208.6:3306","root","testTEST!@#123");
if($con){
echo "ok";
}else{
echo mysql_error();
}
?>

但是连接报错
Permission denied
于是用远程连接mysql试试看
进入mysql

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY 'testTEST!@#123' WITH GRANT OPTION;
FLUSH PRIVILEGES;

然后再本机上面用远程连接时ok的,于是试着关掉selinux,

setenforce 0

就ok了
于是再开启selinux,查看http相关的selinux选项

[root@localhost html]# getsebool -a|grep -i httpd | grep net
httpd_can_network_connect --> off
httpd_can_network_connect_cobbler --> off
httpd_can_network_connect_db --> off
httpd_can_network_memcache --> off
httpd_can_network_relay --> off

修改selinux的选项

[root@localhost html]# setsebool -P httpd_can_network_connect=1

再就ok了~~~

提高PHP速度的几种办法

PHP,是英文超级文本预处理语言Hypertext Preprocessor的缩写。PHP 是一种 HTML 内嵌式的语言,是一种在服务器端执行的嵌入HTML文档的脚本语言,语言的风格有类似于C语言,被广泛的运用。下面介绍提高PHP速度的方法。
用单引号来包含字符串要比双引号来包含字符串更快一些。因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译注:PHP手册中说echo是语言结构,不是真正的函数,故把函数加上了双引号)。
1、如果能将类的方法定义成static,就尽量定义成static,它的速度会提升将近4倍。
2、$row[’id’] 的速度是$row[id]的7倍。
3、echo 比 print 快,并且使用echo的多重参数(译注:指用逗号而不是句点)代替字符串连接,比如echo $str1,$str2。
4、在执行for循环之前确定最大循环数,不要每循环一次都计算最大值,最好运用foreach代替。
5、注销那些不用的变量尤其是大数组,以便释放内存。
6、尽量避免使用__get,__set,__autoload。
7、require_once()代价昂贵。
8、include文件时尽量使用绝对路径,因为它避免了PHP去include_path里查找文件的速度,解析操作系统路径所需的时间会更少。
9、如果你想知道脚本开始执行(译注:即服务器端收到客户端请求)的时刻,使用$_SERVER[‘REQUEST_TIME’]要好于time()。
10、函数代替正则表达式完成相同功能。
11、str_replace函数比preg_replace函数快,但strtr函数的效率是str_replace函数的四倍。
12、如果一个字符串替换函数,可接受数组或字符作为参数,并且参数长度不太长,那么可以考虑额外写一段替换代码,使得每次传递参数是一个字符,而不是只写一行代码接受数组作为查询和替换的参数。
13、使用选择分支语句(译注:即switch case)好于使用多个if,else if语句。
14、用@屏蔽错误消息的做法非常低效,极其低效。
15、打开apache的mod_deflate模块,可以提高网页的浏览速度。
16、数据库连接当使用完毕时应关掉,不要用长连接。
17、错误消息代价昂贵。
18、在方法中递增局部变量,速度是最快的。几乎与在函数中调用局部变量的速度相当。
19、递增一个全局变量要比递增一个局部变量慢2倍。
20、递增一个对象属性(如:$this->prop++)要比递增一个局部变量慢3倍。
PHP的优点之一是速度很快,对于一般的网站应用,可以说是已经足够了。不过如果站点的访问量很高、带宽窄或者其它的因素令服务器产生性能瓶颈的时候,提高速度是我们首先想到的。希望本文对你有帮助。

php闭包

<?php
//提到闭包就不得不想起匿名函数,
//也叫闭包函数(closures),貌似PHP闭包实现主要就是靠它。声明一个匿名函数是这样:
$func=function ($value='')
{
    echo "test!!!";
};
$func();//输出test!!!
?>

闭包函数当成变量使用

<?php
//闭包函数当作变量来使用
$func_1=function ($value='')
{
    echo "This is func_1!!!";
};
$func_2=function ($value='')
{
    echo "This is func_2!!!";
};
//闭包函数当作变量来使用
function test($value){
    $value();
}
test($func_1);//输出  This is func_1!!!
test($func_2);//输出  This is func_2!!!
//或者直接传递
test(function ($value='')//输出  This is func_3!!!
{
    echo "This is func_3!!!";
});
?>

闭包函数使用外部变量:PHP在默认情况下,匿名函数不能调用所在代码块的上下文变量,而需要通过使用use关键字。
如下,会报错

<?php
//闭包函数当作变量来使用
$str='This is string';
$num=1;
$func_1=function ($value='')
{
    echo "This is func_1!!!";
    echo $str;//报错Notice: Undefined variable: str in D:\wamp\wamp\www\mytest\tp_ceshi.php on line <em>8</em>
    echo $num;//报错Notice: Undefined variable: num in D:\wamp\wamp\www\mytest\tp_ceshi.php on line <em>9</em>
};
$func_1();
?>
```php
如下
```php
<?php
//闭包函数使用外部变量
$str='This is string';
$num=1;
$func_1=function ($value='')use($str,$num)
{
    echo "This is func_1!!!";
    echo "<br/>";
    echo $str;
    echo "<br/>";
    echo $num;
};
$func_1();
//输出
This is func_1!!!
This is string
1
?>

闭包改变外部变量

<?php
//闭包函数使用外部变量
$str='This is string';
$num=1;
$func_1=function ($value='')use($str,$num)
{
    echo $num;
    $num++;
};
$func_1();
echo $num;
//输出结果: 1   1
?>

和一般函数一样,闭包函数不能改变外部变量的值,因为是值传递,想要改变外部变量的值,只需要在传递变量进去的时候在变量前加上"&"
php php

php发送get,post

转载自:http://blog.csdn.net/haha00217/article/details/7969504

一,用file_get_contents来发送get以get方式接受

<?php
$url='http://localhost/mytest/tset.php';
$html=file_get_contents($url);
var_dump($html);
?>

二, 用fopen打开url, 以get方式获取内容

<?php
$url='http://localhost/mytest/tset.php';
//php手册上说是把资源绑定到一个流上面,成功之后返回文件指针资源
$html=fopen($url, 'r');
//这个函数会返回当前协议文件中取得一些信息(php手册上这样说,我自己把这个理解成取得当前请求的一些信息,比如请求是否成功,服务器版本啊之类的信息)
var_dump(stream_get_meta_data($html));
$str='';
//因为$html是一个指针资源,不能直接输出,所以要使用下面的循环来取出,类似于从数据库取出资源
//循环,没到文件结尾就循环feof检测是否到达文件指针结尾
while (!feof($html)) {
    //从文件指针中读取后面设置的字节
    $str.=fgets($html,1024);
}
var_dump($str);
//关闭已经打开的文件指针,免得占用资源,感觉和mysql连接一样,不用的时候要关掉~~~
fclose($html);
?>

三,用file_get_contents函数,以post方式获取

<?php
$url='http://localhost/mytest/tset.php';
//要传递的参数
$data=array('boo'=>'bar');
//这个函数是可以把数组转换成boo=bar形式,现在参数很少所以没什么,但是参数多的话用这个函数很方便(应该是这个作用吧~~)
$data=http_build_query($data);
//http请求的参数,注意header项的值要用双引号,不然\r\n不会被解析成换行符,会影响传值,别的影响没遇到,不知道
$options=array(
    'http'=>array(
            'method'=>'POST',
            'header'=>"Content-type: application/x-www-form-urlencoded\r\n"."Content-Length: " . strlen($data) . "\r\n",
            'content' => $data
    )
);
//创建一个上下文资源流
$content=stream_context_create($options);
//发送请求第一个参数是文件位置,改成false和true没什么影响啊,第三个就是http请求头以及一些设置选项了
$html=file_get_contents($url,true,$content);
var_dump($html);
?>

四,用fsockopen函数
post

<?php
//@file phpinput_post.php
//要传递的参数
$http_entity_body = 'n=' . urldecode('perfgeeks') . '&p=' . urldecode('7788');
//应该是请求发送给接受者的实体正文类型
$http_entity_type = 'application/x-www-form-urlencoded';
//正文的长度
$http_entity_length = strlen($http_entity_body);
$host = '127.0.0.1';
$port = 80;
$path = 'http://localhost/mytest/tset.php';
//依次是,主机,端口,错误号,错误信息,过期时间,这步好像没发请求
$fp = fsockopen($host, $port, $error_no, $error_desc, 30);
if ($fp) {
    //写协议内容。。。页面地址
fputs($fp, "POST {$path} HTTP/1.1\r\n");
//主机
fputs($fp, "Host: {$host}\r\n");
//请求正文类型
fputs($fp, "Content-Type: {$http_entity_type}\r\n");
//正文长度
fputs($fp, "Content-Length: {$http_entity_length}\r\n");
//这个不知道....
fputs($fp, "Connection: close\r\n\r\n");
//发送的数据
fputs($fp, $http_entity_body . "\r\n\r\n");
$d='';
//取出数据
while (!feof($fp)) {
$d .= fgets($fp, 4096);
}
//关闭连接
fclose($fp);
//直接输出会输出http协议信息,所以做一些处理
//先从http协议中计算出正文的长度
preg_match('/Content-Length: (\d+)/', $d, $maths );
//var_dump($maths[1]);
//用字符串的总长度减去正文的长度就是http协议的长度了,再用substr截取就ok了
$d=substr($d,strlen($d)-(int)$maths[1]);
//输出请求结果
echo $d;
}
?>

get

<?php
//@file phpinput_post.php
//要传递的参数
$http_entity_body = 'n=' . urldecode('perfgeeks') . '&p=' . urldecode('7788');
$host = '127.0.0.1';
$port = 80;
$path = 'http://localhost/mytest/tset.php';
//依次是,主机,端口,错误号,错误信息,过期时间,这步好像没发请求
$fp = fsockopen($host, $port, $error_no, $error_desc, 10);
if ($fp) {
    //写协议内容。。。页面地址
fputs($fp, "GET {$path}?{$http_entity_body} HTTP/1.1\r\n");
//主机
fputs($fp, "Host: {$host}\r\n");
//这个不知道....
fputs($fp, "Connection: close\r\n\r\n");
$d='';
//取出数据
while (!feof($fp)) {
$d .= fgets($fp, 4096);
}
//关闭连接
fclose($fp);
//直接输出会输出http协议信息,所以做一些处理
//先从http协议中计算出正文的长度
preg_match('/Content-Length: (\d+)/', $d, $maths );
//var_dump($maths[1]);
//用字符串的总长度减去正文的长度就是http协议的长度了,再用substr截取就ok了
//$d=substr($d,strlen($d)-(int)$maths[1]);
//输出请求结果
echo $d;
}
?>

五,使用curl库,使用curl库之前,需要查看一下php.ini是否已经打开了curl扩展

<?php
//初始化一个curl回话(感觉就是实例化了一个对象。。)
$cu=curl_init();
$timeout=30;
//设置一个curl回话的传输选项,有很多选项可以设置,这里就不一一设置了
curl_setopt($cu, CURLOPT_URL, 'http://localhost/mytest/tset.php');
//设置值为"1"成功会返回请求页面的内容,设置为"0"成功会返回1
curl_setopt($cu, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cu, CURLOPT_CONNECTTIMEOUT, $timeout);
$res=curl_exec($cu);
echo $res;
?>