oksvip

相信作为PHP开发者对于linux都不会陌生,linux是开发者必备的技能之一。现在很多公司招聘的时候都会在招聘要求上写上会linux下开发,那么在linux下开发首先我们要有php的运行环境,对于一个没有接触过linux的人来说在linux下搭建php运行环境可不是一般的复杂,本身在linux下安装软件就不是一件简单的事,就算是熟悉linux软件安装的开发者都需要借助百度才能配置完全。
所以经过博主的多次安装、测试、踩坑后总结出一整套linux下安装php开发环境的教程,博主目前使用的环境就是这个环境,今天将教程贡献给大家,后面的坑大家一起踩_。再次声明根据此套教程安装的linux环境可直接运用于开发,可直接应用于开发,可直接应用于开发。重要的事情说三遍!
此文章属于日出博客原创,在无利益关系的情况下可自由传播,转载请注明出处,谢谢!

一、准备篇

1、下载安装包

LINUX
Apache2.4.25:
最新地址:http://httpd.apache.org/download.cgi
下载地址:http://apache.fayea.com/httpd/httpd-2.4.25.tar.gz

Mysql5.7.17:
最新地址:https://dev.mysql.com/downloads/mysql/
下载地址:https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz

php7.1:
最新地址:http://php.net/downloads.php
下载地址:http://cn2.php.net/distributions/php-7.1.2.tar.gz

phpMyAdmin4.7.0:
最新地址:https://www.phpmyadmin.net/
下载地址:https://files.phpmyadmin.net/phpMyAdmin/4.7.0-rc1/phpMyAdmin-4.7.0-rc1-all-languages.zip

Git
最新地址:https://github.com/git/git
下载链接:https://github.com/git/git/archive/master.zip

2、安装依赖包

# 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 libaio pcre python python-devel openssl-devel kernel-devel freetype-devel libcurl-devel

3、删除自带apache

# rpm -qa httpd
# rpm -qa | grep httpd
httpd-2.2.15-29.el6.centos.i686
# rpm -e --nodeps httpd-2.2.15-29.el6.centos.i686

4、卸载自带的mysql

# rpm -qa mysql
# rpm -qa | grep mysql
mysql-libs-5.1.71-1.el6.i686
# rpm -e --nodeps mysql-libs-5.1.71-1.el6.i686

5、开启80端口、3306端口

在22端口这条规则的下面加上
# vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
# /etc/init.d/iptables restart 或者 # service iptables restart

6、关闭SELINUX

# vi /etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
# shutdown -r now#重启系统

7、解压压缩包

# cd /usr/local/soft
# tar -xzvf apr-1.5.2.tar.gz
# tar -xzvf apr-util-1.5.4.tar.gz
# tar -xzvf httpd-2.4.25.tar.gz

8、secureCRT安装上传下载插件

# yum install lrzsz
# rz    //上传文件
# sz    //下载文件

9、安装vim并设置代码高亮

1)下载vim
# rpm -qa|grep vim
# yum -y install vim-enhanced
# yum -y install vim-*
2)设置vim编程风格
# vim /etc/vimrc
set nu 		// 这是设置显示行号
set showmode	//设置在命令行界面最下面显示当前模式等。
set autoindent 	// 设置每次单击Enter键后,光标移动到下一行时与上一行的起始字符对齐
set ts=4  		//注:ts是tabstop的缩写,设TAB宽4个空格)
set expandtab
syntax on 		// 即设置语法检测,当编辑C或者Shell脚本时,关键字会用特殊颜色显示

ln -sf /usr/bin/vim /bin/vi

//设置vi命令高亮

对于已保存的文件,可以使用下面的方法进行空格和TAB的替换:
TAB替换为空格:
:set ts=4
:set expandtab
:%retab!

空格替换为TAB:
:set ts=4
:set noexpandtab
:%retab!
加!是用于处理非空白字符之后的TAB,即所有的TAB,若不加!,则只处理行首的TAB。

