mysql apache Firefox php 微软 nginx HTML5 Python Android Windows Ubuntu 程序员 centos java linux命令 shell 开源 google wordpress linux

LNMP环境搭建RHEL5.4 + Nginx1.4.2 + PHP5.4.17(FastCGI) + MySQL5.5.32

闲来搭建环境练手,每次搭建LNMP都有不一样的经历,每次碰到的问题还不大一样,值得庆幸的是每次遇到的问题都顺利解决。前些日子有同学想看如何搭建LNMP环境来求分享,说实在的这样的分享意义不大,环境的搭建都是经验的积累即使分享也会很枯燥并且没什么效果,重要的是一定要自己动手搭建,遇到问题自己想办法解决,动手可能是唯一的绝技^_^。

重整yum

搭建环境的时候使用yum来安装一些基础软件包是个不错的选择,不幸的是该系统的yum不可用,那么就自己动手重新安装yum

rpm -aq|grep yum|xargs rpm -e –-nodeps//卸载yum重新安装新yum  
wget http://CentOS.ustc.edu.cn/centos/5/os/x86_64/CentOS/yum-fastestmirror-1.1.16-21.el5.centos.noarch.rpm   
wget http://centos.ustc.edu.cn/centos/5/os/x86_64/CentOS/yum-metadata-parser-1.1.2-4.el5.x86_64.rpm    
wget http://centos.ustc.edu.cn/centos/5/os/x86_64/CentOS/yum-3.2.22-40.el5.centos.noarch.rpm       
wget http://centos.ustc.edu.cn/centos/5/os/x86_64/CentOS/python-iniparse-0.2.3-6.el5.noarch.rpm  
rpm -ivh python-iniparse-0.2.3-6.el5.noarch.rpm  yum-metadata-parser-1.1.2-4.el5.x86_64.rpm yum-3.2.22-40.el5.centos.noarch.rpm yum-fastestmirror-1.1.16-21.el5.centos.noarch.rpm   
cd /etc/yum.repos.d/  
wget http://mirrors.163.com/.help/CentOS-Base-163.repo)  
mv CentOS-Base.repo.5 CentOS-Base.repo    

默认的配置文件中服务器地址用的版本号是变量$releasever,所以需要将其替换为实际的版本号,否则是无法连接到服务器的。当前CentOS最新版是5.4,所以我们修改CentOS-Base.repo

vi编辑器中进行全文件替换命令如下:

:1,$s/$releasever/5/g  
yum update  

很长时间的下载后,报错:

warning: rpmts_HdrFromFdno: Header V3 DSA signature: NOKEY, key ID e8562897

出现类似错误-解决方法

rpm --import /etc/pki/rpm-gpg/RPM*   
rpm -ivh python-iniparse-0.2.3-6.el5.noarch.rpm  yum-metadata-parser-1.1.2-4.el5.x86_64.rpm yum-3.2.22-40.el5.centos.noarch.rpm yum-fastestmirror-1.1.16-21.el5.centos.noarch.rpm --force --nodeps   

重新进行yum update 会出现

GPG key retrieval failed: [Errno 5] OSError: [Errno 2] No such file or directory: ‘/etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5′

指定的文件/etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5在本地不存在导致的。

去官网http://mirror.centos.org/centos/下载相应文件

cd /etc/pki/rpm-gpg  
wget http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5  

到此不出意外的话yum就可以正常使用了,可能你会感觉纳闷怎么RHEL5.4的系统怎么用CentOS-5的yum源。其实CentOS是RHEL的一个分支,CentOS的软件是可以在RHEL中使用的,接下来要把一些基础包一次性搞定。

yum -y 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 openldap openldap-devel nss_ldap openldap-clients openldap-servers  

安装Nginx
安装nginx需要zlib与pcre、ssl看情况而定需要的话就单独安装
安装zlib

cd /usr/local/src/zlib  
wget http://zlib.net/zlib-1.2.8.tar.gz  
tar -zxvf zlib-1.2.8.tar.gz   
./configure && make && make install  

安装pcre

cd /usr/local/src/pcre  
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz  
tar -zxvf pcre-8.21.tar.gz  
cd pcre-8.21  
./configure && make && make install  

安装nginx

cd /usr/local/src/nginx  
wget http://nginx.org/download/nginx-1.4.2.tar.gz  
cd nginx-1.4.2  
./configure  --user=www --group=www --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-pcre=/usr/local/src/pcre/pcre-8.21 --with-zlib=/usr/local/src/zlib/zlib-1.2.8 && make && make install  

