云开发环境

本文于 2268 天之前发表,文中内容可能已经过时。

一直为开发环境的选择烦恼着,以前使用的是虚拟机。配置lnmp开发环境,其实如果只是在一台机子上折腾,虚拟机的开发环境是很好的。由于开发的时候还涉及到回家调试的问题,自己的电脑又不想天天携带者,所以以前的虚拟机步骤很少痛苦。那时候就在想,什么时候能有云虚拟主机。
自己上传vm文件到git的形式尝试过,文件太大了,更新起来很麻烦的。
前一段时间阿里云促销,所以直接买了三年的服务器。有一个想法就是布置一个云开发环境。
好处: 使用阿里云的镜像备份可以方便的管理自己版本的镜像,这个速度很快,推荐推荐!有网的地方就可以进行调试。

环境搭建

lnmp搭建

使用的系统centos6.9,貌似centos6用的人比较多。
lnmp搭建有很多方法,比较推荐使用源码的方式进行安装,软件都不是太大,编译的速度都挺快的。编译安装对于软件和配置文件的摆放都可以随心,这一点挺好的。软件统一放到/usr/src目录下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
cd /usr/src
wget http://am1.php.net/distributions/php-5.6.30.tar.gz
tar zxvf php-5.6.30.tar.gz
cd php-5.6.30

依赖安装
yum -y install gd-devel pcre-devel libxml2-devel libjpeg-devel libpng-devel libevent-devel libtool* autoconf* freetype* libstd* gcc44* ncurse* bison* openssl* libcurl* libcurl* libmcrypt*

根据所需安装扩展
./configure --prefix=/usr/local/php \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-sysvshm \
--enable-sysvmsg \
--enable-inline-optimization \
--with-curl \
--with-mcrypt \
--enable-mbregex \
--enable-fpm \
--enable-mbstring \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--enable-ftp \
--without-pear \
--enable-opcache

make && make install

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
cp ./php.ini-development /usr/local/php/lib/php.ini

配置信息
short_open_tag = On
output_buffering = 4096
max_execution_time = 60
# 错误显示和级别
error_reporting = E_ALL
display_errors = On
display_startup_errors = On
log_errors = On
# 把PHP报错记录到syslog中或指定的文件中(注意目录权限)
error_log = syslog
# 设置默认时区
date.timezone = PRC

php-fpm 默认就有安装了
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

[global]
# 重要,一定要记录notice, 及时发现异常情况
log_level = notice
# 重要,每个子进程能打开的文件描述符数量上限(系统默认是1024),一定要尽量提高
rlimit_files = 60000
[www]
# 重要,设置fpm运行状态查看地址
pm.status_path = /status
# fpm存活状态检测地址
ping.path = /ping
# 用于检测fpm进程是否运行正常
ping.response = pong
# 子进程处理指定的请求数
pm.max_requests = 5000
# 定义慢速日志文件位置
slowlog = var/log/slow.log
# 慢请求时间上下限,超过此值就记录到日志文件中
request_slowlog_timeout = 1s
# 单个脚本运行超时时间,fpm模式下,php.ini中的max_execution_time配置无效,必须指定
request_terminate_timeout = 30s
# 捕获php程序的报错,并发送给客户端(nginx), 如果不配置这项,通过nginx访问时php报错,无法在nginx响应中看到错误内容,只会产生50X错误
catch_workers_output = yes

添加到系统

1
2
3
4
5
6
7
8
9
10
11
12
cp /usr/src/php-5.6.30/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod a+x /etc/init.d/php-fpm
# 添加到init.d服务中
chkconfig --add php-fpm
# 设置服务自动运行
chkconfig php-fpm on

ln -s /usr/local/php/bin/php /usr/bin/php
ln -s /usr/local/php/sbin/php-fpm /usr/bin/php-pfm
# 配置语法检测
php-pfm -t
service php-fpm start

mysql

配置用户组和文件夹

1
2
3
4
5
6
7
8
9
10
11
# mysql运行用户
$ groupadd mysql
$ useradd mysql -g mysql -M
$ mkdir -p /usr/local/mysql
$ mkdir -p /usr/local/mysql/data
$ chown -R mysql:mysql /usr/local/mysql
$ vi /etc/profile
# profile追加如下内容
PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
export PATH
$ source /etc/profile

下载安装
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
wget https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.41.tar.gz
tar zxvf mysql-5.6.41.tar.gz
cd mysql-5.6.41
yum -y install ncurses-devel perl ncurses-devel bison-devel

cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DMYSQL_TCP_PORT=3306 \
-DMYSQL_USER=mysql \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/usr/local/boost \

make && make install

cd /usr/local/mysql
./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

根据需求进行配置
[mysqld]
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
character_set_server=utf8
init_connect='SET NAMES utf8'
# 开发机设置为远程可以访问的
bind=0.0.0.0
... ...
[client]
default-character-set=utf8

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig mysqld on
service mysqld start

