您的位置:

phpclimysql的简单介绍

本文目录一览:

如何在ubuntu上装nginx+php+mysql

1、先更新ubuntu系统

更新命令

sudo apt-get update

sudo apt-get upgrade

2

添加ubuntu nginx更新源镜像

cd /etc/apt/

sudo cp sources.list sources.list_bak

将如下代码添加到sources.list文件中

deb precise nginx

deb-src precise nginx

使用

sudo vi sources.list

如果提示: W: GPG error: precise Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY ABF5BD827BD9BF62 由于官方不信任该源

解决方法: sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys ABF5BD827BD9BF62

3

3、更新和安装update and install

sudo apt-get update

sudo apt-get install nginx

4、启动nginx

sudo /etc/init.d/nginx start

5、check version

nginx -v

6、配置php+mysql

sudo apt-get install php5-cli php5-cgi mysql-server php5-mysql

7、安装FastCgi

sudo apt-get install spawn-fcgi

8、配置nginx

8.1、修改nginx的配置文件:/etc/nginx/sites-available/default 修改主机名

server_name localhost;

8.2、修改index的一行,添加index.php

index index.php index.html index.htm;

8.3、去掉下面部分的注释用于支持 php 脚本:

location ~ .php$ { include /etc/nginx/fastcgi_params; #需放在第一行,否则会出错 fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name; }

9、 重新启动nginx

/etc/init.d/nginx stop

/etc/init.d/nginx start

10、启动fastcgi php

spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -u www-data -f /usr/bin/php-cgi

11、nginx提示502 错误

nginx 502 Bad Gateway没有启动,启动命令是:

spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -u www-data -f /usr/bin/php-cgi

12、设置开机自启动

Ubuntu开机之后会执行/etc/rc.local文件中的脚本

所以我们可以直接在/etc/rc.local中添加启动脚本。

spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -u www-data -f /usr/bin/php-cgi 添加到语句: exit 0

前面才行

13、no input file specified错误

sudo vi /etc/nginx/sites-available/default

其中这个字段

location ~ .php$ {

root html;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;

include fastcgi_params;

}

注意

fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;

/var/www/nginx-default 改为你的网站根目录,一般就是改成这个。

server 字段下root 目录和网站根目录保持一致

CentOS 7 用户怎样安装 LNMP

1 先说一下

本文使用的主机名称: server1.example.com 和IP地址: 192.168.1.105。这些可能与你的计算机有所不同,注意进行修改。

2 使用外部仓库

Nginx不是从官方CentOS库安装,我们从 nginx 项目安装库安装,修改源:

vi /etc/yum.repos.d/nginx.repo

修改为:

[nginx]

name=nginx repo

baseurl=

gpgcheck=0

enabled=1

3 安装 MySQL

我们先安装MariaDB。一个免费的MySQL 分支。运行此命令:

yum install mariadb mariadb-server net-tools

然后我们创建MySQL系统启动链接(所以MySQL的自动启动时,系统启动)启动MySQL服务器:

systemctl enable mariadb.service

systemctl start mariadb.service

现在检查网络启用。运行

netstat -tap | grep mysql

它应该显示出这样的内容:

[root@example ~]# netstat -tap | grep mysql

tcp 0 0 0.0.0.0:mysql 0.0.0.0:* LISTEN 10623/mysqld

运行

mysql_secure_installation

为用户设置根口令(否则,任何人都可以访问你的MySQL数据库!):

[root@example ~]# mysql_secure_installation

/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found

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] – 回车

New password: – 输入ROOT密码

Re-enter new password: – 再输入一次ROOT密码

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] – 回车

… 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] – 回车

… 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] – 回车

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

installation should now be secure.

Thanks for using MariaDB!

[root@example ~]#

[root@server1 ~]# mysql_secure_installation

4 安装 Nginx

Nginx可以作为一个包从nginx.org安装,运行:

yum install nginx

然后我们创建的系统启动nginx的链接和启动它:

systemctl enable nginx.service

systemctl start nginx.service

有时,你会得到一个错误,如80端口已在使用中,错误消息会是这样的

[root@server1 ~]# service nginx start

