CentOS编译安装PHP开发环境
最近在安装服务器开发环境,踩了不少坑,这里总结下来. yum安装虽然简单,却不灵活,版本也比较老旧不合符设计中的选型,因此只使用yum安装一些依赖库,目标软件采用编译安装.
目录
安装PHP
yum安装依赖库
yum install -y make cmake gcc gcc-c++ autoconf automake libpng-devel libjpeg-devel zlib libxml2-devel ncurses-devel bison \ libtool-ltdl-devel libiconv libmcrypt mhash mcrypt pcre-devel openssl-devel freetype-devel libcurl-devel
安装PHP7.0.9
#先下载PHP cd /tmp wget http://cn2.PHP.net/distributions/PHP-7.0.9.tar.gz tar -zxvf PHP-7.0.9.tar.gz cd PHP-7.0.9.tar.gz #我们先配置下PHP的编译参数 ./configure --prefix=/usr/local/PHP \ --with-MysqL \ --with-MysqLi \ --with-pdo_MysqL \ --with-iconv-dir \ --with-zlib \ --with-libxml-dir \ --enable-xml \ --with-curl \ --enable-fpm \ --enable-mbstring \ --with-gd \ --with-openssl \ --with-mhash \ --enable-sockets \ --with-xmlrpc \ --enable-zip \ --enable-soap \ --with-freetype-dir=/usr/lib64 #编译 make make install clean #复制PHP.ini cp PHP.ini-development /usr/local/PHP/lib/PHP.ini cp /usr/local/PHP/etc/PHP-fpm.conf.default /usr/local/PHP/etc/PHP-fpm.conf #运行PHP-fpm /usr/local/PHP/sbin/PHP-fpm #将PHP命令加入到全局 vi /root/.bash_profile #将/usr/local/PHP/bin 加到后面,用:隔开 PATH=$PATH:$HOME/bin:/usr/local/MysqL/bin:/usr/local/MysqL/lib:/usr/local/PHP/bin #重启 source /root/.bash_profile
PHP-fpm的启动脚本
然后就可以启动,停止重启等操作了:
service PHP-fpm restart
安装PHP扩展
下载扩展
http://pecl.PHP.net
安装
tar -zxvf redis-3.0.1.tgz cd redis-3.0.1 PHPize ./configure --with-PHP-config=/usr/local/PHP/bin/PHP-config make && make install
修改PHP.ini
extension=redis.so
重启PHP-fpm
安装Phalcon框架
phalcon作为PHP的扩展存在,理应可以使用PHPize安装的,但是试过几次,依赖的东东比较难搞,还是使用官方给出的安装器:
编译安装
git clone git://github.com/phalcon/cphalcon.git cd cphalcon/build sudo ./install
其中遇到gcc报错,是因为虚拟机内存太小,后来加了一个swap分区解决了.
修改PHP.ini
extension=phalcon.so
安装Nginx
安装Nginx的依赖包
Nginx 依赖于 zlib pcre ssl 三个模块,安装之前要先安装它们
cd /lamp wget http://zlib.net/zlib-1.2.8.tar.gz tar -zxvf zlib-1.2.7.tar.gz cd zlib-1.2.7 ./configure make make install wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.32.tar.gz tar -zxvf pcre-8.21.tar.gz cd pcre-8.21 ./configure make make install wget http://www.openssl.org/source/openssl-1.0.2.tar.gz tar zxvf openssl-1.0.2.tar.gz cd openssl-1.0.2.tar.gz ./config # 注意是config,不是configure make make install
安装Nginx
# 下载 wget http://Nginx.org/download/Nginx-1.11.3.tar.gz tar -zxvf Nginx-1.11.3.tar.gz cd Nginx-1.11.3.tar.gz # 编译 # 这3个扩展的目录是他们的源代码目录,不是安装目录,这点很容易搞错 ./configure --prefix=/usr/local/Nginx \ --sbin-path=/usr/local/Nginx/Nginx \ --conf-path=/usr/local/Nginx/Nginx.conf \ --pid-path=/usr/local/Nginx/Nginx.pid \ --with-http_ssl_module \ --with-pcre=/lamp/pcre-8.32 \ --with-zlib=/lamp/zlib-1.2.7 \ --with-openssl=/lamp/openssl-1.0.2 make && make install #启动 /usr/local/Nginx/Nginx #查看端口 netstat -tnl|grep 8080 #访问 curl http://localhost:8080
开机自启动
开机启动的配置文件是:/etc/rc.local ,vi加入 /usr/local/Nginx/Nginx 即可
#!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local /usr/local/apache/bin/apachectl start /usr/local/bin/redis-server /etc/redis.conf /usr/local/PHP/sbin/PHP-fpm /usr/local/Nginx/Nginx
开启Nginx的iptables限制
我们在本地访问127.0.0.1:8080/index.PHP,是可以打开的。 但是如果,在另外一台机子上访问:http://192.168.155.128:8080/index.PHP 不能访问,可能是这个8080端口号没有加入到iptables的白名单,所以需要加一下:
(PS: 如果你的Nginx端口号是80,应该是已经在iptables白名单中了,如果能访问就不需要加了)
iptables的配置文件在这:/etc/sysconfig/iptables
我们vi 打开下,然后在倒数第二行上面加入:-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
然后,重启下 iptables
service iptables restart
安装MysqL
cd /tmp #先下载MysqL http://dev.MysqL.com/Downloads/MysqL/MysqL-5.7.14.tar.gz tar zxvf MysqL-5.7.14.tar.gz cd MysqL-5.7.14 #cmake配置下 cmake \ -DCMAKE_INSTALL_PREFIX=/usr/local/MysqL \ #安装目录 -DMysqL_DATADIR=/usr/local/MysqL/data \ #数据库存放目录 -DDEFAULT_CHARSET=utf8 \ #使用utf8字符 -DDEFAULT_COLLATION=utf8_general_ci \ #校验字符 -DEXTRA_CHARSETS=all \ #安装所有扩展字符集 -DENABLED_LOCAL_INFILE=1 #允许从本地导入数据 #编译安装 make && make install #创建MysqL用户和用户组 groupadd MysqL useradd -r -g MysqL MysqL #给MysqL目录设置目录权限 chown -R MysqL:MysqL /usr/local/MysqL #将MysqL的启动服务添加到系统服务中 cd /usr/local/MysqL/ cp support-files/my-default.cnf /etc/my.cnf #创建系统数据库的表 cd scripts/ ./MysqL_install_db --user=MysqL --basedir=/usr/local/MysqL --datadir=/usr/local/MysqL/data/ #复制MysqL管理脚本到系统服务目录 cd /opt/MysqL/support-files cp MysqL.server /etc/rc.d/init.d/MysqL #添加MysqL命令到系统服务命令 chkconfig --add MysqL #加入开机启动策略 chkconfig MysqL on service MysqL start #以后就可以调用service 命令来管理MysqL service MysqL start service MysqL stop service MysqL restart #将MysqL命令加入全局可用 vi /root/.bash_profile #在PATH=$PATH:$HOME/bin添加参数为: PATH=$PATH:$HOME/bin:/usr/local/MysqL/bin:/usr/local/MysqL/lib #重新生效 source /root/.bash_profile #修改root密码 MysqL -u root MysqL MysqL>use MysqL; MysqL>desc user; MysqL> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "root"; //为root添加远程连接的能力。 MysqL>update user set Password = password('12346') where User='root'; MysqL>select Host,User,Password from user where User='root'; MysqL>flush privileges; MysqL>exit #重新登录: MysqL -uroot -p123456
安装MongoDB
下载解压
wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.6.4.tgz tar -zxvf mongodb-linux-x86_64-2.6.4.tgz -C /usr/src cd mongodb-linux-x86_64-2.6.4
创建数据库和日志的目录
mkdir log mkdir db
以后台方式启动
./bin/mongod --dbpath=./db --logpath=./log/mongodb.log --fork --auth
设置开机启动
echo "/usr/src/mongodb-linux-x86_64-2.6.4/bin/mongod --dbpath=/usr/src/mongodb-linux-x86_64-2.6.4/db --logpath=/usr/src/mongodb-linux-x86_64-2.6.4/log/mongodb.log --fork --auth" >> /etc/rc.local
查看端口
netstat -nalupt | grep mongo
安装Redis
安装
#下载解压 wget http://download.redis.io/releases/redis-3.0.3.tar.gz tar xzf redis-3.0.3.tar.gz cd redis-3.0.3 #编译 make make install
修改redis.conf
daemonize yes loglevel notice logfile /var/log/redis.log dir ./
设置系统的overcommit_memory,vi /etc/sysctl.conf
vm.overcommit_memory = 1
执行:
sysctl vm.overcommit_memory=1
添加启动脚本
#!/bin/sh # # redis Startup script for Redis Server # # chkconfig: - 90 10 # description: Redis is an open source,advanced key-value store. # # processname: redis-server # config: /etc/redis.conf # pidfile: /var/run/redis.pid REDISPORT=6379 EXEC=/usr/local/bin/redis-server REDIS_CLI=/usr/local/bin/redis-cli PIDFILE=/var/run/redis.pid CONF="/usr/src/redis-3.0.3/redis.conf" case "$1" in start) if [ -f $PIDFILE ] then echo "$PIDFILE exists,process is already running or crashed" else echo "Starting Redis server..." $EXEC $CONF fi if [ "$?"="0" ] then echo "Redis is running..." fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist,process is not running" else PID=$(cat $PIDFILE) echo "Stopping ..." $REDIS_CLI -p $REDISPORT SHUTDOWN while [ -x ${PIDFILE} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi ;; restart|force-reload) ${0} stop ${0} start ;; *) echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2 exit 1 esac
chmod +x /etc/init.d/redis chkconfig --add redis # 加入开机启动 chkconfig redis on