初次进入设置密码提示 其他的操作也做不了
SET PASSWORD = PASSWORD('123456');
GRANT ALL PRIVILEGES ON *.* TO root@"%" WITH GRANT OPTION;
FLUSH PRIVILEGES;

nginx安装
准备

1
2
3
4
5
6
7
8
9
# 用户
$ groupadd www
$ useradd -g www www -s /bin/false -M
# 依赖
$ yum -y install pcre* opensll*

$ wget http://nginx.org/download/nginx-1.7.8.tar.gz
$ tar zxvf nginx-1.7.8.tar.gz
$ cd nginx-1.7.8

安装

1
2
3
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_spdy_module --with-http_stub_status_module --with-pcre

make && make install

系统服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
vi /etc/init.d/nginx
# 追加入如下内容
#!/bin/bash
# chkconfig: - 85 15
# description: Nginx is a World Wide Web server.
# processname: nginx
nginx=/usr/local/nginx/sbin/nginx
conf=/usr/local/nginx/conf/nginx.conf
case $1 in
start)
echo -n "Starting Nginx"
$nginx -c $conf
echo " done"
;;
stop)
echo -n "Stopping Nginx"
killall -9 nginx
echo " done"
;;
test)
$nginx -t -c $conf
;;
reload)
echo -n "Reloading Nginx"
ps auxww | grep nginx | grep master | awk '{print $2}' | xargs kill -HUP
echo " done"
;;
restart)
echo -n "Restart Nginx"
$0 stop
sleep 1
$0 start
echo " done"
;;
show)
ps -aux|grep nginx
;;
*)
echo -n "Usage: $0 {start|restart|reload|stop|test|show}"
;;
esac
# 保存并退出

$ cd /etc/init.d
$ chmod +x nginx
$ chkconfig --add nginx
$ chkconfig --level 2345 nginx on
$ chkconfig nginx on

# 检测配置
$ service nginx test
$ service nginx start

php-fpm和nginx的关联

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
有sock和listen两种配置模式

首先主要的配置一下用户和组,有些时候失败主要是权限的问题。

php-fpm的配置 sock文件不存在的话,可以自己touch一个,一般重启下就有了
user = www
group = www
listen = /var/run/php/php-fpm.sock

ngixn.conf的配置
user www www;
worker_processes 4;
error_log logs/error.log notice;
... ...
http {
... ...
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
... ...
server {
listen 80 default;
server_name _;
return 403;
}
# 各站配置文件
include vhost/*.conf;
... ...
}


server配置 注意下路径就可以了
location ~ \.php$ {
root /home/www/default;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
}

重新启动lnmp,一般就可以运行了。出现问题的时候再具体调试。

其他一些安装

composer可以参照官网进行安装,一般可以运行php -v就可以安装成功,基本上都是成功的;

nodejs安装

安装nodejs费的时间最多,而且特别繁琐。也可以直接使用rpm进行安装。本人也是安装了一次,备份了镜像方便以后使用的。node安装顺利的话估计要一个个小时,gcc编译升级就要两个小时。好慢的。
安装nodejs会提示安装gcc版本低,所以首先要进行gcc升级

1
2
3
4
5
6
7
8
9
10
11
wget http://gcc.skazkaforyou.com/releases/gcc-4.9.1/gcc-4.9.1.tar.gz
tar -xf gcc-4.9.1.tar.gz
cd gcc-4.9.1
./contrib/download_prerequisites ,这个脚本会自动帮你下载所需要的依赖文件和库
mkdir gcc_temp
cd gcc_temp
../configure --enable-checking=release --enable-languages=c,c++ --disable-multilib
出错几率大,而且编辑速度特别慢将近两个小时
make & make install

gcc --version

编译安装之后,默认的gcc库还是没有替换的,所以要手动替换一下

1
2
3
4
5
6
strings /usr/lib/libstdc++.so.6 | grep GLIBCXX 
find / -name "libstdc++.so.6"
cp /usr/local/src/gcc-4.9.1/gcc_temp/stage1-i686-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6 /usr/lib

64位的操作系统,文件是不一样的 /usr/lib64
strings /usr/lib/libstdc++.so.6 | grep GLIBCXX

node安装

1
2
3
4
5
6
7
8
9
$ cd /usr/src/
$ wget https://nodejs.org/dist/v6.9.1/node-v6.9.1.tar.gz
$ tar zxvf ./node-v6.9.1.tar.gz

$ cd ./node-v6.9.1
$ mkdir /usr/local/node
$ ./configure --prefix=/usr/local/node

$ make && make install

node环境配置
1
2
3
4
5
6
#set for nodejs
$ export NODE_HOME=/usr/local/node
$ export PATH=$NODE_HOME/bin:$PATH
$ source /etc/profile
$ node –v