CentOS7源码编译安装nginx+php7.2+mysql5.7并使用systemctl管理

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
云数据库 RDS MySQL,高可用系列 2核4GB
简介: 这篇笔记记录了在CentOS7.6中通过源码编译的方式安装nginx1.14,php7.2和mysql5.7的过程,以及使用systemctl管理服务,mysql5.7并未使用官网的二进制包,而是从源码开始自己编译的,要提醒的是mysql5.

相关笔记:
CentOS7yum安装nginx+php7+mysql
CentOS6.9源码编译安装nginx+php7+mysql环境
CentOS6.9yum安装nginx+php7+mysql环境
1.安装nginx
安装依赖

yum -y install gcc gcc-c++ wget automake autoconf libtool libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed pcre-devel openssl  openssl-devel

创建一个不能登录的nginx运行用户

groupadd www-data
useradd -s /sbin/nologin -g www-data www-data

创建源码保存目录和运行时的临时文件夹,下载nginx源码,当前稳定版为nginx-1.14.2

mkdir -p /var/cache/nginx
mkdir -p /usr/local/src/nginx
cd /usr/local/src/nginx
wget -c http://nginx.org/download/nginx-1.14.2.tar.gz

解压

tar -zxvf nginx-1.14.2.tar.gz
cd /usr/local/src/nginx/nginx-1.14.2

编译前配置检查

./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/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.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--user=www-data \
--group=www-data \
--with-pcre \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-http_v2_module \
--with-threads \
--with-stream \
--with-stream_ssl_module

配置检查完毕,已经创建了Makefile

creating objs/Makefile

Configuration summary
  + using threads
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/var/run/nginx.pid"
  nginx error log file: "/var/log/nginx/error.log"
  nginx http access log file: "/var/log/nginx/access.log"
  nginx http client request body temporary files: "/var/cache/nginx/client_temp"
  nginx http proxy temporary files: "/var/cache/nginx/proxy_temp"
  nginx http fastcgi temporary files: "/var/cache/nginx/fastcgi_temp"
  nginx http uwsgi temporary files: "/var/cache/nginx/uwsgi_temp"
  nginx http scgi temporary files: "/var/cache/nginx/scgi_temp"

编译,安装

make
make install

修改配置
nginx详细配置请移步(nginx的configure参数,配置文件,虚拟主机配置,信号控制)

vim /usr/local/nginx/conf/nginx.conf
user  www-data;
pid   /var/run/nginx.pid;

启动nginx

/usr/local/nginx/sbin/nginx

查看进程

ps aux|grep nginx

杀掉进程

pkill -9 nginx

配置服务

vim /usr/lib/systemd/system/nginx.service

输入如下配置

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
ExecStartPost=/bin/sleep 0.1

[Install]
WantedBy=multi-user.target

注意:如果你是单核服务器,需要加ExecStartPost=/bin/sleep 0.1

否则在执行systemctl start nginx之后,在执行systemctl status nginx时

会出现systemd[1]: Failed to read PID from file /var/run/nginx.pid: Invalid argument

这是因为nginx还未启动完成,systemctl就去寻找pid进程文件了,这是一个已知的bug

地址https://bugs.launchpad.net/ubuntu/+source/nginx/+bug/1581864

重新载入 systemd

systemctl daemon-reload

开启开机启动

systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

启动和查看nginx状态

systemctl start nginx
systemctl status nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since 五 2019-01-04 18:24:18 CST; 9min ago
     Docs: http://nginx.org/en/docs/
  Process: 75573 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)
  Process: 75576 ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 75577 (nginx)
   CGroup: /system.slice/nginx.service
           ├─75577 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
           └─75578 nginx: worker process

1月 04 18:24:18 jmsiteos7 systemd[1]: Stopping nginx - high performance web server...
1月 04 18:24:18 jmsiteos7 systemd[1]: Stopped nginx - high performance web server.
1月 04 18:24:18 jmsiteos7 systemd[1]: Starting nginx - high performance web server...
1月 04 18:24:18 jmsiteos7 systemd[1]: Started nginx - high performance web server.

2.安装mysql
注:MySQL从5.5版本开始,通过./configure进行编译配置方式已经被取消,取而代之的是cmake工具。因此,我们首先要在系统中源码编译安装cmake工具。
安装依赖

yum install -y cmake gcc-c++ bison bison-devel ncurses-devel perl-Data-Dumper boost boost-doc boost-devel

创建mysql用户和用户组

groupadd mysql
useradd -g mysql mysql

创建源码存放目录,下载,解压

mkdir -p /usr/local/src/mysql
cd /usr/local/src/mysql
wget -c https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.20.tar.gz
tar -zxvf mysql-boost-5.7.20.tar.gz
cd mysql-5.7.20

