欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

ubuntu下安装 nginx + php + memcached + mariadb

程序员文章站 2022-04-13 08:36:56
...

一,apt-get 安装 1,安装nginx sudo apt-get install nginx 所有的配置文件都在/etc/nginx下,虚拟主机配置在/etc/nginx/sites-available下 程序文件在/usr/sbin/nginx 日志放在了/var/log/nginx中 并已经在/etc/init.d/下创建了启动脚本nginx 默认的虚拟主

一,apt-get 安装

1,安装nginx

sudo apt-get install nginx

所有的配置文件都在/etc/nginx下,虚拟主机配置在/etc/nginx/sites-available下

程序文件在/usr/sbin/nginx

日志放在了/var/log/nginx中

并已经在/etc/init.d/下创建了启动脚本nginx

默认的虚拟主机的目录设置在了/var/www/nginx-default

  启动nginx

sudo /etc/init.d/nginx start
sudo /etc/init.d/nginx reload

2,安装php

fpm

sudo apt-get install php5-fpm
/etc/init.d/php5-fpm start

or fcgi

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

3,安装FastCgi

 apt-get install php5-cgi

也可以再安装spawn-fcgi,spawn-fcgi是fastcgi的管理程序,从Lighthttpd独立出来的项目。实际运营中可以使用php-fpm(php的fastcgi php manager). php 5.3.3中自带php-fpm,但我现在的版本是php 5.3.2.

采用spawn-fcgi执行

sudo /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pid

参数意义:

* -f 指定调用FastCGI的进程的执行程序位置,根据系统上所装的PHP的情况具体设置 
* -a 绑定到地址addr 
* -p 绑定到端口port 
* -s 绑定到unix socket的路径path 
* -C 指定产生的FastCGI的进程数,默认为5(仅用于PHP) 
* -P 指定产生的进程的PID文件路径 
* -u和-g FastCGI使用什么身份(-u 用户 -g 用户组)运行,Ubuntu下可以使用www-data,其他的根据情况配置,如nobody、apache等

重启fcgi

php-cgi: 先杀死进程

sudo killall -HUP php5-cgi

再启动fcgi

4,配置nginx支持php

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

server_name localhost;

修改index的一行修改为:

index index.php index.html index.htm;

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

location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000; #
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
    include /etc/nginx/fastcgi_params;
}

重新启动nginx:

/etc/init.d/nginx stop
/etc/init.d/nginx start

启动fastcgi php:

sudo /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pid

6,安装memcached服务端

sudo apt-get install memcached

  启动memcached服务

memcached -d -m 128 -p 11111 -u root

  参数说明:

-p 监听的端口
-l 连接的IP地址, 默认是本机
-d start 启动memcached服务
-d restart 重起memcached服务
-d stop|shutdown 关闭正在运行的memcached服务
-d install 安装memcached服务
-d uninstall 卸载memcached服务
-u 以的身份运行 (仅在以root运行的时候有效)
-m 最大内存使用,单位MB。默认64MB
-M 内存耗尽时返回错误,而不是删除项
-c 最大同时连接数,默认是1024
-f 块大小增长因子,默认是1.25-n 最小分配空间,key+value+flags默认是48
-h 显示帮助

  查看服务是否启动

ps aux | grep memcached

  

7,安装memcached php扩展

sudo apt-get install php5-memcached

安装完成后,需要重启nginx和fcgi,才能使memcached生效。

参考资料:

http://www.gaojinbo.com/ubuntu12-04-apt-get-install-php5-5%E5%AE%89%E8%A3%85.html

8 安装mariadb

sudo apt-get install mariadb-server
/etc/init.d/mysql start

二,源代码编译方式

1,准备编译环境

apt-get install build-essential

2,下载php源码

