做php开发的,想要进一步提升自己,手动搭建开发环境,我想是必须经历的一个坎。虽然说有很多第三方集成环境可供使用,但我想说的是在你没有自己搭建过一次环境的时候,你没有太多的资本去“偷懒”。虽然我自己也是个菜鸟,但我乐意分享我的成长历程和学习成果。所以今天我说说我自己搭建lnmp环境的整个流程,有表述不清的地方或者错误,希望大伙不吝指出。废话不多说了,开干。
一、主机环境:
虚拟机安装的centos 6.7 , i686
二、各应用版本:
php: 5.6.3
mysql: 5.6.12
nginx: 1.8.0
三、准备安装包:
为了节省大伙时间,我已经放置在微云盘: http://share.weiyun.com/f891e9c6547bd9abf03072e554626ee6(密码:9Z4G),包含了安装过程需要用到的所有安装包。
现在,我们做如下约定:
<pre name="code" class="plain"> <strong><span style="font-size:18px;">/usr/local/src</span></strong>
这是我们存放安装包的路径<pre name="code" class="plain"> <strong><span style="font-size:18px;">/usr/local</span></strong>
这是各应用的安装路径
在安装包目录下解压压缩包:
<pre name="code" class="plain"> <strong><span style="font-size:18px;">unzip lnmp.zip</span></strong>
四、环境预设:
1、防火墙设置:
打开 iptables文件:
<pre name="code" class="plain"><span style="font-size:18px;"> <strong>vim /etc/sysconfig/iptables</strong></span>
添加两行:
<span style="font-size:18px;">-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</span>
第一行是为网络协议开放的端口,第二行为mysql数据库开放的端口。
编辑完要重启iptables:
<pre name="code" class="plain"><strong><span style="font-size:18px;">service iptables restart</span></strong>
2、关闭SELINUX:
<strong><span style="font-size:18px;">vim /etc/selinux/config</span></strong>
</pre><pre>
注释掉 SELINUX=enforcing 和 SELINUXTYPE=targeted,
然后 在最底部添加
SELINUX=disabled
3、安装编译工具及依赖库文件:
直接用yum命令安装,如下:
<pre name="code" class="plain"><strong><span style="font-size:18px;">yum install -y apr* autoconf automake bison bzip2 bzip2* cloog-ppl compat* cpp curl curl-devel fontconfig fontconfig-devel freetype freetype* freetype-devel gcc gcc-c++ gtk+-devel gd gettext gettext-devel glibc kernel kernel-headers keyutils keyutils-libs-devel krb5-devel libcom_err-devel libpng libpng* libpng-devel libjpeg* libsepol-devel libselinux-devel libstdc++-devel libtool* libgomp libxml2 libxml2-devel libXpm* libX* libtiff libtiff* make mpfr ncurses* ntp openssl nasm nasm* openssl-devel patch pcre-devel perl php-common php-gd policycoreutils ppl telnet t1lib t1lib* wget zlib-devel</span></strong>
五、安装mysql
1、由于是采用cmake方式编译安装mysql,因此得先安装cmake:
<pre name="code" class="plain"><strong><span style="font-size:18px;">cd /usr/local/src</span></strong>
<strong><span style="font-size:18px;">tar -zxvf cmake-3.0.2.tar.gz</span></strong>
<strong><span style="font-size:18px;">cd cmake-3.0.2
./configure && make && make install</span></strong>
2、cmake安装完成后即可编译安装mysql了,正式安装mysql之前,我们还有个小任务
<pre name="code" class="plain"><strong><span style="font-size:18px;">groupadd mysql #添加mysql组
useradd -g mysql mysql -s /bin/false #创建用户mysql并加入到mysql组,不允许mysql用户直接登录系统
mkdir -p /data/mysql #创建MySQL数据库存放目录
chown -R mysql:mysql /data/mysql #设置MySQL数据库存放目录权限
mkdir -p /usr/local/mysql #创建MySQL安装目录</span></strong>
3、现在可以正式编译mysql了:
<pre name="code" class="plain"><strong><span style="font-size:18px;">tar -zxvf mysql-5.6.21.tar.gz
cd mysql-5.6.21
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_TCP_PORT=3306
make && make install</span></strong>
4、编译完成后我们就用进入mysql的安装目录:
<pre name="code" class="plain"><strong><span style="font-size:18px;">cd /usr/local/mysql</span></strong>
生成mysql数据库系统:
<pre name="code" class="plain"><strong><span style="font-size:18px;">./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql</span></strong>
<pre name="code" class="html">
<strong><span style="font-size:18px;">cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld</span></strong>
<strong><span style="font-size:18px;">
vim /etc/rc.d/init.d/mysqld</span></strong>
<pre name="code" class="plain">
找到大概46、47行的位置,修改如下:
<strong><span style="font-size:18px;">basedir=/usr/local/mysql
datadir=/data/mysql</span></strong>
保存退出。
6、试着启动mysql:
<pre name="code" class="plain"><strong><span style="font-size:18px;">service mysqld start</span></strong>
可以看到 "Starting MySQL. SUCCESS !"说明启动成功。
我们通常习惯将其放置系统变量,因此打开/etc/profile 文件,在倒数第三行左右,修改如下:
<strong><span style="font-size:18px;">export PATH=$PATH:/usr/local/mysql/bin</span></strong>
然后刷新/etc/profile文件:
<pre name="code" class="plain"><strong><span style="font-size:18px;">source /etc/profile</span></strong>
<pre name="code" class="plain"><strong><span style="font-size:18px;">mysql_secure_installation</span></strong>
输入命令后按照提示走,相信你可以的。
设置完后,我们测试root密码是否生成了:
<pre name="code" class="plain"><strong><span style="font-size:18px;">mysql -uroot -p</span></strong>
根据提示输入刚刚设置的root新密码,
如果看到上面这个界面,那么恭喜你,说明你完成了mysql的安装。
六、安装nginx
1、安装nginx所需的依赖包pcre、openssl、zlib:
<pre name="code" class="plain"><strong><span style="font-size:18px;">tar -zxvf pcre-8.36.tar.gz
cd pcre-8.36
./configure && make && make install</span></strong>
安装openssl和zlib和安装pcre的方法是一样的,这里不多赘述。
在按装完openssl后,需要多做件事: 打开/etc/profile,在之前添加的
<strong><span style="font-size:18px;">export PATH=$PATH:/usr/local/mysql/bin</span></strong>
后面添加下面这行:
<strong><span style="font-size:18px;">export PATH=$PATH:/usr/local/openssl/bin
</span></strong>
保存后,让其生效:
<pre name="code" class="plain"><pre name="code" class="plain"><strong><span style="font-size:18px;">source /etc/profile</span></strong>
2、编译安装nginx:
<pre name="code" class="plain"><strong><span style="font-size:18px;">tar -zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=nobody --group=nobody --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-openssl=/usr/local/src/openssl-1.0.1j --with-zlib=/usr/local/src/zlib-1.2.8 --with-pcre=/usr/local/src/pcre-8.36</strong>
make && make install</span></strong>
编译安装完成后,启动:
<pre name="code" class="plain"><pre name="code" class="plain"><strong><span style="font-size:18px;">/usr/local/nginx/sbin/nginx</span></strong>
然后将在/etc/rc.d/init.d/下新增文件nginx,内容如下
<strong><span style="font-size:18px;">#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
ulimit -SHn 65535
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac</span></strong>
赋予其执行权限:
<pre name="code" class="plain"><strong><span style="font-size:18px;">chmod 775 /etc/rc.d/init.d/nginx</span></strong>
设置开机启动:
<pre name="code" class="plain"><strong><span style="font-size:18px;">chkconfig --add nginx
chkconfig nginx on </span></strong>
再重启nginx:
<pre name="code" class="plain"><strong><span style="font-size:18px;">service nginx restart</span></strong>
3、测试nginx是否安装成功:
在浏览器网址输入框输入: localhost ,你会看到很鸡冻的画面。
==================================================我的鸡汤========================================================
天哪,废了好大劲才把mysql安装完成,这全部弄完不累死才怪。。。
千万别这么想啊,最难过的时候,也是离成功最近的时候。所以一定要挺住,干吧得
=================================================================================================================
七、安装php
1、先安装php的相关扩展库yasm、libmcrypt、libvpx、tiff、libpng、freetype、jpeg
这些命令都是: ./configure && make && make install
安装 libgd 有些区别:
<pre name="code" class="plain"><strong><span style="font-size:18px;">./configure --prefix=/usr/local/libgd --enable-shared --with-jpeg=/usr/local/jpeg --with-png=/usr/local/libpng --with-freetype=/usr/local/freetype --with-fontconfig=/usr/local/freetype --with-xpm=/usr/ --with-tiff=/usr/local/tiff --with-vpx=/usr/local/libvpx
make && make install</span></strong>
安装 t1lib 这里有个坑,虽小但估计害死了一批人,记得不是make而是make without_doc:
<pre name="code" class="plain"><strong><span style="font-size:18px;">./configure --prefix=/usr/local/t1lib --enable-shared
make without_doc && make install</span></strong>
2、编译安装php
<pre name="code" class="plain"><strong><span style="font-size:18px;">tar -zvxf php-5.6.3.tar.gz
cd php-5.6.3
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-pdo-mysql=/usr/local/mysql --with-gd --with-png-dir=/usr/local/libpng --with-jpeg-dir=/usr/local/jpeg --with-freetype-dir=/usr/local/freetype --with-xpm-dir=/usr/ --with-vpx-dir=/usr/local/libvpx/ --with-zlib-dir=/usr/local/zlib --with-t1lib=/usr/local/t1lib --with-iconv --enable-libxml --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-opcache --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt --with-curl --enable-ctype
make && make test && make install</span></strong>
3、配置php
复制php配置文件到安装目录:
<pre name="code" class="plain"><strong><span style="font-size:18px;">cp php.ini-production /usr/local/php/etc/php.ini</span></strong>
将配置文件链到etc下:
<pre name="code" class="plain"><strong><span style="font-size:18px;">ln -s /usr/local/php/etc/php.ini /etc/php.ini</span></strong>
4、由于我们使用php-fpm管理php的,因此php-fpm也需要配置
<pre name="code" class="plain"><strong><span style="font-size:18px;">cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
vim /usr/local/php/etc/php-fpm.conf</span></strong>
修改对应的地方(地25行左右):
<strong><span style="font-size:18px;">pid = run/php-fpm.pid #取消前面的分号</span></strong>
接下来设置php-fpm开机启动:
<pre name="code" class="plain"><strong><span style="font-size:18px;">cp /usr/local/src/php-5.6.3/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
chmod +x /etc/rc.d/init.d/php-fpm
chkconfig php-fpm on </span></strong>
5、配置nginx支持php
所有服务都安装完了,但这时候nginx和php还不是同路人呢,得给他们牵条线,双方才能合作呢。
<pre name="code" class="plain"><strong><span style="font-size:18px;">vim /usr/local/nginx/conf/fcgi.conf</span></strong>
内容如下:
<pre name="code" class="plain"><strong><span style="font-size:18px;">fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
</span></strong>
修改nginx配置文件
<pre name="code" class="plain"><strong><span style="font-size:18px;">vim /usr/local/nginx/conf/nginx.conf</span></strong>
内容修改如下:
<pre name="code" class="plain"><strong><span style="font-size:18px;">user nobody nobody;
worker_processes 4;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /data/www;
index index.php index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root html;
#}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /data/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf; #此处就是引用刚刚新增的文件
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
location /tongji {
stub_status on;
access_log off;
auth_basic "Nginx status";
auth_basic_user_file /usr/local/nginx/.htpasswd;
}
}
</span></strong>
6、测试php和nginx是否合作愉快:
在/data/www/目录下新增index.php:
写入php测试代码:
<pre name="code" class="php"><strong><span style="font-size:18px;"><?php
<span style="white-space:pre"> </span>phpinfo();
?></span></strong>
打开浏览器,输入localhost,如果你看到phpinfo函数打印出的相关信息,说明你成功了。好感动啊啊啊。。。。。
七、总结
此教程属于为新手准备的饭菜,按照此教程配置的lnmp环境只是达到了让其工作的层面,离部署代码正式上线是还有很大的差距,因此,希望各位参考为主,不应该将此配置成功用于正式环境。介于我粗劣的描述能力,不喜勿喷。