原文:
http://solf.me/compile-nginx-php-mysql-on-centos-lnmp/
原文作者:夏日里的冻虾
插入:我自己的特别说明
参见网络上的教程,是php-fpm可以用root权限来运行,非常重要。
正文:
LNMP的好处就不说了,特别是对于这个正在运行的小 内存VPS 。鉴于师傅的淫威,我只能手工编译安装Nginx 1.0.15 + PHP 5.3.12 + MySQL 5.1.62 + eAccelerator 0.9.6.1,不能使用各种一键安装包,以及使用yum命令,因为他说“不手动试一下怎么能学到东西”。废话不说,先将步骤和代码贴出来,打造一个适合 新手(如我)的详细安装教程
准备
- 当然是可以上网的机器,可以是虚拟机,VPS,服务器,安装CentOS,其他分支没测试过,最好在虚拟机上先试手一下,因为有 snapshot,随时可以回滚所有操作。
- 必要时先清空iptables, 关闭SELINUX
- 源文件下载的目录是 /usr/local/src
前期工作
更新系统,安装必要的程序,此步骤需要用到yum命令
安装
先下载各个软件包
cd
/usr/local/src
wget http:
//nginx
.org
/download/nginx-1
.0.15.
tar
.gz
wget http:
//au
.php.net
/get/php-5
.3.16.
tar
.gz
/from/us
.php.net
/mirror
wget http:
//pecl
.php.net
/get/APC-3
.1.9.tgz
wget http:
//mysql
.mirror.kangaroot.net
/Downloads/MySQL-5
.1
/mysql-5
.1.62.
tar
.gz
wget http:
//ftp
.gnu.org
/pub/gnu/libiconv/libiconv-1
.14.
tar
.gz
wget http:
//downloads
.sourceforge.net
/mcrypt/libmcrypt-2
.5.8.
tar
.gz?modtime=1171868460&big_mirror=0
wget http:
//downloads
.sourceforge.net
/mcrypt/mcrypt-2
.6.8.
tar
.gz?modtime=1194463373&big_mirror=0
wget http:
//downloads
.sourceforge.net
/mhash/mhash-0
.9.9.9.
tar
.gz?modtime=1175740843&big_mirror=0
wget http:
//downloads
.sourceforge.net
/project/pcre/pcre/8
.30
/pcre-8
.30.
tar
.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fpcre%2Ffiles%2Fpcre%2F8.30%2F&ts=1335184494&use_mirror=aarnet
wget http:
//downloads
.sourceforge.net
/project/eaccelerator/eaccelerator/eAccelerator
%200.9.6.1
/eaccelerator-0
.9.6.1.
tar
.bz2?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Feaccelerator%2Ffiles%2Feaccelerator%2FeAccelerator%25200.9.6.1%2F&ts=1335276559&use_mirror=waix
tar
zxvf libiconv-1.14.
tar
.gz
cd
libiconv-1.14
.
/configure
--prefix=
/usr/local
make
make
install
cd
../
tar
zxvf libmcrypt-2.5.8.
tar
.gz
cd
libmcrypt-2.5.8/
.
/configure
make
make
install
/sbin/ldconfig
cd
libltdl/
.
/configure
--
enable
-ltdl-
install
make
make
install
cd
../../
tar
zxvf mhash-0.9.9.9.
tar
.gz
cd
mhash-0.9.9.9/
.
/configure
make
make
install
cd
../
ln
-s
/usr/local/lib/libmcrypt
.la
/usr/lib/libmcrypt
.la
ln
-s
/usr/local/lib/libmcrypt
.so
/usr/lib/libmcrypt
.so
ln
-s
/usr/local/lib/libmcrypt
.so.4
/usr/lib/libmcrypt
.so.4
ln
-s
/usr/local/lib/libmcrypt
.so.4.4.8
/usr/lib/libmcrypt
.so.4.4.8
ln
-s
/usr/local/lib/libmhash
.a
/usr/lib/libmhash
.a
ln
-s
/usr/local/lib/libmhash
.la
/usr/lib/libmhash
.la
ln
-s
/usr/local/lib/libmhash
.so
/usr/lib/libmhash
.so
ln
-s
/usr/local/lib/libmhash
.so.2
/usr/lib/libmhash
.so.2
ln
-s
/usr/local/lib/libmhash
.so.2.0.1
/usr/lib/libmhash
.so.2.0.1
tar
zxvf mcrypt-2.6.8.
tar
.gz
cd
mcrypt-2.6.8/
/sbin/ldconfig
.
/configure
make
make
install
cd
../
tar
zxvf pcre-8.30.
tar
.gz
cd
pcre-8.30
.
/configure
make
&&
make
install
cd
../
一、安装MySQL
首先是添加组
groupadd mysql
#创建目录,存放data和pid
mkdir
-p
/home/mysql/mysql-5
.1.62
/3306/
chown
-r mysql:mysql
/home/mysql/
useradd
-s
/sbin/nologin
-M -g mysql mysql
.
/configure
--prefix=
/usr/local/webserver/mysql/
--
enable
-assembler --with-extra-charsets=complex --
enable
-thread-safe-client --with-big-tables --with-readline --with-ssl --with-embedded-server --
enable
-
local
-infile --with-plugins=partition,innobase,myisammrg
make
&&
make
install
chmod
+w
/usr/local/webserver/mysql
chown
-R mysql:mysql
/usr/local/webserver/mysql
/usr/local/webserver/mysql/bin/mysql_install_db
--basedir=
/usr/local/webserver/mysql
--user=mysql
cp
-f
/usr/local/webserver/mysql/share/mysql/my-medium
.cnf
/etc/my
.cnf
#将skip-locking 转换成skip-external-locking
sed
-i
's/skip-locking/skip-external-locking/g'
/etc/my
.cnf
sed
-i
's:#innodb:innodb:g'
/etc/my
.cnf
cp
/usr/local/webserver/mysql/share/mysql/mysql
.server
/etc/init
.d
/mysqld
chmod
755
/etc/init
.d
/mysqld
#添加执行权限
ln
-s
/usr/local/webserver/mysql/lib/mysql
/usr/lib/mysql
ln
-s
/usr/local/webserver/mysql/insclude/mysql
/usr/include/mysql
chkconfig mysqld on
#设置开机启动
log-error = /home/mysql/mysql-5.1.62/3306/mysql_error.log
pid-file = /home/mysql/mysql-5.1.62/3306/mysql.pid
datadir = /home/mysql/mysql-5.1.62/3306/data
/usr/local/webserver/mysql/bin/mysqladmin
-u root password ‘root'
cat
>
/tmp/mysql_sec_script
<<EOF
use mysql;
delete from user where not (user=
'root'
) ;
delete from user where user=
'root'
and password=
''
;
drop database
test
;
DROP USER
''
@
'%'
;
flush privileges;
EOF
/usr/local/webserver/mysql/bin/mysql
-u root –p
'root'
-h localhost <
/tmp/mysql_sec_script
rm
-f
/tmp/mysql_sec_script
/etc/init
.d
/mysqld
restart
/etc/init
.d
/mysqld
stop
二、安装PHP
cd
/usr/local/src
tar
zvxf php-5.3.16.
tar
.gz
cd
php-5.3.16
mkdir
-p
/usr/local/webserver/php
.
/configure
--prefix=
/usr/local/webserver/php
--with-config-
file
-path=
/usr/local/webserver/php/etc
--with-mysql=
/usr/local/webserver/mysql
--with-mysqli=
/usr/local/webserver/mysql/bin/mysql_config
--with-iconv-
dir
=
/usr/local
--with-freetype-
dir
--with-jpeg-
dir
--with-png-
dir
--with-zlib --with-libxml-
dir
=
/usr
--
enable
-xml --disable-rpath --
enable
-discard-path --
enable
-safe-mode --
enable
-bcmath --
enable
-shmop --
enable
-sysvsem --
enable
-inline-optimization --with-curl --with-curlwrappers --
enable
-mbregex --
enable
-fpm --
enable
-mbstring --with-mcrypt --with-gd --
enable
-gd-native-ttf --with-openssl --with-mhash --
enable
-pcntl --
enable
-sockets --with-xmlrpc --
enable
-zip --
enable
-soap --with-pdo-mysql=
/usr/local/webserver/mysql/
--with-mysql-sock=
/tmp/mysql
.sock
make
ZEND_EXTRA_LIBS=
'-liconv'
make
install
cp
php.ini-production
/usr/local/webserver/php/etc/php
.ini
cp
sapi
/fpm/init
.d.php-fpm
/etc/init
.d
/php-fpm
#因为php5.3开始自带fpm,使用自带的管理脚本
chmod
+x
/etc/init
.d
/php-fpm
chkconfig php-fpm on
#设置开机自启动
cp
/usr/local/webserver/php/etc/php-fpm
.conf.default
/usr/local/webserver/php/etc/php-fpm
.conf
pid = run
/php-fpm
.pid
user = www
group = www
编译安装apc
cd
/usr/local/src
tar
-zxvf APC-3.1.9.tgz
cd
APC-3.1.9
/usr/local/webserver/php/bin/phpize
.
/configure
--
enable
-apc --
enable
-apc-mmap --with-php-config=
/usr/local/webserver/php/bin/php-config
make
make
install
extension = "apc.so"
apc.enabled = 1
apc.cache_by_default = on
apc.shm_segments = 1
apc.shm_size = 32M
apc.ttl = 3600
apc.user_ttl = 3600
apc.num_files_hint = 0
apc.write_lock = On
三、安装Nginx
首先是建立好用户组
groupadd www
useradd
-s
/sbin/nologin
-g www www
#www 用户不能login
mkdir
-p
/home/wwwroot
#此路 径是用于存放各域名的root路径
chmod
+w
/home/wwwroot
mkdir
-p
/home/wwwlogs
chmod
755
/home/wwwlogs
chown
-R www:www
/home/wwwroot
tar
zxvf nginx-1.0.15.
tar
.gz
cd
nginx-1.0.15/
.
/configure
--user=www --group=www --prefix=
/usr/local/webserver/nginx
--with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
make
&&
make
install
mkdir
/usr/local/webserver/nginx/conf/vhost/
#用于配置各域名
rm
-f
/usr/local/webserver/nginx/conf/nginx
.conf
vi
/usr/local/webserver/nginx/conf/nginx
.conf
user www www;
worker_processes 1;
error_log /home/wwwlogs/nginx_error.log crit;
pid /usr/local/webserver/nginx/logs/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
#log format
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
include vhost/*.conf;
}
cd
/usr/local/webserver/nginx/conf/vhost
vi
domain.com.conf
server
{
listen 80;
server_name www.domain.com domain.com;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/domain.com/www;
location ~ .*\.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
access_log /home/wwwlogs/domain.com.access.log;
}
mkdir
-p
/home/wwwroot/domain
.com
/www/
#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=
/usr/local/sbin
:
/usr/local/bin
:
/sbin
:
/bin
:
/usr/sbin
:
/usr/bin
DESC=
"nginx daemon"
NAME=nginx
DAEMON=
/usr/local/webserver/nginx/sbin/
$NAME
CONFIGFILE=
/usr/local/webserver/nginx/conf/
$NAME.conf
PIDFILE=
/usr/local/webserver/nginx/logs/
$NAME.pid
SCRIPTNAME=
/etc/init
.d/$NAME
set
-e
[ -x
"$DAEMON"
] ||
exit
0
do_start() {
$DAEMON -c $CONFIGFILE ||
echo
-n
"nginx already running"
}
do_stop() {
kill
-INT `
cat
$PIDFILE` ||
echo
-n
"nginx not running"
}
do_reload() {
kill
-HUP `
cat
$PIDFILE` ||
echo
-n
"nginx can't reload"
}
case
"$1"
in
start)
echo
-n
"Starting $DESC: $NAME"
do_start
echo
"."
;;
stop)
echo
-n
"Stopping $DESC: $NAME"
do_stop
echo
"."
;;
reload|graceful)
echo
-n
"Reloading $DESC configuration..."
do_reload
echo
"."
;;
restart)
echo
-n
"Restarting $DESC: $NAME"
do_stop
do_start
echo
"."
;;
*)
echo
"Usage: $SCRIPTNAME {start|stop|reload|restart}"
>&2
exit
3
;;
esac
exit
0
chmod
+x
/etc/init
.d
/nginx
chkconfig nginx on
直接输入以下命令,会得到相应的结果
service nginx restart #重启nginx
service mysqld restart #重启mysql
service php-fpm restart #重启php-fpm
service php-fpm stop #停止php-fpm
service php-fpm start #启动php-fpm
各站点的根目录是 /home/wwwroot/
应用的目录是 /usr/local/webserver/
各配置文件的存放位置是:
php: /usr/local/webserver/php/etc/
mysql: /etc/
nginx: /usr/local/webserver/nginx/conf/
1. 启动nginx的时候遇到 error while loading shared libraries: libpcre.so.1:
答:增加以下软连接,注意,如果是64位系统,对应目录是/lib64
#cd /lib64
cd
/lib
ln
-s libpcre.so.0.0.1 libpcre.so.1
参考:
CentOS 6.2编译安装Nginx1.0.14+MySQL5.5.22+PHP5.3.10 http://www.osyunwei.com/archives/3339.html
Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器(第6版)[原创]http://blog.s135.com /nginx_php_v6/
LNMP一键安装 http://lnmp.org/