创建相应目录并授权

mkdir -p /usr/local/mysql/data
chown -R mysql:mysql /usr/local/mysql/
touch /var/log/mysql.log
chown -R mysql:mysql /var/log/mysql.log
mkdir -p /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql
mkdir -p /var/run/mysqld
chown -R mysql:mysql /var/run/mysqld

配置检查

cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data  \
-DSYSCONFDIR=/etc \
-DMYSQL_USER=mysql \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_unicode_ci \
-DWITH_DEBUG=0 \
-DMYSQL_MAINTAINER_MODE=0 \
-DWITH_SYSTEMD=1 \
-DWITH_BOOST=boost \
-DWITH_SSL:STRING=bundled \
-DWITH_ZLIB:STRING=bundled

编译,安装

make
make install

修改mysql配置

vim /etc/my.cnf

如下设置

[mysql]
default-character-set=utf8
socket=/var/lib/mysql/mysql.sock

[mysqld]
init-connect = 'SET NAMES utf8'
character-set-server = utf8
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/var/lib/mysql/mysql.sock
user = mysql
explicit_defaults_for_timestamp=true
bind-address = 0.0.0.0
server-id = 1
log_error=/var/log/mysql.log


# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

加入环境变量

vim /etc/profile

尾部加入下面两行

PATH=$PATH:/usr/local/mysql/bin/
export PATH

执行source使环境变量立即生效

source /etc/profile

初始化数据库,安装密钥

mysqld --defaults-file=/etc/my.cnf --initialize-insecure --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
mysql_ssl_rsa_setup

--initialize-insecure不生成随机密码

--initialize生成随机密码

设置开机启动

cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system

重新载入 systemd

systemctl daemon-reload

开机启动

systemctl enable mysqld.service
systemctl start mysqld.service
[root@jmsiteos7 mysql-5.7.20]# systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 五 2019-01-04 15:36:52 CST; 3s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 30737 ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 30719 ExecStartPre=/usr/local/mysql/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 30740 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─30740 /usr/local/mysql/bin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

1月 04 15:36:51 jmsiteos7 systemd[1]: Starting MySQL Server...
1月 04 15:36:52 jmsiteos7 systemd[1]: Started MySQL Server.

设置密码(如果初始化参数是--initialize,这步跳过)

mysql_secure_installation

登录mysql

[root@jmsiteos7 ~]# mysql -uroot -p
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.20 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 |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> set password for root@localhost = password('yourpassword');
mysql> flush privileges;
mysql> quit;
Bye

3.安装php
安装依赖

yum install libxml2 libxml2-devel curl-devel openjpeg openjpeg-devel openjpeg-libs libjpeg libjpeg-devel libpng freetype libpng-devel freetype-devel openssl openssl-devel

创建源码保存目录,下载,解压

mkdir -p /usr/local/src/php72
cd /usr/local/src/php72
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

编译,安装

make -j4
make install

设置环境变量

vim /etc/profile

文件末尾加入如下两行代码

PATH=$PATH:/usr/local/php72/bin/:/usr/local/php72/sbin/
export PATH

使之立即生效

source /etc/profile

测试一下

[root@jmsiteos7 php-7.2.13]# php -v
PHP 7.2.13 (cli) (built: Jan  4 2019 17:35:17) ( 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/php72/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/php72/php-7.2.13/sapi/fpm/php-fpm.service /usr/lib/systemd/system/

重新载入 systemd

systemctl daemon-reload

启用开机启动

systemctl enable php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.

启动php-fpm

systemctl start php-fpm

查看状态

[root@jmsiteos7 php-fpm.d]# systemctl status php-fpm
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
   Active: active (running) since 五 2019-01-04 17:55:16 CST; 5s ago
 Main PID: 70669 (php-fpm)
   CGroup: /system.slice/php-fpm.service
           ├─70669 php-fpm: master process (/usr/local/php72/etc/php-fpm.conf)
           ├─70670 php-fpm: pool www
           └─70671 php-fpm: pool www

1月 04 17:55:16 jmsiteos7 systemd[1]: Started The PHP FastCGI Process Manager.

4.验证安装的nginx,php,mysql
编辑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;
        }

保存并退出,重启nginx

systemctl restart nginx

nginx默认的web目录下新建index.php

vim /usr/local/nginx/html/index.php

输入如下php代码

<?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 "连接成功<br/>";
    foreach ($dbh->query('SELECT db from db') as $row) {
        print_r($row);
    }

    $dbh = null;
} catch (PDOException $e) {
    die ("Error!: " . $e->getMessage() . "<br/>");
}
?>

