CentOS7.2编译安装LNMP
一、前言
1、环境说明
基础环境Linux+Nginx+MySQL+PHP
Linux:CentOS7.2
Nginx:1.10.2
MySQL:5.6.17
PHP:5.5.12
2、部署说明
php安装目录:/usr/local/php
php.ini配置文件路径:/usr/local/php/etc/php.ini
Nginx安装目录:/usr/local/nginx
Nginx配置文件路径:/etc/nginx/nginx.conf
Nginx网站根目录:/usr/local/nginx/html
二、准备工作
1、最小化安装Centos7.2
#建立一个软件包目录存放
mkdir -p /usr/local/src/
#清理已经安装包
rpm -e httpd
rpm -e mysql
rpm -e php
yum -y remove httpd
yum -y remove mysql
yum -y remove php
#搜索apache包
rpm -qa http*
#强制卸载apache包
rpm -e --nodeps 查询出来的文件名
#检查是否卸载干净
rpm -qa|grep http*
selinux可能会致使编译安装失败,我们先禁用它。
永久禁用,需要重启生效
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
临时禁用,不需要重启
1 setenforce 0
2、安装必备工具
yum -y install make gcc gcc-c++ gcc-g77 flex bison file libtool libtool-libs autoconf kernel-devel libjpeg libjpeg-devel libpng libpng-devel libpng10 libpng10-devel gd gd-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel libevent libevent-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel gettext gettext-devel ncurses-devel gmp-devel pspell-devel unzip libcap lsof
三、安装MySQL5.6.17
1、准备工作
按照标准需要给mysql创建所属用户和用户组
#创建群组
groupadd mysql
#创建一个用户,不允许登陆和不创主目录
useradd -s /sbin/nologin -g mysql -M mysql
#检查刚创建的用户
tail -1 /etc/passwd
centos最小化安装后,会有mysql的库因此先卸载!
#检查安装与否
rpm -qa|grep mysql
#强制卸载
rpm -erpm -e mysql-libs-5.1.73-3.el6_5.x86_64 --nodeps
MySQL从5.5版本开始,通过./configure进行编译配置方式已经被取消,取而代之的是cmake工具。因此,我们首先要在系统中源码编译安装cmake工具。
wgethttp://www.cmake.org/files/v2.8/cmake-2.8.12.2.tar.gz
tar zxvf cmake-2.8.12.2.tar.gz
cd cmake-2.8.12.2
./configure
make && make install
2、使用cmake来编译安装MySQL 5.6.17
wgethttp://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.17.tar.gz
tar zxvf mysql-5.6.17.tar.gz
cd mysql-5.6.17
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DMYSQL_USER=mysql \
-DWITH_DEBUG=0 \
-DWITH_SSL=system
make && make install
#修改/usr/local/mysql权限
chmod +w /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
关于my.cnf配置文件:
在启动MySQL服务时,会按照一定次序搜索my.cnf,先在/etc目录下找,找不到则会搜索”$basedir/my.cnf”就是安装目录下/usr/local/mysql/my.cnf,这是新版MySQL的配置文件的默认位置!注意:在最小安装完成后,在/etc目录下会存在一个my.cnf,需要将此文件更名为其他的名字。如:/etc/my.cnf.bak,否则,该文件会干扰源码安装的MySQL的正确配置,造成无法启动。由于我们已经卸载了最小安装完成后的mysql库所以,就没必要操作了。
cd support-files/
#如果还有my.cnf请备份
mv /etc/my.cnf /etc/my.cnf.bak
cp my-default.cnf /etc/my.cnf
#执行初始化配置脚本,创建系统自带的数据库和表,注意配置文件的路径
/usr/local/mysql/scripts/mysql_install_db--defaults-file=/etc/my.cnf --basedir=/usr/local/mysql--datadir=/usr/local/mysql/data --user=mysql
#拷贝mysql安装目录下support-files服务脚本到init.d目录
cp support-files/mysql.server /etc/init.d/mysqld
#赋予权限、设置开机自启并启动
chmod +x /etc/init.d/mysqld
chkconfig mysqld on
service mysqld start
#或者
/etc/init.d/mysql start
MySQL5.6.x启动成功后,root默认没有密码,我们需要设置root密码。设置之前,我们需要先设置PATH,否则不能直接调用mysql
#修改/etc/profile文件
vim /etc/profile
#在文件末尾添加
PATH=/usr/local/mysql/bin:$PATH
export PATH
#让配置立即生效
source /etc/profile
#登陆测试,默认是没有密码,直接回车就可进入
mysql -uroot -p
#设置mysql密码
/usr/local/mysql/bin/mysqladmin -uroot -p password '你的密码'
#登陆进命令行模式
mysql -uroot -p
#查看用户
>select user,host from mysql.user;
#删除不必要的用户
>drop user ""@localhost;
> drop user root@'::1';
#赋予账号远程访问的权限
>GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY '你的密码';
>GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '你的密码';
#其它一些信息查询:
#检查mysql版本
mysql -uroot -p"密码" -e "select version();"
MySQL安装完毕!
四、安装PHP5.5.12
1、安装依赖关系
yum install libmcrypt libmcrypt-devel mcrypt mhash
2、下载并编译安装php
wget http://mirrors.sohu.com/php/php-5.5.12.tar.gz
tar zxvf php-5.5.12.tar.gz
cd php-5.5.12
./configure --prefix=/usr/local/php--with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www--with-fpm-group=www --with-mysql=mysqlnd --with-mysqli=mysqlnd--with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir--with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath--enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop--enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers--enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd--enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl--enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear--with-gettext --disable-fileinfo --enable-maintainer-zts
make && make install
下面的警告说明已自带不需启用或安装,可忽略:
继续:
#修改fpm配置php-fpm.conf.default文件名称
mv /usr/local/php/etc/php-fpm.conf.default/usr/local/php/etc/php-fpm.conf
#复制php.ini配置文件
cp php.ini-production /usr/local/php/etc/php.ini
#复制php-fpm启动脚本到init.d
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
#赋予执行权限
chmod +x /etc/init.d/php-fpm
#添加为启动项
chkconfig --add php-fpm
#设置开机启动
chkconfig php-fpm on
#按照标准,给php-fpm创建一个指定的用户和组
#创建群组
groupadd www
#创建一个用户,不允许登陆和不创主目录
useradd -s /sbin/nologin -g www -M www
#立即启动php-fpm
/etc/init.d/php-fpm start
PHP安装完毕!
五、安装Nginx1.10.2
1、准备工作
#添加用户与属组
groupadd -r nginx
useradd -s /sbin/nologin -g nginx -M nginx
#创建目录
mkdir -pv /var/tmp/nginx/client/
#解压安装包
tar zxvf nginx-1.10.2.tar.gz
2、编译安装
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/bin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre
敲黑板:注意sbin、conf、pid、local的path要和后续的控制脚本保持一致!
make && make install
3、为Nginx提供SysV init脚本
cat /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /run/nginx/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf "
lockfile=/var/lock/nginx.lock
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
类似的脚本网上很多,注意config、pid、sbin的路径保持和编译一致即可。
chmod +x /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
#然后就可以启动服务了
systemctl restart nginx.service
#或
/etc/init.d/nginx start
Nginx安装完毕 ,下面开始整合Nginx与PHP
六、整合Nginx与PHP
1、编辑Nginx.conf,启用如下选项:
cd /usr/local/nginx/conf/
cat nginx.conf
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
2、编辑/etc/nginx/fastcgi_params,将其内容更改为如下内容:
# mv fastcgi_paramsfastcgi_params.bak
# cat fastcgi_params
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
在所支持的主页面格式中添加php格式的主页,类似如下:
location / {
root html;
index index.php index.html index.htm;
}
#重新载入配置
# /etc/init.d/nginx reload
3、在/usr/local/nginx/html目录下新建index.php测试页面
cat > /usr/local/nignx/htmlindex.php << EOF
<?php
phpinfo();
?>
可通过浏览器 访问此测试页面,验证nginx连接php配置是否成功。页面应该如下图
至此CenOS72下LNMP编译安装完毕!
LNMP安装参考如下文章