cd /usr/local/src
wget http://cn2.php.net/distributions/php-5.5.13.tar.gz
sudo tar -zxvf php-5.5.13.tar.gz
cd php-5.5.13
./configure --prefix=/etc/php-5.5.13 --without-pear --with-config-file-path=/etc/php-5.5.13/etc/ --enable-shared=yes --with-zlib-dir=/usr/local/lib --with-pcre-dir=/usr/local/lib --enable-zip --enable-mysqlnd --with-libxml-dir=/usr/local/lib --with-iconv-dir=/usr/local/lib --enable-sockets --enable-soap --enable-opcache --enable-embedded-mysqli --with-mysqli --with-mysql --with-mcrypt=/usr/local/lib --enable-mbstring --with-mhash --enable-gd-native-ttf --with-freetype-dir=/usr/local/lib --with-png-dir --with-jpeg-dir=/usr/local/lib --with-gd --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --enable-ftp --enable-dba --with-curl
make
make install

初次configure会出现

configure: error: mcrypt.h not found. Please reinstall libmcrypt.

这是由于版权关系,未带mcrypt包,需要先安装:

cd /usr/local/src
wget http://softlayer.dl.sourceforge.net/sourceforge/mcrypt/libmcrypt-2.5.8.tar.gz
tar -zxvf libmcrypt-2.5.8.tar.gz
cd /usr/local/src/libmcrypt-2.5.8
./configure --prefix=/usr/local
make
make install 

其他安装过程缺少相关依赖包的处理可参见:http://zgadzaj.com/how-to-install-php-53-and-52-together-on-ubuntu-1204

如果未安装编译环境,会出现:

root@ubuntu:/usr/local/src/libmcrypt-2.5.8# ./configure --prefix=/usr/local
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for gawk... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
checking whether to enable maintainer-specific portions of Makefiles... no
checking whether make sets $(MAKE)... (cached) yes
checking for g++... no
checking for c++... no
checking for gpp... no
checking for aCC... no
checking for CC... no
checking for cxx... no
checking for cc++... no
checking for cl... no
checking for FCC... no
checking for KCC... no
checking for RCC... no
checking for xlC_r... no
checking for xlC... no
checking for C++ compiler default output file name... configure: error: C++ compiler cannot create executables
See `config.log' for more details.

拷贝php.ini文件:

root@ubuntu:/etc/php-5.5.13/etc# cp /usr/local/src/php-5.5.13/php.ini-development php.ini

开机自启动php-fpm,运行php-fpm:

/usr/local/src/php-5.5.13为源码路径。

root@ubuntu:/usr/local/src/php-5.5.13/sapi/fpm# cp php-fpm /etc/init.d/
root@ubuntu:/etc/php-5.5.13/etc# cp php-fpm.conf.default php-fpm.conf
root@ubuntu:/etc/php-5.5.13/etc# /etc/init.d/php-fpm 
root@ubuntu:/etc/php-5.5.13/etc# ps aux|grep fpm
root      3565  0.0  0.0 130124  4980 ?        Ss   16:25   0:00 php-fpm: master process (/etc/php-5.5.13/etc/php-fpm.conf)
www-data  3566  0.0  0.0 130124  4280 ?        S    16:25   0:00 php-fpm: pool www  
www-data  3567  0.0  0.0 130124  4280 ?        S    16:25   0:00 php-fpm: pool www  
root      3569  0.0  0.0  12256   940 pts/2    S+   16:25   0:00 grep --color=auto fpm

停止fpm,直接kill相关进程即可。

重新编译php,添加了一个--with-openssl选项,但是总不起作用。最后删除了源码,重新解压编译才起作用。

看来是这个原因:

需要重新安装 php ,然而是源代码包中执行 make clean 后,然后configure ,make, make install, 发现还是和上次编译的一样,参数没有变化。 肯定是没有删除掉原来configure 出的Makefile文件,查资料后,发现不能用make clean 要用 make distclean 才可以删除包括Makefile文件在内的文件。 重新编译,好了。

参考资料:

http://asange.blog.51cto.com/7125040/1229976

http://my.oschina.net/ruiorz/blog/280289