最新消息:博客的意义不在于数量,在于原创。

在阿里云(Centos 6.3 64bit)上搭建LNMP环境

Linux 辰 羽 3973浏览 0评论

搭建LNMP环境就是LNMP环境,为什么这里的标题是在阿里云上啦?因为我选择的是阿里云的 Centos 6.3 64bit 系统。刚开始在网上找到很多搭建LNMP环境的文档都没能搭建成功。

好了,话不多说,搭建系统开始

1、规划目录:

个人觉得无论如何应该都要买一块数据盘吧?虽然选择Linux系统主机送20G硬盘。好吧咱们的数据盘就挂载在 /data 吧,不会挂载的参考:http://help.aliyun.com/view/13435365.html

# 应用安装目录
/usr/local/
# web网站目录
/data/htdocs
# 数据库存放目录
/data/mysql
# 软件包,存下来防止下次使用
/data/lnmpsoft

2、下载必要的软件包

网上有很多人提供了下面一大堆链接,不知道我不会用还是怎么着,反正有些软件是下载不了。我就直接把所有软件打包给大家吧,点击这里

cd /data/src
wget "http://nginx.org/download/nginx-1.5.8.tar.gz"
wget "http://cn2.php.net/distributions/php-5.4.23.tar.bz2"
wget "http://cdn.mysql.com/Downloads/MySQL-5.5/mysql-5.5.32.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 "ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gz"
wget "http://www.cmake.org/files/v2.8/cmake-2.8.12.1.tar.gz"

3、安装必要的组件

yum install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel libtool libtool-libs libevent-devel libevent openldap openldap-devel nss_ldap openldap-clients openldap-servers libtool-ltdl libtool-ltdl-devel bison

4、安装CMake

MySQL5.5之后的版本安装需要用CMake编译,因此首先要先安装CMake

cd /data/lnmpsoft
tar zxf cmake-2.8.12.1.tar.gz
cd cmake-2.8.12.1
./configure --prefix=/usr
make
make install
cd ..

5、安装MySQL

tar xzf mysql-5.5.32.tar.gz
cd mysql-5.5.32
# 首先建立mysql用户组及用户
groupadd mysql
useradd -g mysql mysql
# 编译安装MySQL
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql/ -DMYSQL_DATADIR=/usr/local/mysql/data/db -DMYSQL_UNIX_ADDR=/tmp/mysql/mysqld.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_TCP_PORT=3306 -DWITH_EXTRA_CHARSETS:STRING=ALL -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_UNIX_ADDR=/tmp/mysql/mysql.sock -DWITH_DEBUG=0 -DWITH_READLINE=1 -DWITH_SSL=yes -DSYSCONFDIR=/usr/local/mysql/
make
make install
# 创建一系列的MySQL需要目录
mkdir -p /data/mysql/db
mkdir -p /data/mysql/binlog/
mkdir -p /data/mysql/relaylog/
ln -s /data/mysql/ /usr/local/mysql/data
# 赋予MySQl权限
chmod +w /usr/local/mysql/
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /data/mysql
# 以mysql账号建立数据库
/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/db &
# 复制mysql的启动脚本到/etc/init.d/目录方便启动
cd /usr/local/mysql/support-files/
cp mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
# 配置数据库启动目录
vim /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/data/mysql/db
# 复制配置文件
cp /usr/local/mysql/support-files/my-medium.cnf /usr/local/mysql/my.cnf
cd ../
# 查看在etc目录是否有默认配置my.cnf。如有,在启动mysql里会优先引用etc下的配置文件,所以要把/etc/my.cnf文件删除或修个名字
# 配置my.cnf,可直接 my.cnf.zip
# 配置启动MySQL
/etc/init.d/mysqld start
# 设置MySQL系统自动启动,删除空密码账号:
chkconfig --add mysqld
chkconfig --level 345 mysqld on
service mysqld start
/usr/local/mysql/bin/mysqladmin -u root password
your_mysql_password
# 删除空root密码
/usr/local/mysql/bin/mysql -u root -p
mysql> use mysql;
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'mysql_password';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY 'mysql_password';
mysql> delete from user where host='127.0.0.1' or host='::1' or host='localhost.localdomain';
mysql> delete from user where password="";
mysql> flush privileges;
mysql> quit;
# 创建libmysqlclient软链接
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18 (64位系统)
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18 (32位系统)
# 查看数据库状态
/usr/local/mysql/bin/mysql -u root -p 'your_mysql_password'
show full processlist;
show status;