Starting nginx: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

nginx: [emerg] still could not bind()

[FAILED]

[root@server1 ~]#

这就意味着有时在运行Apache服务。停止服务,进一步启动服务nginx如下

systemctl stop httpd.service

yum remove httpd

systemctl disable httpd.service

systemctl enable nginx.service

systemctl start nginx.service

开放的HTTP和HTTPS防火墙中的端口

firewall-cmd –permanent –zone=public –add-service=http

firewall-cmd –permanent –zone=public –add-service=https

firewall-cmd –reload

输出的shell结果将看起来像这样:

[root@example ~]# firewall-cmd –permanent –zone=public –add-service=http

success

[root@example ~]# firewall-cmd –permanent –zone=public –add-service=https

success

[root@example ~]# firewall-cmd –reload

success

[root@example ~]#

在你的Web服务器的IP地址或主机名称输入到浏览器(如HTTP:/ /192.168.1.105),你应该看到nginx的欢迎页面。

5 安装 PHP5

我们可以通过PHP-FPM使nginx的PHP5工作(PHP-FPM(FastCGI进程管理器)是一种替代PHP FastCGI执行一些额外的功能,支持任何规模大小,尤其是繁忙的站点很有用)。我们可以安装php-fpmtogether用PHP-CLI和一些PHP5的模块,如PHP,MySQL,你需要的,如果你想使用MySQL的PHP命令如下:

yum install php-fpm php-cli php-mysql php-gd php-ldap php-odbc php-pdo php-pecl-memcache php-pear php-mbstring php-xml php-xmlrpc php-mbstring php-snmp php-soap

APC是一个自由和开放的PHP操作码来缓存和优化PHP的中间代码。它类似于其他PHP操作码cachers,如eAccelerator和XCache。强烈建议有这些安装,以加快您的PHP页面。

我会从PHP PECL库中安装的APC。 PECL要求CentOS开发工具beinstalled编译APC包。

yum install php-devel

yum groupinstall ‘Development Tools’

安装 APC

pecl install apc

[root@example ~]# pecl install apc

downloading APC-3.1.13.tgz …

Starting to download APC-3.1.13.tgz (171,591 bytes)

……………..done: 171,591 bytes

55 source files, building

running: phpize

Configuring for:

PHP Api Version: 20100412

Zend Module Api No: 20100525

Zend Extension Api No: 220100525

Enable internal debugging in APC [no] : – 回车

Enable per request file info about files used from the APC cache [no] : – 回车

Enable spin locks (EXPERIMENTAL) [no] : – 回车

Enable memory protection (EXPERIMENTAL) [no] : – 回车

Enable pthread mutexes (default) [no] : –回车

Enable pthread read/write locks (EXPERIMENTAL) [yes] : – 回车

building in /var/tmp/pear-build-rootVrjsuq/APC-3.1.13

……

然后打开 /etc/php.ini 并设置 cgi.fix_pathinfo=0:

vi /etc/php.ini

[...]

; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP's

; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok

; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting

; this to 1 will cause PHP CGI to fix its paths to conform to the spec. A setting

; of zero causes PHP to behave as before. Default is 1. You should fix your scripts

; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.

;

cgi.fix_pathinfo=0

[...]

并添加行:

[...]

extension=apc.so

在 /etc/php.ini 文件后面。

除此之外,为了避免这样的时区的错误:

[21-July-2014 10:07:08] PHP Warning: phpinfo(): It is not safe to rely on the system’s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected ‘Europe/Berlin’ for ‘CEST/2.0/DST’ instead in /usr/share/nginx/html/info.php on line 2

… in /var/log/php-fpm/www-error.log 当你在浏览器中调用一个PHP脚本,你应该设置 date.timezone in /etc/php.ini:

[...]

[Date]

; Defines the default timezone used by the date functions

;

date.timezone = "Europe/Berlin"

[...]

您可以通过运行正确的时区支持您的系统:

cat /etc/sysconfig/clock

[root@server1 nginx]# cat /etc/sysconfig/clock

ZONE=”Europe/Berlin”

[root@server1 nginx]#