10、安装sshd服务配置secureCRT远程登录

# yum install openssh-server
# service sshd start
# service sshd status

11、centos6.5修改主机名

# hostname	//显示当前主机名
# vi /etc/sysconfig/network
HOSTNAME=centos
# vi /etc/hosts
在第一行127.0.0.1 centos localhost localhost.localdomain localhost4 localhost4.localdomain4
# hostname centos
# exit

12、CentOS6 配置FTP服务器

1)先检查有没有安装
# rpm -q vsftpd 	//先检查有没有安装vsftpd
# yum install vsftpd   	//安装vsftpd
2)开放21端口
# service iptables stop
# vi /etc/sysconfig/iptables
# 添加规则允许21端口通行
-A INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT
# 防火墙配置开放:
-A INPUT -p tcp --dport 30000:30100 -j ACCEPT
# service iptables restart
3)编辑vsftpd.config文件
# 备份 etc/vsftpd/vsftpd.conf
# cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.bak
# vi /etc/vsftpd/vsftpd.conf
anonymous_enable=NO		//设定不允许匿名用户访问。
# 为了让FLASHFXP之类的软件更好的连接服务器,得让VSFTPD支持被动模式才行,上面已经开通相应端口防火墙,在最后加入
pasv_enable=YES
pasv_max_port=30100
pasv_min_port=30000
(上面的30000--30100端口号可以是其它的,在此举例)

# 指定此用户的网站文件夹
# adduser -d /usr/local/httpd/htdocs -g ftp -s /sbin/nologin test
//-s /sbin/nologin是让其不能登陆系统,-d 是指定用户目录为/usr/local/httpd/htdocs ,即该账户只能登陆ftp,却不能用做登陆系统用。-g 是指定为FTP用户组
# passwd test
Changing password for user beinan.//接下来会出现让你设置新的密码
有必要的话  设置TEST目录权限
修改根目录属性:
# chmod -R 777 /usr/local/httpd/htdocs 	//递归地给此目录下所有文件和子目录的读、写、执行权限
# chgrp -R ftp /usr/local/httpd/htdocs 	//递归地把此目录及该目录下所有文件和子目录的组属性设置成ftp组
4)限制用户目录,不得改变目录到上级
# vi /etc/vsftpd/vsftpd.conf 
将这两行
#chroot_list_enable=YES
#chroot_list_file=/etc/vsftpd/chroot_list
注释去掉

新增一个文件: 
# vi /etc/vsftpd/chroot_list 
内容写需要限制的用户名:
test
重新启动vsftpd
# service vsftpd restart
5)设置开机启动
# vi /etc/rc.local
# 在最后一行添加:
# service vsftpd start

二、安装篇:

1、编译安装apache

1)安装apr
# cd apr-1.5.2
# ./configure --prefix=/usr/local/apr
# make && make install
2)安装apr-util
# cd ../apr-util-1.5.4
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
# make && make install
3)安装apache
# cd ../httpd-2.4.25
# ./configure --prefix=/usr/local/httpd --sysconfdir=/usr/local/httpd/conf --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mpms-shared=all --with-mpm=event
# make && make install
4)备份httpd.conf
# cd /usr/local/httpd/conf
# cp httpd.conf httpd.conf.bak
5)修改httpd的主配置文件
# vi /usr/local/httpd/conf/httpd.conf
将ServerName www.example.com:80修改为localhost:80
6)提供SysV服务脚本/etc/rc.d/init.d/httpd
#!/bin/bash  
#  
# httpd        Startup script for the Apache HTTP Server  
#  
# chkconfig: - 85 15  
# description: The Apache HTTP Server is an efficient and extensible  \  
#          server implementing the current HTTP standards.  
# processname: httpd  
# config: /usr/local/httpd/conf/httpd.conf  
# config: /etc/sysconfig/httpd  
# pidfile: /var/run/httpd/httpd.pid  
#  
### BEGIN INIT INFO  
# Provides: httpd  
# Required-Start: $local_fs $remote_fs $network $named  
# Required-Stop: $local_fs $remote_fs $network  
# Should-Start: distcache  
# Short-Description: start and stop Apache HTTP Server  
# Description: The Apache HTTP Server is an extensible server   
#  implementing the current HTTP standards.  
### END INIT INFO  
  
