虚拟机CentOS搭建PHP开发环境以及安装Memcached

虚拟机CentOS搭建PHP开发环境


1. 开启上网功能

命令:cd /etc/sysconfig/network-scripts/
命令:ls #查看目录下所有文件
有一个ifcfg-******
命令:vi ifcfg-****打开
按i进入编辑状态
将onboot的no改为yes
按esc
按shift + z(z快速按两下)退出保存
命令:service network restart #重启

2.安装wget和ifconfig

命令:yum -y install wget
命令:yum install net-tools

3.更换镜像为163

命令:cd /etc/yum.repos.d/
命令:wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
命令:yum clean all
命令:yum makecache

4.关闭防火墙

命令:systemctl stop firewalld.service #停止firewall
命令:systemctl disable firewalld.service #禁止firewall开机启动
命令:firewall-cmd --state #查看默认防火墙状态(关闭后显示not running,开启后显示running)

5.安装mysql

命令:yum install mysql-server mysql
说明:CentOD7中mysql被替换为mariadb
命令:yum install mariadb-server -y #安装
命令:systemctl start mariadb.service #启动
命令:systemctl enable mariadb.service #设置开机启动
命令:systemctl stop mariadb.service #停止(可以不做)
命令:systemctl restart mariadb.service #重启(可以不做)
命令:cp /usr/share/mysql/my-huge.cnf /etc/my.cnf #拷贝配置文件
命令:mysql #进入mysql命令行
说明:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.44-MariaDB MariaDB Server
Copyright © 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
MariaDB [(none)]>

命令:exit;#退出mysql命令行

5.1修改mysql密码

mysql_secure_installation
根据提示一路输入y

6.安装Apache

命令:yum install httpd #根据提示,输入Y安装即可成功安装
命令:systemctl start httpd.service #启动apache
命令:systemctl enable httpd.service #设置apache开机启动
命令:systemctl stop httpd.service #停止apache(可以不做)
命令:systemctl restart httpd.service #重启apache(可以不做)

7.安装php

yum install php #根据市输入y直到安装完成
安装PHP组件,是php支持MariaDB
yum install php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mhash
#根据提示输入y回车
systemctl restart mariadb.service #重启MariaDB
systemctl restart httpd.service #重启apache

8.升级php为5.6

命令:php -v #进入终端后查看php版本
输出结果可能如下:
PHP 5.4.35 (cli) (built: Nov 14 2014 07:04:10) Copyright © 1997-2014 The PHP Group Zend Engine v2.6.0, Copyright © 1998-2015 Zend Technologies with Zend OPcache v7.0.4-dev, Copyright © 1999-2015, by Zend Technologies
执行下面的命令升级软件仓库
命令:rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
命令:rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
命令:yum remove php-common #执行下面的命令删除php
命令:yum install -y php56w php56w-opcache php56w-xml php56w-mcrypt php56w-gd php56w-devel php56w-mysql php56w-intl php56w-mbstring #安装php 5.6版本(php56w-devel这个不是必需的)
命令:service httpd restart #重启httpd
命令:php -v
现在应该是5.6了!
8.1安装PHP FPM
命令:yum install php56w-fpm

9.安装ssh

yum install openssh-server

10.安装图形界面(最好不安装)

命令:yum -y groupinstall “GNOME Desktop”
命令:sudo ln -sf /lib/systemd/system/runlevel5.target /etc/systemd/system/default.target
重启虚拟机,应该就进入图形界面了.
一般不需要安装图形界面
可以通过ctrl+alt+f1/f2进行图形界面和终端界面的切换

12.测试

12.1 FileZilla连接CentOS

给Windows安装FileZilla
连接到Linux虚拟机
将php项目拖入/var/www/html/下
在Window浏览器中进行访问即可

补充:

错误1: wget: unable to resolve host address “mirrors.163.com” 的解决办法
进入/etc/resolv.conf。
修改内容为下
nameserver 8.8.8.8 #google域名服务器
nameserver 8.8.4.4 #google域名服务器
错误2:目录无法自动生成!请手动生成目录!
修改对应文件夹和其子文件权限为777
如果没有解决,再执行命令 setenforce 0

安装telnet

  1. CentOS7.0 telnet-server 启动的问题。
    解决方法:
    ①、先检查CentOS7.0是否已经安装以下两个安装包:telnet-server、xinetd。命令如下:
    rpm -qa telnet-server
    rpm -qa xinetd
    如果没有安装,则先安装。安装命令:
    [root@master ~]# yum list |grep telnet
    telnet-server.x86_64 1:0.17-59.el7 @base
    telnet.x86_64 1:0.17-59.el7 base
    [root@master ~]# yum install telnet-server.x86_64
    [root@master ~]# yum install telnet.x86_64
    [root@master ~]# yum list |grep xinetd
    xinetd.x86_64 2:2.3.15-12.el7 @base
    [root@master ~]# yum install xinetd.x86_64

安装完成后,将xinetd服务加入开机自启动:
systemctl enable xinetd.service
将telnet服务加入开机自启动:
systemctl enable telnet.socket
最后,启动以上两个服务即可:
systemctl start telnet.socket
systemctl start xinetd(或service xinetd start)