接下来,创建系统启动链接的PHP-FPM并启动它:

systemctl enable php-fpm.service

systemctl start php-fpm.service

PHP-FPM是一个守护进程(使用init脚本/etc/init.d/php-fpm) 运行在端口9000的FastCGI服务器。

在php队列php-resque里头使用了数据库的单例模式显示MySQL server has gone away

PHP的轻量消息队列php-resque使用说明

消息队列处理后台任务带来的问题

项目中经常会有后台运行任务的需求,比如发送邮件时,因为要连接邮件服务器,往往需要5-10秒甚至更长时间,如果能先给用户一个成功的提示信息,然后在后台慢慢处理发送邮件的操作,显然会有更好的用户体验。

为了实现类似的需求,Web项目中一般的实现方法是使用消息队列(Message Queue),比如MemcacheQ,RabbitMQ等等,都是很著名的产品。

消息队列说白了就是一个最简单的先进先出队列,队列的一个成员就是一段文本。正是因为消息队列实在太简单了,当拿着消息队列时,反而有点无从下手的感觉,因为这仅仅一个发送邮件的任务,就会引申出很多问题:

消息队列只能存储字符串类型的数据,如何将一个发送邮件这样的“任务”,转换为消息队列中的一个“消息”?

消息队列只负责数据的存放与进出,本身不能执行任何程序,那么我们要如何从消息队列中一个一个取出数据,再将这些数据转化回任务并执行。

我们无法预知消息队列何时会有数据产生,所以我们的任务执行程序还需要具备监控消息队列的能力,也就是一个常驻后台的守护进程。

一般的Web应用PHP都以cgi方式运行,无法常驻内存。我们知道php还有cli模式,那么守护进程是否能以php cli来实现,效率如何?

当守护进程运行时,Web应用能否与后台守护进程交互,实现开启/杀死进程的功能以及获得进程的运行状态?

Resque对后台任务的设计与角色划分

对以上这些问题,目前为止我能找到的最好答案,并不是来自php,而是来自Ruby的项目Resque,正是由于Resque清晰简单的解决了后台任务带来的一系列问题,Resque的设计也被Clone到Python、php、NodeJs等语言:比如Python下的pyres以及PHP下的php-resque等等,这里有各种语言版本的Resque实现,而在本篇日志里,我们当然要以PHP版本为例来说明如何用php-resque运行一个后台任务,可能一些细节方面会与Ruby版有出入,但是本文中以php版为准。

Resque是这样解决这些问题的:

后台任务的角色划分

其实从上面的问题已经可以看出,只靠一个消息队列是无法解决所有问题的,需要新的角色介入。在Resque中,一个后台任务被抽象为由三种角色共同完成:

Job | 任务 : 一个Job就是一个需要在后台完成的任务,比如本文举例的发送邮件,就可以抽象为一个Job。在Resque中一个Job就是一个Class。

Queue | 队列 : 也就是上文的消息队列,在Resque中,队列则是由Redis实现的。Resque还提供了一个简单的队列管理器,可以实现将Job插入/取出队列等功能。

Worker | 执行者 : 负责从队列中取出Job并执行,可以以守护进程的方式运行在后台。

那么基于这个划分,一个后台任务在Resque下的基本流程是这样的:

将一个后台任务编写为一个独立的Class,这个Class就是一个Job。

在需要使用后台程序的地方,系统将Job Class的名称以及所需参数放入队列。

以命令行方式开启一个Worker,并通过参数指定Worker所需要处理的队列。

Worker作为守护进程运行,并且定时检查队列。

当队列中有Job时,Worker取出Job并运行,即实例化Job Class并执行Class中的方法。

至此就可以完整的运行完一个后台任务。

在Resque中,还有一个很重要的设计:一个Worker,可以处理一个队列,也可以处理很多个队列,并且可以通过增加Worker的进程/线程数来加快队列的执行速度。

php-resque的安装

需要提前说明的是,由于涉及到进程的开辟与管理,php-resque使用了php的PCNTL函数,所以只能在Linux下运行,并且需要php编译PCNTL函数。如果希望用Windows做同样的工作,那么可以去找找Resque的其他语言版本,php在Windows下非常不适合做后台任务。