6、安装PHP所需要的一些库

# libiconv库的编译安装
cd /data/lnmpsoft
tar xzf libiconv-1.14.tar.gz
cd libiconv-1.14/
./configure --prefix=/usr/local
make
make install
cd ..
# libmcrypt库的编译安装
cd /data/lnmpsoft
tar xzf 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 ../../
# 建立libmcrypt库软链接
# 64位系统
ln -sf /usr/local/lib/libmcrypt.la /usr/lib64/libmcrypt.la
ln -sf /usr/local/lib/libmcrypt.so /usr/lib64/libmcrypt.so
ln -sf /usr/local/lib/libmcrypt.so.4 /usr/lib64/libmcrypt.so.4
ln -sf /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib64/libmcrypt.so.4.4.8
ln -sf /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
ln -sf /usr/local/lib/libiconv.so.2 /usr/lib64/libiconv.so.2
ldconfig
# 32位系统(如果你是32位)
ln -sf /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -sf /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -sf /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -sf /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -sf /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
ln -sf /usr/local/lib/libiconv.so.2 /usr/lib/libiconv.so.2
ldconfig
# 编译安装mhash
cd /data/lnmpsoft
tar xzf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9
./configure
make
make install
cd ..
# 添加libmhash相关软链接
# 64位系统使用
ln -sf /usr/local/lib/libmhash.a /usr/lib64/libmhash.a
ln -sf /usr/local/lib/libmhash.la /usr/lib64/libmhash.la
ln -sf /usr/local/lib/libmhash.so /usr/lib64/libmhash.so
ln -sf /usr/local/lib/libmhash.so.2 /usr/lib64/libmhash.so.2
ln -sf /usr/local/lib/libmhash.so.2.0.1 /usr/lib64/libmhash.so.2.0.1
ldconfig
# 32位系统使用(如果你是32位)
ln -sf /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -sf /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -sf /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -sf /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -sf /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
# 编译安装mcrypt:
tar xzf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
/sbin/ldconfig
./configure
make
make install
cd ..

7、安装PHP

# 添加PHP依赖的软链接(64位系统)
ln -s /usr/lib64/libjpeg.so /usr/lib/libjpeg.so
ln -s /usr/lib64/libldap.so /usr/lib/libldap.so
ln -s /usr/lib64/libpng.so /usr/lib/libpng.so
# PHP-FPM编译安装:
tar xjf php-5.4.23.tar.bz2
cd php-5.4.23
./configure -prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --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-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-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --without-pear
make ZEND_EXTRA_LIBS='-liconv'
make install
# 创建PHP的配置文件
cp -f php.ini-production /usr/local/php/etc/php.ini
cd ..
# 由于刚才安装的时候试用了without-pear参数没有安装PEAR(因为直接安装会报错),现在我们再来补上:
/usr/local/php/bin/php go-pear.phar

注意:如果编译的时候出现“virtual memory exhausted: Cannot allocate memory”错误,可以参考这里 

8、安装PHP的扩展模块及配置PHP

