虛擬機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(不要把雞蛋放到一個籃子裏)

一般建議,開啓多個實例(可以在不同的機器,或童泰機器上的不同端口)

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