以Ubuntu12.04LTS为例,Ubuntu用apt安装的php已经默认编译了PCNTL函数,无需任何配置,以下指令均为root帐号安装Redis

apt-get install redis-server

安装Composer

apt-get install curl

cd /usr/local/bin

curl -s | phpchmod a+x composer.phar

alias composer='/usr/local/bin/composer.phar'

使用Composer安装php-resque

假设web目录在/opt/htdocs

apt-get install git git-core

cd /opt/htdocs

git clone git://github.com/chrisboulton/php-resque.gitcd php-resque

composer install

php-resque的使用

编写一个Worker

其实php-resque已经给出了简单的例子, demo/job.php文件就是一个最简单的Job:

class PHP_Job

{

public function perform()

{

sleep(120);

fwrite(STDOUT, 'Hello!');

}

}

这个Job就是在120秒后向STDOUT输出字符Hello!

在Resque的设计中,一个Job必须存在一个perform方法,Worker则会自动运行这个方法。

将Job插入队列

php-resque也给出了最简单的插入队列实现 demo/queue.php:

if(empty($argv[1])) {

die('Specify the name of a job to add. e.g, php queue.php PHP_Job');}

require __DIR__ . '/init.php';

date_default_timezone_set('GMT');

Resque::setBackend('127.0.0.1:6379');

$args = array(

'time' = time(),

'array' = array(

'test' = 'test',

),

);

$jobId = Resque::enqueue('default', $argv[1], $args, true);echo "Queued job ".$jobId."\n\n";

在这个例子中,queue.php需要以cli方式运行,将cli接收到的第一个参数作为Job名称,插入名为'default'的队列,同时向屏幕输出刚才插入队列的Job Id。在终端输入:

php demo/queue.php PHP_Job

结果可以看到屏幕上输出:

Queued job b1f01038e5e833d24b46271a0e31f6d6即Job已经添加成功。注意这里的Job名称与我们编写的Job Class名称保持一致:PHP_Job查看Job运行情况

php-resque同样提供了查看Job运行状态的例子,直接运行:

php demo/check_status.php b1f01038e5e833d24b46271a0e31f6d6可以看到输出为:

Tracking status of b1f01038e5e833d24b46271a0e31f6d6. Press [break] to stop.

Status of b1f01038e5e833d24b46271a0e31f6d6 is: 1我们刚才创建的Job状态为1。在Resque中,一个Job有以下4种状态:

Resque_Job_Status::STATUS_WAITING = 1; (等待)Resque_Job_Status::STATUS_RUNNING = 2; (正在执行)Resque_Job_Status::STATUS_FAILED = 3; (失败)Resque_Job_Status::STATUS_COMPLETE = 4; (结束)因为没有Worker运行,所以刚才创建的Job还是等待状态。

运行Worker

这次我们直接编写demo/resque.php:

?php

date_default_timezone_set('GMT');

require 'job.php';

require '../bin/resque';

可以看到一个Worker至少需要两部分:

可以直接包含Job类文件,也可以使用php的自动加载机制,指定好Job Class所在路径并能实现自动加载包含Resque的默认Worker: bin/resque

在终端中运行:

QUEUE=default php demo/resque.php

前面的QUEUE部分是设置环境变量,我们指定当前的Worker只负责处理default队列。也可以使用QUEUE=* php demo/resque.php

来处理所有队列。

运行后输出为

#!/usr/bin/env php

*** Starting worker

用ps指令检查一下:

ps aux | grep resque

可以看到有一个php的守护进程已经在运行了

1000 4607 0.0 0.1 74816 11612 pts/3 S+ 14:52 0:00 php demo/resque.php再使用之前的检查Job指令

php demo/check_status.php b1f01038e5e833d24b46271a0e31f6d62分钟后可以看到

Status of b1f01038e5e833d24b46271a0e31f6d6 is: 4任务已经运行完毕,同时屏幕上应该可以看到输出的Hello!

至此我们已经成功的完成了一个最简单的Resque实例的全部演示,更复杂的情况以及遗留的问题会在下一次的日志中说明。

