这篇笔记记录了在CentOS6.9中源码编译安装nginx1.14,php7,mysql5.6的过程,并附上启动脚本,记录了集成的过程,mysql5.6并未使用官方二进制包,而是自己通过源码编译安装的
相关笔记:
CentOS7源码编译安装nginx+php7.2+mysql5.7并使用systemctl管理
CentOS7yum安装nginx+php7+mysql
CentOS6.9yum安装nginx+php7+mysql环境
安装nginx
1.安装nginx所需依赖
yum install wget gcc gcc-c++ pcre pcre-devel openssl openssl-devel zlib zlib-devel
安装完后的大致结果
......
已安装:
gcc.x86_64 0:4.4.7-23.el6 gcc-c++.x86_64 0:4.4.7-23.el6 openssl-devel.x86_64 0:1.0.1e-57.el6 pcre-devel.x86_64 0:7.8-7.el6 wget.x86_64 0:1.12-10.el6 zlib-devel.x86_64 0:1.2.3-29.el6
作为依赖被安装:
cloog-ppl.x86_64 0:0.15.7-1.2.el6 cpp.x86_64 0:4.4.7-23.el6 glibc-devel.x86_64 0:2.12-1.212.el6 glibc-headers.x86_64 0:2.12-1.212.el6
kernel-headers.x86_64 0:2.6.32-754.9.1.el6 keyutils-libs-devel.x86_64 0:1.4-5.el6 krb5-devel.x86_64 0:1.10.3-65.el6 libcom_err-devel.x86_64 0:1.41.12-24.el6
libgomp.x86_64 0:4.4.7-23.el6 libkadm5.x86_64 0:1.10.3-65.el6 libselinux-devel.x86_64 0:2.0.94-7.el6 libsepol-devel.x86_64 0:2.0.41-4.el6
libstdc++-devel.x86_64 0:4.4.7-23.el6 mpfr.x86_64 0:2.4.1-6.el6 ppl.x86_64 0:0.10.2-11.el6
完毕!
[root@jmsite ~]#
2.创建一个不能登录的nginx启动用户
groupadd www-data
useradd -s /sbin/nologin -g www-data www-data
3.创建源码保存目录,下载nginx源码,当前稳定版为nginx-1.14.2
mkdir -p /usr/local/src
cd /usr/local/src
wget -c http://nginx.org/download/nginx-1.14.2.tar.gz
4.解压
tar -zxvf nginx-1.14.2.tar.gz
cd /usr/local/src/nginx-1.14.2
5.编译前配置
./configure --prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/var/run/nginx/nginx.pid \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--user=www-data \
--group=www-data \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-pcre
# --with-http_stub_status_module 监控nginx状态
# --with-http_ssl_module 支持ssl
# --with-http_gzip_static_module 静态压缩
6.编译,安装
make
make install
7.启动并查看nginx进程
[root@jmsite nginx-1.14.2]# /usr/local/nginx/sbin/nginx
[root@jmsite nginx-1.14.2]# ps aux | grep nginx
root 4275 0.0 0.1 46856 1192 ? Ss 00:08 0:00 nginx: master process /usr/local/nginx/sbin/nginx
www-data 4276 0.0 0.1 47288 1772 ? S 00:08 0:00 nginx: worker process
root 4278 0.0 0.0 103336 900 pts/1 S+ 00:08 0:00 grep nginx
查看nginx版本
[root@jmsite ~]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.14.2
8.创建nginx启动脚本(查看官方脚本)
vim /etc/init.d/nginx
写入以下脚本信息
#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/var/run/nginx/$NAME.pid
if [ -s /bin/ss ]; then
StatBin=/bin/ss
else
StatBin=/bin/netstat
fi
case "$1" in
start)
echo -n "Starting $NAME... "
if $StatBin -tnpl | grep -q nginx;then
echo "$NAME (pid `pidof $NAME`) already running."
exit 1
fi
$NGINX_BIN -c $CONFIGFILE
if [ "$?" != 0 ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
stop)
echo -n "Stoping $NAME... "
if ! $StatBin -tnpl | grep -q nginx; then
echo "$NAME is not running."
exit 1
fi
$NGINX_BIN -s stop
if [ "$?" != 0 ] ; then
echo " failed. Use force-quit"
exit 1
else
echo " done"
fi
;;
status)
if $StatBin -tnpl | grep -q nginx; then
PID=`pidof nginx`
echo "$NAME (pid $PID) is running..."
else
echo "$NAME is stopped."
exit 0
fi
;;
force-quit|kill)
echo -n "Terminating $NAME... "
if ! $StatBin -tnpl | grep -q nginx; then
echo "$NAME is is stopped."
exit 1
fi
kill `pidof $NAME`
if [ "$?" != 0 ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
restart)
$0 stop
sleep 1
$0 start
;;
reload)
echo -n "Reload service $NAME... "
if $StatBin -tnpl | grep -q nginx; then
$NGINX_BIN -s reload
echo " done"
else
echo "$NAME is not running, can't reload."
exit 1
fi
;;
configtest)
echo -n "Test $NAME configure files... "
$NGINX_BIN -t
;;
*)
echo "Usage: $0 {start|stop|restart|reload|status|configtest|force-quit|kill}"
exit 1
;;
esac
9.设置nginx开机启动
# 修改脚本执行权限
chmod 755 /etc/init.d/nginx
# nginx加入服务
chkconfig --add nginx
# nginx 设置为开机启动
chkconfig nginx on
#测试一下
service nginx status
nginx详细配置请移步(nginx的configure参数,配置文件,虚拟主机配置,信号控制)
安装mysql
1.创建mysql用户,不允许登陆和不创建主目录
groupadd mysql
useradd -s /sbin/nologin -g mysql -M mysql
2.创建mysql相应目录,并设置权限
mkdir -p /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
mkdir -p /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql
mkdir -p /var/run/mysqld
chown -R mysql:mysql /var/run/mysqld
注:MySQL从5.5版本开始,通过./configure进行编译配置方式已经被取消,取而代之的是cmake工具。因此,我们首先要在系统中源码编译安装cmake工具。
3.安装依赖
yum install make cmake bison-devel ncurses-devel
4.进入源码存放目录,下载,解压mysql
cd /usr/local/src/
wget -c https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.37.tar.gz
tar -zxvf mysql-5.6.37.tar.gz
cd mysql-5.6.37
5.编译前配置(查看官方文档)
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
......
CMake Warning:
Manually-specified variables were not used by the project:
MYSQL_USER
WITH_MEMORY_STORAGE_ENGINE
WITH_READLINE
-- Build files have been written to: /usr/local/src/mysql-5.6.37
#配置完毕,CMake Warning可以略过
6.编译,安装(-j4:表示cpu核心数,我的虚拟机设置4核,所以-j4)
make -j4
make install
7.执行初始化配置脚本,创建系统自带的数据库和表
/usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
8.设置mysql启动脚本,将mysql.server复制到init.d目录下
cp support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
chkconfig mysqld on
service mysqld start
9.如果出现如下错误提示则修改my.cnf中datadir为你的数据目录
[root@jmsite mysql-5.6.37]# service mysqld start
Starting MySQL.181220 05:27:46 mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists.
ERROR! The server quit without updating PID file (/var/lib/mysql/jmsite.cn.pid).
[root@jmsite mysql-5.6.37]# vim /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
......
[root@jmsite mysql-5.6.37]# service mysqld start
Starting MySQL. SUCCESS!
10.将mysql加入环境变量,并立刻生效
vim /etc/profile
#尾部加入下面两行
PATH=$PATH:/usr/local/mysql/bin/
export PATH
#执行source使环境变量立即生效
source /etc/profile
11.测试一下
[root@jmsite mysql-5.6.37]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.37 Source distribution
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql>
12.打开log
tail -f /var/log/mysqld.log
......
2018-12-20 05:13:34 16977 [Note] RSA private key file not found: /usr/local/mysql/data//private_key.pem. Some authentication plugins will not work.
2018-12-20 05:13:34 16977 [Note] RSA public key file not found: /usr/local/mysql/data//public_key.pem. Some authentication plugins will not work.
......
13.发现没有公钥和私钥,开始生成
cd /usr/local/mysql/data
openssl genrsa -out private_key.pem 1024
openssl rsa -in private_key.pem -pubout > public_key.pem
安装php
安装php依赖(gcc gcc-c++ openssl openssl-devel等依赖在上面安装nginx时已安装)
yum install libxml2 libxml2-devel curl-devel openjpeg openjpeg-devel openjpeg-libs libjpeg libjpeg-devel libpng freetype libpng-devel freetype-devel
下载,解压源码
cd /usr/local/src/
wget -c http://cn2.php.net/get/php-7.2.13.tar.gz
tar -xzvf php-7.2.13.tar.gz
cd php-7.2.13
编译前配置
./configure --prefix=/usr/local/php72 \
--with-config-file-path=/usr/local/php72/etc \
--with-config-file-scan-dir=/usr/local/php72/etc/php.d \
--with-mhash \
--disable-debug \
--disable-rpath \
--enable-mysqlnd \
--with-mysqli \
--with-pdo-mysql \
--enable-fpm \
--with-fpm-user=www-data \
--with-fpm-group=www-data \
--with-gd \
--with-iconv \
--with-zlib \
--enable-bcmath \
--enable-xml \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--with-openssl \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--without-pear \
--with-gettext \
--enable-session \
--with-curl \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--enable-opcache
编译,安装
make -j4
make install
设置环境变量
vim /etc/profile
#文件末尾加入如下两行代码
PATH=$PATH:/usr/local/php72/bin/:/usr/local/php72/sbin/
export PATH
#使之立即生效
source /etc/profile
#测试一下
[root@jmsite php-fpm.d]# php -v
PHP 7.2.13 (cli) (built: Dec 20 2018 07:41:00) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
设置php.ini和php-fpm.conf,www.conf
#进入源码目录
cd /usr/local/src/php-7.2.13
#复制示例配置
cp php.ini-development /usr/local/php72/etc/php.ini
#或
cp php.ini-production /usr/local/php72/etc/php.ini
#进入php.ini目录
cd /usr/local/php72/etc
#打开配置文件
vim /usr/local/php72/etc/php.ini
#更改pdo_mysql.default_socket为上面安装mysql时.sock设定的位置
pdo_mysql.default_socket = /var/lib/mysql/mysql.sock
#如果不设置,php通过pdo连接mysql时会报SQLSTATE[HY000] [2002] No such file or directory
#复制fpm示例配置
cp php-fpm.conf.default php-fpm.conf
#进入php-fpm.d目录
cd /usr/local/php72/etc/php-fpm.d
#复制www.conf
cp www.conf.default www.conf
复制php-fpm启动脚本
cp /usr/local/src/php-7.2.13/sapi/fpm/init.d.php-fpm /etc/init.d/php72-fpm
#设置权限
chmod 755 /etc/init.d/php72-fpm
#作为一项新的系统服务添加
chkconfig --add php72-fpm
#设置开机启动
chkconfig php72-fpm on
#测试一下
[root@jmsite ~]# service php72-fpm start
Starting php-fpm done
[root@jmsite ~]# service php72-fpm status
php-fpm (pid 1516) is running...
验证安装的nginx,php,mysql
1.编辑nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
#更改运行用户
user www-data;
#编辑server段,默认文件添加index.php
location / {
root html;
index index.php index.html index.htm;
}
#匹配php的配置块取消注释并更改/scripts为$document_root
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;
}
2.保存并退出,重启加载nginx配置
service nginx reload
3.nginx默认的web目录下新建index.php
vim /usr/local/nginx/html/index.php
4.输入如下php代码
$dbms='mysql'; //数据库类型
$host='localhost'; //数据库主机名
$dbName='mysql'; //使用的数据库
$user='root'; //数据库连接用户名
$pass=''; //对应的密码
$dsn="$dbms:host=$host;dbname=$dbName";
try {
$dbh = new PDO($dsn, $user, $pass); //初始化一个PDO对象
echo "连接成功
";
foreach ($dbh->query('SELECT db from db') as $row) {
print_r($row);
}
$dbh = null;
} catch (PDOException $e) {
die ("Error!: " . $e->getMessage() . "
");
}
?>
5.浏览器访问你的站点,如果看到下图的内容,说明你成功了!!!
原文地址:https://www.jmsite.cn/blog-155.html