LNMP为(Linux+Nginx+Mysql+Php)的简称,本次系统环境为CentOS 6.6 Linux 2.6.32-504.el6.x86_64,Nginx和Php采用源码安装方式,Mysql采用二进制包安装方式
一、安装Nginx
1.1上传安装包
Nginx官网http://www.nginx.org,下载稳定版本1.8.1,上传至源码包目录/root/tools,没有rz命令可以安装lrzsz包。
#mkdir /root/tools #cd /root/tools #rz # ll nginx-1.8.1.tar.gz -rw-r--r--. 1 root root 833473 Mar 11 16:35 nginx-1.8.1.tar.gz #tar xf nginx-1.8.1.tar.gz #cd nginx-1.8.1
1.2安装依赖的软件包
首先安装开发环境Development Tools
#yum groupinstall "Development Tools"
安装依赖的软件包
#yum install pcre-devel openssl-devel
1.3创建相应的用户
#useradd -r -M -s /sbin/nologin nginx
1.4编译安装
#./configure --prefix=/application/nginx-1.8.1 --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre #make #make install
1.5创建链接
# ln -s /application/nginx-1.8.1/ /application/nginx
1.6启动服务
# /application/nginx/sbin/nginx -t nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful # /application/nginx/sbin/nginx # netstat -tlunp|grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 10781/nginx # curl -I 127.0.0.1 HTTP/1.1 200 OK Server: nginx/1.8.1 Date: Thu, 12 May 2016 06:13:47 GMT Content-Type: text/html Content-Length: 24 Last-Modified: Tue, 10 May 2016 07:43:20 GMT Connection: keep-alive ETag: "57319118-18" Accept-Ranges: bytes
打开浏览器测试是否可以正常访问,提示Thank you for using nginx即为正常!
加入开机自启
echo "/application/nginx/sbin/nginx" >>/etc/rc.local
二、安装Mysql
2.1上传二进制包
#cd /root/tools #rz #tar xf mysql-5.5.32-linux2.6-x86_64.tar.gz #mv mysql-5.5.32-linux2.6-x86_64 /application/mysql-5.5.32 #ln -s /application/mysql-5.5.32/ /application/mysql
2.2添加用户,安装依赖包
#yum install libaio #useradd -r -M -s /sbin/nologin mysql #chown -R mysql:mysql /application/mysql-5.5.32/ #cd /application/mysql
2.3初始化安装
初始化安装时需看到两个OK
#cd /application/mysql #./scripts/mysql_install_db --basedir=/application/mysql --user=mysql --datadir=/application/mysql/data
2.4添加开机自启
#cp support-files/mysql.server /etc/init.d/mysqld #cp support-files/my-small.cnf ./my.cnf #sed -i 's#/usr/local/mysql#/application/mysql#g' /etc/init.d/mysqld #sed -i 's#/usr/local/mysql#/application/mysql#g' /application/mysql/bin/mysqld_safe
2.5启动
#chkconfig mysqld on #chkconfig --list mysqld mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off #/etc/init.d/mysqld start #lsof -i :3306 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mysqld 26754 mysql 10u IPv4 83324 0t0 TCP *:mysql (LISTEN)
如果启动报如下错误:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
则需要创建一个软链接
# mkdir /var/lib/mysql/ # chown mysql /var/lib/mysql/ # ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock
2.6修改密码
第一次启动修改密码:
#mysqladmin -uroot password 123456
有密码情况下修改,-p跟密码没有交互式,如果-p不跟密码,则需要交互式输入密码,安全性更高
#mysqladmin -uroot -p123456 password "newpassword"
注意事项:如果/etc/my.cnf存在,需要删除,使用自定义复制过来的配置文件,否则启动时会报错
如果使用源码安装,编译参数参考如下:
cmake \ -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.30 \ -DMYSQL_DATADIR=/application/mysql-5.6.30/data \ -DSYSCONFDIR=/etc \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_MEMORY_STORAGE_ENGINE=1 \ -DWITH_READLINE=1 \ -DMYSQL_UNIX_ADDR=/application/mysql-5.6.30/mysql.sock \ -DMYSQL_TCP_PORT=3306 \ -DENABLED_LOCAL_INFILE=1 \ -DWITH_PARTITION_STORAGE_ENGINE=1 \ -DWITH-EXTRA_CHARSETS=all \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci make && make install
三、安装PHP
3.1上传安装包
#cd /root/tools #rz #tar xf php-5.5.30.tar.gz #cd php-5.5.30
3.2安装依赖包
安装mcrypt包时需要epel源支持,可以使用阿里的云源http://mirrors.aliyun.com
#wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
如果其它版本的,请参照阿里云网站
#yum install make glibc php-gd gd-devel php-xml php-mbstring php-bcmath libpng-devel bzip2-devel curl libcurl-devel libssh2-devel libmcrypt-devel mhash mhash-devel mcrypt libxslt-devel zlib zlib-devel libxml2-devel libjpeg-turbo-devel libiconv-devel
安装libiconv包
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz tar xf libiconv-1.14.tar.gz cd libiconv-1.14 ./configure --prefix=/usr/local/libiconv make && make install
将mysql库链接到标准库目录中
echo /application/mysql/lib > /etc/ld.so.conf.d/mysql.conf ldconfig
3.3编译安装
./configure \ --prefix=/application/php5.5.30 \ --with-mysql=/application/mysql \ --with-iconv-dir=/usr/local/libiconv \ --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-inline-optimization \ --with-curl \ --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 \ --enable-short-tags \ --enable-static \ --with-xsl \ --with-fpm-user=nginx \ --with-fpm-group=nginx \ --enable-ftp
如果编译参数不想直接指定mysql的路径也可使用mysqlnd代替
--with-MySQL=mysqlnd --with-mysqli=mysqlnd
3.4修改配置文件
ln -s /application/php5.3.27/ /application/php cd /root/tools/php-5.5.30 cp php.ini-production /application/php/lib/php.ini cd /application/php/etc cp php-fpm.conf.default php-fpm.conf
3.5启动服务
/application/php/sbin/php-fpm # ps -ef|grep php-fpm root 113566 1 0 12:14 ? 00:00:00 php-fpm: master process (/application/php5.5.30/etc/php-fpm.conf) nginx 113567 113566 0 12:14 ? 00:00:00 php-fpm: pool www nginx 113568 113566 0 12:14 ? 00:00:00 php-fpm: pool www root 121828 113884 0 14:50 pts/3 00:00:00 grep php-fpm
修改nginx配置文件,加入php支持
#cd /application/nginx/conf
修改nginx.conf,在server大括号内,添加一个location
location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; }
3.6测试
检查nginx语法并重启
/application/nginx/sbin/nginx -t nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful /application/nginx/sbin/nginx -s reload
在网站根目录新建phpinfo.php文件
<?php phpinfo(); ?>
在浏览器中输入http://127.0.0.1/phpinfo.php
出现一个php环境的页面即为正常,使用完成后删除phpinfo.php
在网站根目录新建一个testmysql.php文件
<?php $link_id=mysql_connect('localhost','root','123456') ; if($link_id){ echo "mysql successful !"; }else{ echo mysql_error(); } ?>
在浏览器中输入http://127.0.0.1/testmysql.php
如果提示为:mysql successful则表示php连mysql成功