在WINDOWS 下PHP.INI的路径以及如何激活mysql扩展库

第一个原因是由于系统所读取的php.ini文件与你当时修改的php.ini文件不一致造成的

1、 可以通过phpinfo()的Configuration File (php.ini) Path选项查看当前PHP服务器读取的是哪一个php.ini

2、 如果你要更改php.ini的文件存取位置,可参考php.ini的搜索顺序,如下所示:

a)�0�2�0�2�0�2�0�2�0�2�0�2 SAPI 模块所指定的位置(Apache 2 中的 PHPIniDir 指令,CGI 和 CLI 中的 -c 命令行选项,NSAPI 中的 php_ini 参数,THTTPD 中的 PHP_INI_PATH 环境变量)

b)�0�2�0�2�0�2�0�2�0�2�0�2 HKEY_LOCAL_MACHINE\SOFTWARE\PHP\IniFilePath(Windows 注册表位置)

c)�0�2�0�2�0�2�0�2�0�2 PHPRC 环境变量

d)�0�2�0�2�0�2�0�2�0�2�0�2 当前工作目录(对于 CLI)

e)�0�2�0�2�0�2�0�2�0�2�0�2 web 服务器目录(对于 SAPI 模块)或 PHP 所在目录(Windows 下其它情况)

f)�0�2�0�2�0�2�0�2�0�2�0�2 Windows 目录(C:\windows 或 C:\winnt),或 --with-config-file-path 编译时选项指定的位置

3、 一般做法:通过设置PHPRC 环境变量,

a)�0�2�0�2�0�2�0�2�0�2�0�2 操作:右击我的电脑-属性-高级-环境变量

b)�0�2�0�2�0�2�0�2�0�2�0�2 在系统变量下新建一个“变量名为PHPRC�0�2�0�2 ,变量值为你的php.ini文件所地文件路径”的环境变量第二个原因是:PHP没有找到相应的扩展库

1、 查看PHP找到的扩展库位置:在phpinfo()的extension_dir选项可以看到

2、 在php.ini更改extension_dir文件第三个原因:没有完全重启IIS

1、 对php.ini的修改必须完全重启IIS才能起作用,而不能只是对当前站点进行重启。

A:在PHP5.0以上版本,MySQL 默认未启用,因此需要咱们进行手工激活。

1、 在PHP运行MySql,必须在 php.ini 中激活 php_mysql.dll 动态连接库,另外还需要访问 MySQL 客户端连接库即libmysql.dll文件

2、 可以通过php.ini中的extension激活 php_mysql.dll 动态连接库;而libmysql.dll文件必须放在Windows 的系统路径 PATH才能访问,可以通过把libmysql.dll复制到system32目录下,也可以在PATH环境变量中新增一个路径。

3、 建议做法:通过设置PATH,方便以后的php升级,在系统环境变量中的PATH后增加“libmysql.dll”所在的文件夹即可。记得通过“;”号隔开

4、 [案例]最近,服务器总是找不到MySql扩展库,导致程序无法运行。因此,把所有文件删除后重新配置PHP服务器。但最后在安装MySql的时候,却始终无法激活MySql扩展库。

在phpinfo()中找到的信息如下:

PATH:c:\php

PHPRC:C:\php

extension_dir:c:\php\ext\

另外,我测试了将其它的库激活,如msql、gd2、XML,在phpinfo()都可以正常显示

证明PHP能找到php_mysql.dll 动态连接库,而MySql的运行的另一个条件是能够讯问MySQL 客户端连接库即libmysql.dll文件,看来问题就在这里了。

在记事本里写好的PHP代码 要怎样运行

PHP的运行分两种,一种是CLI,一种是CGI/FASTCGI。

第一种你只要在你的console里边(win下是cmd,linux就是terminal)输入php path_to_your_php_file.php就好了,如果你没有把php.exe配置进你的系统变量里,还需要把php改成php/php.exe。

第二种如1楼,需要cgi运行环境,也就是要一个服务器,也就是一楼介绍的,除了1楼的apache还可以选择其他的如IIS,NGINX等server,至于如何配置,请自行检索,很长,就不写了。