# Source function library.  
. /etc/rc.d/init.d/functions  
  
if [ -f /etc/sysconfig/httpd ]; then  
        . /etc/sysconfig/httpd  
fi  
  
# Start httpd in the C locale by default.  
HTTPD_LANG=${HTTPD_LANG-"C"}  
  
# This will prevent initlog from swallowing up a pass-phrase prompt if  
# mod_ssl needs a pass-phrase from the user.  
INITLOG_ARGS=""  
  
# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server  
# with the thread-based "worker" MPM; BE WARNED that some modules may not  
# work correctly with a thread-based MPM; notably PHP will refuse to start.  
  
# Path to the apachectl script, server binary, and short-form for messages.  
apachectl=/usr/local/httpd/bin/apachectl  
httpd=${HTTPD-/usr/local/httpd/bin/httpd}  
prog=httpd  
pidfile=${PIDFILE-/usr/local/httpd/logs/httpd.pid}  
lockfile=${LOCKFILE-/var/lock/subsys/httpd}  
RETVAL=0  
STOP_TIMEOUT=${STOP_TIMEOUT-10}  
  
# The semantics of these two functions differ from the way apachectl does  
# things -- attempting to start while running is a failure, and shutdown  
# when not running is also a failure.  So we just do it the way init scripts  
# are expected to behave here.  
start() {  
        echo -n $"Starting $prog: "  
        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS  
        RETVAL=$?  
        echo  
        [ $RETVAL = 0 ] && touch ${lockfile}  
        return $RETVAL  
}  
  
# When stopping httpd, a delay (of default 10 second) is required  
# before SIGKILLing the httpd parent; this gives enough time for the  
# httpd parent to SIGKILL any errant children.  
stop() {  
    echo -n $"Stopping $prog: "  
    killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd  
    RETVAL=$?  
    echo  
    [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}  
}  
reload() {  
    echo -n $"Reloading $prog: "  
    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then  
        RETVAL=6  
        echo $"not reloading due to configuration syntax error"  
        failure $"not reloading $httpd due to configuration syntax error"  
    else  
        # Force LSB behaviour from killproc  
        LSB=1 killproc -p ${pidfile} $httpd -HUP  
        RETVAL=$?  
        if [ $RETVAL -eq 7 ]; then  
            failure $"httpd shutdown"  
        fi  
    fi  
    echo  
}  
  
# See how we were called.  
case "$1" in  
  start)  
    start  
    ;;  
  stop)  
    stop  
    ;;  
  status)  
        status -p ${pidfile} $httpd  
    RETVAL=$?  
    ;;  
  restart)  
    stop  
    start  
    ;;  
  condrestart|try-restart)  
    if status -p ${pidfile} $httpd >&/dev/null; then  
        stop  
        start  
    fi  
    ;;  
  force-reload|reload)  
        reload  
    ;;  
  graceful|help|configtest|fullstatus)  
    $apachectl $@  
    RETVAL=$?  
    ;;  
  *)  
    echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"  
    RETVAL=2  
esac  
  
exit $RETVAL
7)为此脚本赋予执行权限:
# chmod +x /etc/rc.d/init.d/httpd
8)加入服务列表
# chkconfig --add httpd
9)启动Apache服务
# service httpd start
# curl http://127.0.0.1

2、编译安装mysql