接下来我们安装一些PHP常用的模块来优化系统
# 用pecl安装memcache扩展库:
cd /usr/local/php/bin/
./pecl install memcache
# 用pecl安装opcache扩展库,目前还是bate版本
./pecl install zendopcache-beta
# 下载php.ini文件,php.ini.zip
# 建立PHP存放日志的目录
mkdir -p /usr/local/php/logs
# 建立php-fpm的配置文件
cd /usr/local/php/etc
cp php-fpm.conf.default php-fpm.conf
mkdir /usr/local/php/run
vim /usr/local/php/etc/php-fpm.conf
# 下载php-fpm配置文件,php-fpm.conf
# 然后我们添加www用户组和帐号信息,以便后面的php-fpm和nginx能够正常工作:
groupadd www
useradd -g www www
# 接下来,让我们测试下php-fpm配置文件是否正确:
/usr/local/php/sbin/php-fpm -t
# 如果出现"NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful"字样则表示配置正确。否则需要检查配置文件。

9、安装Nginx

# 然后创建Nginx所需的日志目录并分配权限:
mkdir -p /var/log/nginx
chmod +w /var/log/nginx
chown -R www:www /var/log/nginx
# 然后编译安装Nginx所需的pcre库:
cd /data/lnmpsoft
tar xjf pcre-8.34.tar.bz2
cd pcre-8.34/
./configure
make
make install
cd ..
ln -s /lib/libpcre.so.0.0.1 /lib/libpcre.so.1
# 安装所需的GD库:
yum install gd gd-devel
# 编译安装Nginx:
cd /data/lnmpsoft
tar xzf nginx-1.5.8.tar.gz
cd nginx-1.5.8/
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-http_realip_module --with-http_image_filter_module
make
make install
cd ..
# 然后编辑Nginx的配置文件:
vim /usr/local/nginx/conf/nginx.conf
可以直接下载,nginx.conf.zip
# 运行下面的命令测试Nginx的配置文件是否正确:
/usr/local/nginx/sbin/nginx -t
# 如果出现下面提示的一种则表示配置文件无误,否则需要做进一步修改:
# nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
# nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successf

10、设置开机启动

# 首先是MySQL(在编译MySQL已经添加):
chkconfig --add mysqld
chkconfig --level 345 mysqld on
# 然后php-fpm和Nginx的启动放到/etc/rc.local启动文件中:
vim /etc/rc.local
# 在文件的末尾增加下面的内容:
ulimit -SHn 65535
/usr/local/php/sbin/php-fpm
/usr/local/nginx/sbin/nginx

11、各服务器的启动暂停方式

# MySQL:
启动:/etc/init.d/mysqld start
停止:/etc/init.d/mysqld stop
重启:/etc/init.d/mysqld restart

# php-fpm:
启动:/usr/local/php/sbin/php-fpm
停止:killall php-fpm
# php-fpm 关闭:
kill -INT cat /usr/local/php/run/php-fpm.pid
# php-fpm 重启:
kill -USR2 cat /usr/local/php/run/php-fpm.pid
# 查看php-fpm进程数:
ps aux | grep -c php-fpm

#Nginx:
启动:/usr/local/nginx/sbin/nginx
停止:killall nginx
平滑重启:/usr/local/nginx/sbin/nginx -s reload

12、遇到的问题

[emerg] 1552#2780: could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32
网上查了一下资料,这个错误表示无法构建server_names_hash,应该增加server_names_hash_bucket_size的值。解决办法很简单,在Nginx的配置文件的"http {"这一节加上下面的内容即可:
server_names_hash_bucket_size 64;

启动nginx提示:error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory,意思是找不到libpcre.so.1这个模块,而导致启动失败。
如果是32位系统
ln -s /usr/local/lib/libpcre.so.1 /lib
如果是64位系统
ln -s /usr/local/lib/libpcre.so.1 /lib64
然后在启动nginx就OK了

以上记录是我在网络上找资料安装完成后,关联着网络上的资料一起整理的,可能有忽略掉的地方。如有问题可以直接在下面留言。

 

转载请注明:辰羽的视界 » 在阿里云(Centos 6.3 64bit)上搭建LNMP环境

发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址