开机启动

echo "/usr/local/nginx/nginx" >> /etc/rc.local  

安装mysql

wget http://www.cmake.org/files/v2.8/cmake-2.8.4.tar.gz  
wget http://ftp.gnu.org/gnu/bison/bison-2.5.tar.gz  
wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.32.tar.gz/from/http://cdn.mysql.com/  

tar zxvf cmake-2.8.4.tar.gz  
cd cmake-2.8.4  
./bootstrap && make && make install  
tar zxvf bison-2.5.tar.gz  
cd bison-2.5  
./configure && make && make install  
/usr/sbin/groupadd mysql  
/usr/sbin/useradd -g mysql mysql  
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql   
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock   
-DDEFAULT_CHARSET=utf8   
-DDEFAULT_COLLATION=utf8_general_ci   
-DWITH_EXTRA_CHARSETS=all   
-DWITH_MYISAM_STORAGE_ENGINE=1   
-DWITH_INNOBASE_STORAGE_ENGINE=1   
-DWITH_MEMORY_STORAGE_ENGINE=1   
-DWITH_READLINE=1   
-DENABLED_LOCAL_INFILE=1   
-DMYSQL_DATADIR=/var/mysql/data   
-DMYSQL_USER=mysql  

详细编译参数参考:http://dev.mysql.com/doc/refman/5.5/en/source-configuration-options.html

make && make install  
chmod +w /usr/local/mysql  
chown -R mysql:mysql /usr/local/mysql  
ln -s /usr/local/mysql/lib/libmysqlclient.so.16 /usr/lib/libmysqlclient.so.16  
mkdir -p /var/mysql/  
mkdir -p /var/mysql/data/  
mkdir -p /var/mysql/log/  
chown -R mysql:mysql /var/mysql/  
cd support-files/  
cp my-large.cnf /var/mysql/my.cnf  
cp mysql.server /etc/rc.d/init.d/mysqld  
/usr/local/mysql/scripts/mysql_install_db --defaults-file=/var/mysql/my.cnf   
--basedir=/usr/local/mysql   
--datadir=/var/mysql/data   
--user=mysql  
  
chmod +x /etc/init.d/mysqld  
vi /etc/init.d/mysqld#(编辑此文件,查找并修改以下变量内容46行左右:)  
basedir=/usr/local/mysql  
datadir=/var/mysql/data  
./mysql -u root #登陆mysql修改root账号密码  
update mysql.user set password=PASSWORD('123') where user='root';  

默认情况下mysql监听的地址是0.0.0.0,是允许外网访问的,风险就不多说,如何修改mysql的监听地址?

修改/etc/init.d/mysql文件

在start部分找到类似下面这行

$bindir/mysqld_safe --datadir=$datadir --pid-file=$pid_file   

修改成

$bindir/mysqld_safe --datadir=$datadir --pid-file=$pid_file --bind-address=127.0.0.1
service mysqld restart

安装PHP

wget http://blog.s135.com/soft/Linux/nginx_php/libiconv/libiconv-1.13.tar.gz  

wget http://blog.s135.com/soft/linux/nginx_php/mcrypt/libmcrypt-2.5.8.tar.gz  

wget http://blog.s135.com/soft/linux/nginx_php/mhash/mhash-0.9.9.9.tar.gz  

wget http://blog.s135.com/soft/linux/nginx_php/mcrypt/mcrypt-2.6.8.tar.gz  

tar zxvf libiconv-1.13.tar.gz  

cd libiconv-1.13/  

./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 ../  

cd /usr/local/src/php/php-5.4.17  

./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-libxml-dir --with-iconv-dir --with-freetype-dir --with-png-dir --with-jpeg-dir --with-zlib --with-mcrypt --with-mhash --with-gd --with-curl --with-bz2 --without-pear --enable-mbstring --enable-xml --enable-bcmath --enable-safe-mode --enable-fpm --with-fpm-user=www --with-fpm-group=www --disable-fileinfo  

cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf  

cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm  

chmod o+x /etc/init.d/php-fpm  

cp php.ini-development /usr/local/php/etc/php.ini  

echo "service php-fpm start" >> /etc/rc.local  

ln -s /usr/local/php/etc/php.ini /etc/php.ini  

ln -s /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf  

php的编译参数根据各自的需要决定

延伸阅读

评论