# 手动搭建lnmp环境
## 1、配置防火墙,开启80端口、3306端口
~~~
vi /etc/sysconfig/iptables
将
-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
加到 22端口配置后面
~~~
## 2、关闭SELINUX
~~~
vi /etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
~~~
重启centos reboot -n
## 3、系统约定
软件源代码包存放位置:/usr/local/src
源码包编译安装位置:/usr/local/软件名字
## 4、下载软件
下载nginx(目前稳定版)http://nginx.org/download/nginx-1.5.13.tar.gz
下载pcre(支持nginx伪静态)http://sourceforge.net/projects/pcre/files/pcre/8.35/pcre-8.35.tar.gz
下载MySQL http://mirrors.sohu.com/mysql/MySQL-5.5/mysql-5.5.37.tar.gz
下载php http://ar2.php.net/get/php-5.5.10.tar.gz/from/this/mirror
下载cmake(MySQL编译工具) http://www.cmake.org/files/v2.8/cmake-2.8.8.tar.gz
下载libmcrypt(PHPlibmcrypt模块)
http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
## 5、安装编译工具及库文件(使用CentOS yum命令安装)
~~~
yum install make apr* autoconf automake curl-devel gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl kernel-headers compat* mpfr cpp glibc libgomp libstdc++-devel ppl cloog-ppl keyutils-libs-devel libcom_err-devel libsepol-devel libselinux-devel krb5-devel zlib-devel libXpm* freetype libjpeg* libpng* php-common php-gd ncurses* libtool* libxml2 libxml2-devel patch
~~~
## 6、安装cmake
~~~
tar zxvf cmake-2.8.8.tar.gz
cd cmake-2.8.8
./configure
make
make install
~~~
## 7、安装mysql
~~~
groupadd mysql #添加mysql组
useradd -g mysql mysql -s /bin/false #创建用户mysql并加入到mysql组,不允许mysql用户直接登录系统
mkdir -p /data/mysql #创建MySQL数据库存放目录
chown -R mysql:mysql /data/mysql #设置权限
mkdir -p /usr/local/mysql #创建安装目录
tar zxvf mysql-5.5.37.tar.gz
cd mysql-5.5.37
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc #配置
make #编译
make install #安装
cd /usr/local/mysql
cp ./support-files/my-huge.cnf /etc/my.cnf #拷贝配置文件(注意:/etc目录下面默认有一个my.cnf,直接覆盖即可)
vi /etc/my.cnf #编辑配置文件,在 [mysqld] 部分增加
datadir = /data/mysql #添加MySQL数据库路径
./scripts/mysql_install_db --user=mysql #生成mysql系统数据库
cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld #把Mysql加入系统启动
chmod 755 /etc/init.d/mysqld #增加执行权限
chkconfig mysqld on #加入开机启动
vi /etc/rc.d/init.d/mysqld #编辑
basedir = /usr/local/mysql #MySQL程序安装路径
datadir = /data/mysql #MySQl数据库存放目录
service mysqld start #启动
vi /etc/profile #把mysql服务加入系统环境变量:在最后添加下面这一行
export PATH=$PATH:/usr/local/mysql/bin
下面这两行把myslq的库文件链接到系统默认的位置,这样你在编译类似PHP等软件时可以不用指定mysql的库文件地址。
ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
ln -s /usr/local/mysql/include/mysql /usr/include/mysql
shutdown -r now #需要重启系统,等待系统重新启动之后继续在终端命令行下面操作
mysql_secure_installation #设置Mysql密码
根据提示按Y 回车输入2次密码
或者直接修改密码/usr/local/mysql/bin/mysqladmin -u root -p password "123456" #修改密码
service mysqld restart #重启
~~~
## 8、安装PCRE
~~~
cd /usr/local/src
mkdir /usr/local/pcre
tar zxvf pcre-8.35.tar.gz
cd pcre-8.35
./configure --prefix=/usr/local/pcre
make
make install
~~~
## 9、安装nginx
~~~
cd /usr/local/src
groupadd www
useradd -g www www -s /bin/false
tar zxvf nginx-1.5.13.tar.gz
cd nginx-1.5.13
./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-openssl=/usr/ --with-pcre=/usr/local/src/pcre-8.35
make
make install
~~~
#设置nginx自启动,加入以下脚本
~~~
vi /etc/init.d/nginx
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
chmod 775 /etc/rc.d/init.d/nginx
chkconfig nginx on
/etc/rc.d/init.d/nginx restart
service nginx restart
~~~
## 10、安装libmcrypt
~~~
cd /usr/local/src
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure
make
nake install
~~~
## 11、安装PHP
~~~
cd /usr/local/src
tar -zvxf php-5.5.10.tar.gz
cd php-5.5.10
mkdir -p /usr/local/php5
./configure --prefix=/usr/local/php5 --with-config-file-path=/usr/local/php5/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-gd --with-iconv --with-zlib --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-pear --with-gettext --enable-session --with-mcrypt --with-curl
make
make install
cp php.ini-production /usr/local/php5/etc/php.ini
rm -rf /etc/php.ini
ln -s /usr/local/php5/etc/php.ini /etc/php.ini
cp /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf
vi /usr/local/php5/etc/php-fpm.conf
user = www #设置php-fpm运行账号为www //在5.5版本此处不存在 不需要配置 在5.3版本需要配置
group = www #设置php-fpm运行组为www //在5.5版本此处不存在 不需要配置 在5.3版本需要配置
pid = run/php-fpm.pid #取消前面的分号
cp /usr/local/src/php-5.5.10/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm#拷贝php-fpm到启动目录
chmod +x /etc/rc.d/init.d/php-fpm #添加执行权限
chkconfig php-fpm on #设置开机启动
vi /usr/local/php5/etc/php.ini #编辑配置文件
修改为:date.timezone = PRC #设置时区
~~~
## 12、配置nginx支持php
~~~
vi /usr/local/nginx/conf/nginx.conf #编辑配置文件,需做如下修改
user www www; #首行user去掉注释,修改Nginx运行组为www www;必须与/usr/local/php5/etc/php-fpm.conf中的user,group配置相同,否则php运行出错
index index.php index.html index.htm; #添加index.php
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#取消FastCGI server部分location的注释,并要注意fastcgi_param行的参数,改为
$document_root$fastcgi_script_name,或者使用绝对路径
/etc/init.d/nginx restart #重启nginx
~~~
## 13、测试
~~~
cd /usr/local/nginx/html/ #进入nginx默认网站根目录
rm -rf /usr/local/nginx/html/* #删除默认测试页
vi index.php #编辑
phpinfo();
chown www.www /usr/local/nginx/html/ -R #设置目录所有者
chmod 700 /usr/local/nginx/html/ -R #设置目录权限
shutdown -r now #重启系统
~~~
## 14、相关命令
~~~
service nginx restart #重启nginx
service mysqld restart #重启mysql
/usr/local/php5/sbin/php-fpm #启动php-fpm
/etc/rc.d/init.d/php-fpm restart #重启php-fpm
/etc/rc.d/init.d/php-fpm stop #停止php-fpm
~~~
# 一键搭建lnmp环境
**1、登录网站查看组新版本当然此处也有安装方法**
http://lnmp.org/install.html
安装LNMP执行:
`wget -c http://soft.vpser.net/lnmp/lnmp1.2-full.tar.gz && tar zxf lnmp1.2-full.tar.gz && cd lnmp1.2-full && ./install.sh lnmp`
如需要安装LNMPA或LAMP,将./install.sh 后面的参数替换为lnmpa或lamp即可。
如下载速度慢请更换其他下载节点,详情请看下载页面。LNMP下载节点具体替换方法。
按上述命令执行后,会出现如下提示:
1、需要设置MySQL的root密码(不输入直接回车将会设置为root),输入后回车进入下一步,如下图所示:
![document/2015-09-16/55f908c8286d2](https://box.kancloud.cn/document_2015-09-16_55f908c8286d2.png)
这里需要确认是否启用MySQL InnoDB,如果不确定是否启用可以输入 y ,输入 y 表示启用,输入 n 表示不启用。默认为y 启用,输入后回车进入下一步,选择MySQL版本:
![document/2015-09-16/55f9093a43611](https://box.kancloud.cn/document_2015-09-16_55f9093a43611.png)
输入MySQL或MariaDB版本的序号,回车进入下一步,选择PHP版本:
![document/2015-09-16/55f90952bd0ae](https://box.kancloud.cn/document_2015-09-16_55f90952bd0ae.png)
输入PHP版本的序号,回车进入下一步,选择是否安装内存优化:
![document/2015-09-16/55f909700a43c](https://box.kancloud.cn/document_2015-09-16_55f909700a43c.png)
可以选择不安装、Jemalloc或TCmalloc,输入对应序号回车。
3、安装完成
如果显示Nginx: OK,MySQL: OK,PHP: OK
## **此处是安装nginx虚拟服务器的配置**
~~~
server
{
listen 80;
#listen [::]:80;
server_name haoli.com hl.com;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/hlsc;
include none.conf;
#error_page 404 /404.html;
location /{
if (-f $request_filename) {
expires max;
break;
}
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
}
}
location ~ \.php($|/)
{
fastcgi_pass unix:/tmp/php-cgi.sock;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
access_log off;
}
~~~