1)解压缩到/usr/local/下面,mysql的主目录命名为mysql
# cd /usr/local/soft/
# tar -zvxf mysql-5.7.17-linux-glibc2.5-i686.tar.gz  /usr/local
# cd ../
# mv mysql-5.7.17-linux-glibc2.5-i686/ mysql
# cd mysql
2)在mysql下面创建data数据库文件目录
# mkdir data
3)创建mysql的用户组和用户,并对mysql目录设置用户组和用户
# groupadd mysql
# useradd mysql -g mysql
# chown -R mysql .
# chgrp -R mysql .
4)初始化mysql并启动mysql服务
# cd /usr/local/mysql/bin
# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data   //指定mysql基本目录和数据目录文件夹
2016-01-09 12:00:28 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2016-01-09 12:00:33 [WARNING] The bootstrap log isn\'t empty:
2016-01-09 12:00:33 [WARNING] 2016-01-09T04:00:29.262989Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead
2016-01-09T04:00:29.264643Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
2016-01-09T04:00:29.264653Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000)
5)启动mysql服务
# cd /usr/local/mysql/support-files/
# ./mysql.server start
Starting MySQL. SUCCESS! 
6)将mysql服务添加至系统服务中
# cp -v /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
# chkconfig --add mysql
# service mysql {start|stop|restart|reload|force-reload|status}
7)将mysql命令加入linux环境变量
# ln -s /usr/local/mysql/bin/mysql /bin
8)查看root随机密码
# cat /root/.mysql_secret
# Password set for user \'root@localhost\' at 2016-01-09 12:00:28 
:5ul#H6dmcwX
9)登录mysql
# mysql -h localhost -u root -p
或者
# cd /usr/local/mysql/bin
# ./mysql -u root -p
10)改mysql的root用户密码为root
mysql> SET PASSWORD=PASSWORD(\'root\');
11)设定远程登录mysql
mysql> USE mysql;
mysql> SELECT Host,User FROM user;
mysql> GRANT ALL PRIVILEGES ON *.* TO root@\'%\' IDENTIFIED BY \'root\' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
12)设定默认utf8字符集
# cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
# vi /etc/my.cnf
修改
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character-set-server = utf8
13)解决navicat for mysql插入数据后显示错误
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

3、编译安装php

1)编译安装php
# tar xf php-7.1.10.tar.gz
# cd php-7.1.10
# ./configure --prefix=/usr/local/php --with-libxml-dir=/usr/include/libxml2 --with-mysqli --with-zlib-dir=/usr/src/kernels/2.6.32-431.el6.i686/include/config/zlib --with-apxs2=/usr/local/httpd/bin/apxs --with-config-file-path=/usr/local/php
# make && make install
# cp /usr/local/soft/php-7.1.1/php.ini-production /usr/local/php/php.ini
##将安装目录下的php.ini-production复制到/usr/local/php/conf下作为配置文件
2)备份php.ini
# cd /usr/local/php/conf
# cp php.ini php.ini.bak
3)配置Apache,使其和Php结合
# vi /usr/local/httpd/conf/httpd.conf
#添加index.php网页为默认访问页:
DirectoryIndex index.php index.html
#使apache与扩展名为.php的文件类型相关联
在/usr/local/httpd/conf/httpd.conf文件中添加一句:
AddType application/x-httpd-php .php
4)将php命令添加至环境变量
# export PATH=$PATH:/usr/local/php/bin
5)重启Apache服务,并添加php和mysql测试网页
# service httpd restart
# cd /usr/local/httpd/htdocs
# vi index.php
#写如下代码
$link = mysqli_connect(\'localhost\', \'root\', \'root\');
if ($link == true) {
    echo \'ok\';
} else {
    echo \'no\';
}
phpinfo();

4、安装GIT

1)安装依赖需求
# yum install curl curl-devel perl cpio expat-devel gettext-devel openssl-devel zlib-devel perl-ExtUtils-MakeMaker
2)卸载Centos自带的git1.7.1
# rpm -qa git
git-1.7.1-4.el6_7.1.i686
# rpm -e --nodeps git-1.7.1-4.el6_7.1.i686
或者
# yum remove git
3)安装git并将git添加到环境变量中
# cd /usr/local/soft
# wget https://github.com/git/git/archive/v2.12.0-rc1.tar.gz
# tar -xzvf git-2.1.2.tar.gz
# cd git-2.1.2
# make prefix=/usr/local/git all
# make prefix=/usr/local/git install
# echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/bashrc
# source /etc/bashrc
4)查看版本号
# git --version
git version 2.1.2