浏览器访问你的站点

原文地址:https://www.jmsite.cn/blog-310.html

相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
18天前
|
Linux 编译器 开发工具
在CentOS上编译安装TinyXml2
以上就是在CentOS上编译安装TinyXml2的步骤。这个过程可能看起来有些复杂,但是只要你按照步骤一步步来,应该不会有太大问题。如果在过程中遇到任何问题,你可以查阅相关的文档,或者在网上搜索解决方案。记住,编程就是一个不断学习和解决问题的过程,不要因为遇到困难就放弃。
56 27
|
1月前
|
关系型数据库 MySQL Linux
CentOS 7系统下详细安装MySQL 5.7的步骤:包括密码配置、字符集配置、远程连接配置
以上就是在CentOS 7系统下安装MySQL 5.7的详细步骤。希望这个指南能帮助你顺利完成安装。
274 26
|
1月前
|
Linux 开发工具 C语言
在CentOS系统中编译安装TinyXml2的方法
以上就是在CentOS系统中编译安装TinyXml2的方法。这个过程可能会有些复杂,但只要你按照上面的步骤一步一步来,你应该能够成功地完成这个任务。如果你在这个过程中遇到任何问题,你可以在网上搜索相关的资料,或者在相关的论坛上寻求帮助。希望这个指南能够帮助你成功地在你的CentOS系统中编译安装TinyXml2。
74 22
|
2月前
|
Linux Python
centos 编译安装 python 和 openssl
centos 编译安装 python 和 openssl
71 3
|
3月前
|
监控 Linux PHP
【02】客户端服务端C语言-go语言-web端PHP语言整合内容发布-优雅草网络设备监控系统-2月12日优雅草简化Centos stream8安装zabbix7教程-本搭建教程非docker搭建教程-优雅草solution
【02】客户端服务端C语言-go语言-web端PHP语言整合内容发布-优雅草网络设备监控系统-2月12日优雅草简化Centos stream8安装zabbix7教程-本搭建教程非docker搭建教程-优雅草solution
113 20
|
6月前
|
关系型数据库 MySQL Linux
在 CentOS 7 中通过编译源码方式安装 MySQL 数据库的详细步骤,包括准备工作、下载源码、编译安装、配置 MySQL 服务、登录设置等。
本文介绍了在 CentOS 7 中通过编译源码方式安装 MySQL 数据库的详细步骤,包括准备工作、下载源码、编译安装、配置 MySQL 服务、登录设置等。同时,文章还对比了编译源码安装与使用 RPM 包安装的优缺点,帮助读者根据需求选择最合适的方法。通过具体案例,展示了编译源码安装的灵活性和定制性。
577 2
|
15天前
|
负载均衡 算法 关系型数据库
大数据大厂之MySQL数据库课程设计:揭秘MySQL集群架构负载均衡核心算法:从理论到Java代码实战,让你的数据库性能飙升!
本文聚焦 MySQL 集群架构中的负载均衡算法,阐述其重要性。详细介绍轮询、加权轮询、最少连接、加权最少连接、随机、源地址哈希等常用算法,分析各自优缺点及适用场景。并提供 Java 语言代码实现示例,助力直观理解。文章结构清晰,语言通俗易懂,对理解和应用负载均衡算法具有实用价值和参考价值。
大数据大厂之MySQL数据库课程设计:揭秘MySQL集群架构负载均衡核心算法:从理论到Java代码实战,让你的数据库性能飙升!
|
2月前
|
关系型数据库 MySQL Java
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
|
15天前
|
存储 关系型数据库 MySQL
大数据新视界 --面向数据分析师的大数据大厂之 MySQL 基础秘籍:轻松创建数据库与表,踏入大数据殿堂
本文详细介绍了在 MySQL 中创建数据库和表的方法。包括安装 MySQL、用命令行和图形化工具创建数据库、选择数据库、创建表(含数据类型介绍与选择建议、案例分析、最佳实践与注意事项)以及查看数据库和表的内容。文章专业、严谨且具可操作性,对数据管理有实际帮助。
大数据新视界 --面向数据分析师的大数据大厂之 MySQL 基础秘籍:轻松创建数据库与表,踏入大数据殿堂
|
2月前
|
关系型数据库 MySQL 数据库连接
docker拉取MySQL后数据库连接失败解决方案
通过以上方法,可以解决Docker中拉取MySQL镜像后数据库连接失败的常见问题。关键步骤包括确保容器正确启动、配置正确的环境变量、合理设置网络和权限,以及检查主机防火墙设置等。通过逐步排查,可以快速定位并解决连接问题,确保MySQL服务的正常使用。
440 82