Linux Memcached 安装

yum install memcached
/usr/bin/memcached -p 11211 -m 64m -vv -u root
2倍增长因子(大 中 小)
/usr/bin/memcached -p 11211 -m 64m -vv -f 2 -u root
列举缓存中所有的key
命令:stats item
stat item:7 number 1
命令:stats cachedump 7 0
所有的key
关闭端口
ps -ef|grep memcached
ps aux|grep memcached
kill -9 id号

作为后台服务进程
/usr/bin/memcached -p 11211 -m 64m -d -u root

连接Memcached
参考菜鸟教程
安装memcache (http://pecl.php.net/package/memcache)
wget http://pecl.php.net/get/memcache-3.0.8.tgz
tar -zxvf memcache-3.0.8.tgz
cd memcache-3.0.8
/usr/bin/phpize
./configure --with-php-config=/usr/bin/php-config
make
make install
提示/usr/lib64/php/modules/则成功,这个地址需要记住,后面会用到
打开php.ini进行相关设置
vi /etc/php.ini #编辑
最下面添加
[Memcache]
extension_dir = “/usrb64/php/modules/”
extension = memcache.so
重启服务器 systemctl restart httpd.service
客户端测试

<?php $mem = new Memcache; $mem->connect("127.0.0.1",11211); var_dump($mem); $mem->set('a', 'hello'); $result = $mem->get('a'); echo $result; ?>

应该会出现hello.如果没有,可以尝试setenforce 0再运行
CentOS中遇到zlib.h: No such file or directory
yum install zlib-devel
出现错误configure: error: no acceptable C compiler found in $PATH
安装GCC软件套件
yum install gcc

11.配置
11.1 Apache配置

vi /etc/httpd/conf/httpd.conf #编辑文件
ServerSignature On #添加,在错误页中显示Apache的版本,Off为不显示
Options Indexes FollowSymLinks #修改为:Options Includes ExecCGI FollowSymLinks(允许服务器执行CGI及SSI,禁止列出目录)
#AddHandler cgi-script .cgi #修改为:AddHandler cgi-script .cgi .pl (允许扩展名为.pl的CGI脚本运行)
AllowOverride None  #修改为:AllowOverride All (允许.htaccess)
AddDefaultCharset UTF-8 #修改为:AddDefaultCharset GB2312 (添加GB2312为默认编码)
#Options Indexes FollowSymLinks #修改为 Options FollowSymLinks(不在浏览器上显示树状目录结构)
DirectoryIndex index.html #修改为:DirectoryIndex index.html index.htm Default.html Default.htm index.php(设置默认首页文件,增加index.php)
MaxKeepAliveRequests 500 #添加MaxKeepAliveRequests 500 (增加同时连接数)
:wq! #保存退出
systemctl restart httpd.service #重启apache
rm -f /etc/httpd/conf.d/welcome.conf /var/www/error/noindex.html #删除默认测试页

11.2 php配置

vi /etc/php.ini #编辑
date.timezone = PRC #把前面的分号去掉,改为date.timezone = PRC
disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname
#列出PHP可以禁用的函数,如果某些程序需要用到这个函数,可以删除,取消禁用。
expose_php = Off #禁止显示php版本的信息
short_open_tag = ON #支持php短标签
open_basedir = .:/tmp/ #设置表示允许访问当前目录(即PHP脚本文件所在之目录)和/tmp/目录,可以防止php木马跨站,如果改了之后安装程序有问题(例如:织梦内容管理系统),可以注销此行,或者直接写上程序的目录/data/www.osyunwei.com/:/tmp/
:wq! #保存退出
systemctl restart mariadb.service #重启MariaDB
systemctl restart httpd.service #重启apache


Memcached的过期数据删除机制


  1. 当某个值过期后,并没有从内存删除,因此stats统计时,curr_item有相关信息
    2.当某个新值去占用他的位置时,当成空chunk来占用
    3.当get值时,判断是否过期,如果过期,返回空,并且清空,curr_item减少
    即这个过期,只是让用户看不到这个数据而已,并没有在过期的瞬间从内存中删除数据
    这个称谓lazy expiration 惰性失效
    好处 – 节省cpu时间和检测的成本
  2. 如果chunk都满了,又有新的值要加入,要挤掉谁?
    Memcached此处用的LRU删除机制
    (操作系统的内存管理 常用FIFO,LRU删除)
    LRU:Least Recently Used 最近最少使用
    FIFO:first in,first out
    原理:当某个单元被请求时,维护一个计数器,通过计数器来判断最近谁最少被使用就把谁T除在这里插入图片描述
    注意:
    即使某个key是设置的永久有效期耶一样哈UI被提出来,即来数据被踢现象

Memcached中的一些参数限制


Key的长度:250字节(二进制协议支持65536个字节)

Value的限制:1M,一般都是存储一些文本,如新闻列表等,这个值足够了

内存限制:32位下最大设置到2G

如果有30G数据要缓存,一般也不会单实例装30G(不要把鸡蛋放到一个篮子里)

一般建议,开启多个实例(可以在不同的机器,或童泰机器上的不同端口)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章