5、安装redis和redis拓展

php全部拓展下载: http://pecl.php.net/package-stats.php
redis下载地址:   https://github.com/MSOpenTech/redis
php7拓展下载地址:http://pecl.php.net/package/redis/3.1.1/windows
1)安装redis依赖文件
# yum install tcl
2)下载redis安装包并解压
# tar -xzvf redis-4.0-rc2.tar.gz
# mv redis-4.0-rc2 redis
# cd redis
# make && make install
3)成功后/usr/local/bin下已有这些文件
redis-benchmark  redis-check-rdb  redis-sentinel
redis-check-aof  redis-cli        redis-server
# redis-server –v (查看版本命令)
4)修改配置文件redis.conf

创建配置文件目录,dump file 目录,进程pid目录,log目录等

# cd /usr/local/
# mkdir redis
# cd redis
# mkdir conf data log run
# cp /usr/local/soft/redis/redis.conf /usr/local/redis/conf/redis_6379.conf
# cd /usr/local/redis/conf
# cp redis_6379.conf redis_6379.conf.bak
5)修改Redis配置
# vi redis_6379.conf
port 6379	//默认端口
pidfile /usr/local/redis/run/redis_6379.pid	//pid目录
dir /usr/local/redis/data	        //dump目录
logfile /usr/local/redis/log/redis.log	//log存储目录
daemonize no改为yes	//修改配置文件使得redis在background运行
持久化	//默认rdb,可选择是否开启aof,若开启,修改配置文件appendonly
6)启动redis,查看各目录下文件
# redis-server /usr/local/redis/conf/redis_6379.conf
7)客户端连接redis
# redis-cli
8)服务及开机自启动

复制启动脚本

# cp /usr/local/soft/redis/utils/redis_init_script /etc/init.d/redis
# cd /etc/init.d
# vi /etc/init.d/redis
PIDFILE=/usr/local/redis/run/redis_6379.pid
CONF="/usr/local/redis/conf/redis_6379.conf"
// 给reids服务设置优先级(在开始位置添加)
#!/bin/sh
#
# added by wt on 20170312
# chkconfig: 2345 90 10
#
# description: Redis is a persistent key-value database
# added by wt on 20170312



# chmod +x /etc/init.d/redis	//给启动脚本添加权限
# chmod –x /etc/init.d/redis	//删除权限

//添加开机启动服务
# chkconfig redis on
9)启动、关闭redis
# service redis start
# service redis stop
10) 安装php拓展
# wget https://codeload.github.com/phpredis/phpredis/zip/php7
# unzip phpredis-php7.zip
# cd phpredis-php7
# /usr/local/php/bin/phpize
# ./configure --with-php-config=/usr/local/php/bin/php-config
# make && make install
11) 将extension=redis.so加入到php.ini
# vim /usr/local/php/php.ini
extension=redis.so
# service httpd restart
12) 测试redis是否成功
<?php
$redis = new Redis();
$redis->connect(\'127.0.0.1\', 6379);
$redis->set(\'name\', \'wt\');
echo $redis->get(\'name\');

分类:

技术点:

相关文章:

  • 2021-04-10
  • 2021-12-12
  • 2021-09-12
  • 2021-11-01
  • 2021-08-05
  • 2021-08-09
猜你喜欢
  • 2021-12-22
  • 2021-10-02
  • 2021-08-25
  • 2021-09-05
  • 2021-11-09
  • 2022-02-17
相关资源
相似解决方案