首页
学
【学】PHP
【学】前端
【学】Linux
【学】杂学
【学】支付
【学】Docker
享
【享】文件
【享】工具
日记
Me
【Me】我
【Me】留言
老郭博客-程序员客栈
文章模型
下载模型
首页
学
【学】PHP
【学】前端
【学】Linux
【学】杂学
【学】支付
【学】Docker
享
【享】文件
【享】工具
日记
Me
【Me】我
【Me】留言
首页
学
【学】Linux
Centos7.4编译安装Nginx_1.13.8、php_7.2.1、mysql_5.7.21、redis_4.0.6
Centos7.4编译安装Nginx_1.13.8、php_7.2.1、mysql_5.7.21、redis_4.0.6
发布时间:
11个月前
作者:
老郭
热度:
447 ℃
评论数:
# 前言 本文章运用了一些centos7 FireWall (防火墙)的知识,未了解的朋友请先阅读[Centos7使用FireWall开放8888端口](https://www.phpsix.com/word/v1xXKWOr.html "Centos7使用FireWall开放8888端口") # centos_7 编译安装 nginx_1.13.8 服务器 ### 安装系统必要编译依赖 ```shell yum install -y gcc-c++ gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel autoconf automake make yum install wget yum install -y openssl* yum -y install ncurses-devel ``` ### 下载 nginx 最新版 ```shell wget -c https://nginx.org/download/nginx-1.13.8.tar.gz ``` ### 解压 ``` tar -zxvf nginx-1.13.8.tar.gz cd nginx-1.13.8 ``` ### 配置并编译安装 ``` ./configure make make install ``` ### 查看开放服务、端口中是否有http服务、80端口 ```shell [root@VM_52_176_centos data]# firewall-cmd --list-all public target: default icmp-block-inversion: no interfaces: sources: services: dhcpv6-client ssh ports: protocols: masquerade: no forward-ports: sourceports: icmp-blocks: rich rules: ``` ### 开放80端口、开放http服务,重启防火墙 ```shell [root@VM_52_176_centos data]# firewall-cmd --permanent --zone=public --add-port=80/tcp success [root@VM_52_176_centos data]# firewall-cmd --permanent --zone=public --add-service=http success [root@VM_52_176_centos data]# firewall-cmd --reload success ``` ### 重新检查 ```shell [root@VM_52_176_centos data]# firewall-cmd --list-all public target: default icmp-block-inversion: no interfaces: sources: services: dhcpv6-client http ssh ports: 80/tcp protocols: masquerade: no forward-ports: sourceports: icmp-blocks: rich rules: ``` ### 设置快捷软链 ```shell ln /usr/local/nginx/sbin/nginx /usr/local/bin/ ``` ### 启动服务器 ```shell nginx ``` ### 修改 nginx 配置文件 ```shell vim /usr/local/nginx/conf/nginx.conf ``` 在 nginx.conf 最后一个’}‘的上一行输入: ```shell include website/*.conf; ``` ### 建立 server 配置文件 ```shell cd /usr/local/nginx/conf/ mkdir website cd website vim web.conf ``` 输入: ```shell server { listen 80; server_name 127.0.0.1; location / { root /opt/data; index index.html index.htm; } } ``` ### 回到系统根目录,建立网站访问目录 ```shell cd /opt/ mkdir data cd data/ vim index.html ``` 输入: ```shell this is test; ``` ### 重启服务器 ```shell nginx -s reload ``` ### 在宿主机浏览器输入 http://IP地址/ 即可验证是否显示“this is test;” ### 重要命令回顾 (启动,重启,终止) ```shell nginx nginx -s reload nginx -s quit (此方式停止步骤是待nginx进程处理任务完毕进行停止) ``` # centos_7 编译安装 php_7.2.1 ### 安装前的依赖包安装 ```shell yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel ``` ### 创建文件夹 ```shell cd /usr/local mkdir php ``` ### 下载最新版 php7.2 ```shell wget http://php.net/distributions/php-7.2.1.tar.gz ``` ### 解压 ```shell tar -zxvf php-7.2.1.tar.gz cd php-7.2.1 ``` ### 配置(这里已经很全了,可根据自己需要去掉部分也行。) ```shell ./configure \ --prefix=/usr/local/php \ --with-config-file-path=/etc \ --enable-fpm \ --with-fpm-user=nginx \ --with-fpm-group=nginx \ --enable-inline-optimization \ --disable-debug \ --disable-rpath \ --enable-shared \ --enable-soap \ --with-libxml-dir \ --with-xmlrpc \ --with-openssl \ --with-mcrypt \ --with-mhash \ --with-pcre-regex \ --with-sqlite3 \ --with-zlib \ --enable-bcmath \ --with-iconv \ --with-bz2 \ --enable-calendar \ --with-curl \ --with-cdb \ --enable-dom \ --enable-exif \ --enable-fileinfo \ --enable-filter \ --with-pcre-dir \ --enable-ftp \ --with-gd \ --with-openssl-dir \ --with-jpeg-dir \ --with-png-dir \ --with-zlib-dir \ --with-freetype-dir \ --enable-gd-native-ttf \ --enable-gd-jis-conv \ --with-gettext \ --with-gmp \ --with-mhash \ --enable-json \ --enable-mbstring \ --enable-mbregex \ --enable-mbregex-backtrack \ --with-libmbfl \ --with-onig \ --enable-pdo \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd \ --with-zlib-dir \ --with-pdo-sqlite \ --with-readline \ --enable-session \ --enable-shmop \ --enable-simplexml \ --enable-sockets \ --enable-sysvmsg \ --enable-sysvsem \ --enable-sysvshm \ --enable-wddx \ --with-libxml-dir \ --with-xsl \ --enable-zip \ --enable-mysqlnd-compression-support \ --with-pear \ --enable-fastcgi \ --enable-opcache ``` 如果在编译中出现 `make: *** [ext/fileinfo/libmagic/apprentice.lo] Error 1`这个错误,则在`./configure`脚本后面加上 ``` --disable-fileinfo ``` 然后我们重新配置、编译文件就可以。 ### 编译安装 ```shell make && make install ``` ### 配置系统环境变量 ```shell vim /etc/profile ``` ```shell 在末尾追加 export PATH=/usr/local/php/bin:$PATH ``` 执行命令让改动生效 ```shell source /etc/profile ``` ### 接下来配置php-fpm ,回到安装包位置 ```shell cp php.ini-production /etc/php.ini cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf chmod +x /etc/init.d/php-fpm ``` ### 建立启动用户及用户组 ```shell groupadd www useradd --shell /sbin/nologin -g www www ``` ### 编辑进配置文件 ```shell vim /usr/local/php/etc/php-fpm.d/www.conf ``` ```shell 查到 user = nginx group = nginx 改成 user = www group = www ``` ### 启动 ```shell /etc/init.d/php-fpm start ``` > (重要) Nginx和PHP-FPM的进程间通信有两种方式,一种是TCP,一种是UNIX Domain Socket. 其中TCP是IP加端口,可以跨服务器.而UNIX Domain Socket不经过网络,只能用于Nginx跟PHP-FPM都在同一服务器的场景.用哪种取决于你的PHP-FPM配置: ``` 方式1: php-fpm.conf: listen = 127.0.0.1:9000 nginx.conf: fastcgi_pass 127.0.0.1:9000; 方式2: php-fpm.conf: listen = /tmp/php-fpm.sock nginx.conf: fastcgi_pass unix:/tmp/php-fpm.sock; 其中php-fpm.sock是一个文件,由php-fpm生成,类型是srw-rw----. ``` UNIX Domain Socket可用于两个没有亲缘关系的进程,是目前广泛使用的IPC机制,比如X Window服务器和GUI程序之间就是通过UNIX Domain Socket通讯的.这种通信方式是发生在系统内核里而不会在网络里传播.UNIX Domain Socket和长连接都能避免频繁创建TCP短连接而导致TIME_WAIT连接过多的问题.对于进程间通讯的两个程序,UNIX Domain Socket的流程不会走到TCP那层,直接以文件形式,以stream socket通讯.如果是TCP Socket,则需要走到IP层,对于非同一台服务器上,TCP Socket走的就更多了. ### ①、TCP 通信方式 来对 Nginx和PHP-FPM 之间进行通讯(二选一) ```shell vim /usr/local/nginx/conf/website/web.conf ``` ```shell #加入,(这里配置了一个$root变量,为程序跟目录) set $root /data/www; location ~ .+.php($|/) { set $script $uri; set $path_info ""; if ($uri ~ "^(.+.php)(/.+)") { set $script $1; set $path_info $2; } fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php?IF_REWRITE=1; fastcgi_param PATH_INFO $path_info; fastcgi_param SCRIPT_FILENAME $root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $script; include fastcgi_params; } ``` ### 重启 nginx ```shell nginx -s reload ``` ### ②、UNIX Domain Socket.通信方式 来对 Nginx和PHP-FPM 之间进行通讯 (二选一) >配置php-fpm ```shell vim /usr/local/php/etc/php-fpm.d/www.conf ``` ``` #将listen = 127.0.0.1:9000 注释 ;listen = 127.0.0.1:9000 #加入下面这一行 listen = /var/run/php-fpm/php-fpm.sock :wq ``` >创建php-fpm文件夹,并给相应的权限: ``` cd /var/run mkdir php-fpm chmod -Rf 777 php-fpm ``` >配置nginx文件: ```shell vim /usr/local/nginx/conf/website/web.conf ``` ```shell #加入,(这里配置了一个$root变量,为程序跟目录) server { listen 81; server_name localhost; location / { root /opt/data; index index.html index.htm; } set $root /opt/data; location ~ .+.php($|/) { set $script $uri; set $path_info ""; if ($uri ~ "^(.+.php)(/.+)") { set $script $1; set $path_info $2; } # fastcgi_pass 127.0.0.1:9000; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php?IF_REWRITE=1; fastcgi_param PATH_INFO $path_info; fastcgi_param SCRIPT_FILENAME $root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $script; include fastcgi_params; } } ``` ### 重启 nginx ```shell nginx -s reload ``` ### 在网站目录写一个 php 文件 ```shell cd /data/www vim info.php 输入 phpinfo(); ``` 在宿主机浏览器输入 http://ip地址/info.php 即可验证是否显示 phpinfo() ### 重要命令回顾(启动) ```shell /etc/init.d/php-fpm start /etc/init.d/php-fpm stop /etc/init.d/php-fpm restart /etc/init.d/php-fpm reload /etc/init.d/php-fpm status ``` # centos_7 安装 mysql_5.7.21 离线 rpm 包 >Centos7将默认数据库mysql替换成了Mariadb,用处不大,先卸载哈 ### rpm 查询这个安装包 ```shell rpm -qa|grep mariadb 输出 mariadb-libs-5.5.52-1.el7.x86_64 ``` ### 卸载命令 ```shell rpm -e --nodeps mariadb-libs-5.5.52-1.el7.x86_64 ``` ### 新增用户、用户组 ```shell groupadd mysql useradd --shell /sbin/nologin -g mysql mysql ``` ### 下载最新版 mysql5.7 离线安装包 (寻到对应服务器类型的对应版本就行,挺大的好几百 M 呢) ```shell wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.21-1.el7.x86_64.rpm-bundle.tar ``` > 也可以先把包下载到宿主机,然后通过ftp工具上传到服务器。 ### 解压 ```shell [root@VM_52_176_centos /]# tar -xvf mysql-5.7.21-1.el7.x86_64.rpm-bundle.tar mysql-community-embedded-devel-5.7.21-1.el7.x86_64.rpm mysql-community-minimal-debuginfo-5.7.21-1.el7.x86_64.rpm mysql-community-common-5.7.21-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.21-1.el7.x86_64.rpm mysql-community-embedded-compat-5.7.21-1.el7.x86_64.rpm mysql-community-server-minimal-5.7.21-1.el7.x86_64.rpm mysql-community-client-5.7.21-1.el7.x86_64.rpm mysql-community-server-5.7.21-1.el7.x86_64.rpm mysql-community-embedded-5.7.21-1.el7.x86_64.rpm mysql-community-test-5.7.21-1.el7.x86_64.rpm mysql-community-devel-5.7.21-1.el7.x86_64.rpm mysql-community-libs-5.7.21-1.el7.x86_64.rpm ``` ### 然后开始按照一定顺序,一个一个安装吧,虽然解压出来一大推,安装依赖关系,只需要安装下面4个就行。 ```shell rpm -ivh mysql-community-common-5.7.21-1.el7.x86_64.rpm rpm -ivh mysql-community-libs-5.7.21-1.el7.x86_64.rpm rpm -ivh mysql-community-client-5.7.21-1.el7.x86_64.rpm rpm -ivh mysql-community-server-5.7.21-1.el7.x86_64.rpm ``` 此时提示缺少依赖 net-tools,使用 yum 安装下就行(若是提示其他也一样,yum 直接安装) ```shell yum install net-tools ``` 继续执行安装上一句就行 ```shell rpm -ivh mysql-community-server-5.7.21-1.el7.x86_64.rpm ``` 注意:如果出现下面情况: ```shell [root@VM_52_176_centos /]# rpm -ivh mysql-community-server-5.7.21-1.el7.x86_64.rpm warning: mysql-community-server-5.7.21-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY error: Failed dependencies: libnuma.so.1()(64bit) is needed by mysql-community-server-5.7.21-1.el7.x86_64 libnuma.so.1(libnuma_1.1)(64bit) is needed by mysql-community-server-5.7.21-1.el7.x86_64 libnuma.so.1(libnuma_1.2)(64bit) is needed by mysql-community-server-5.7.21-1.el7.x86_64 ``` 解决方法:后面加上(` --force --nodeps`) ```shell rpm -ivh mysql-community-server-5.7.21-1.el7.x86_64.rpm --force --nodeps ``` 这样我们就把MySQL安装好了 ### 初始化数据库账号,这里会自动初始化并生成一个 root 的密码 注:在执行本操作时,如若出现下面情况,请根据帖子提示安装即可。 ```shell [root@VM_52_176_centos mysqld]# /usr/sbin/mysqld --initialize --user=mysql /usr/sbin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory ``` 原因:是用yum 安装的`libnuma.so.1`,但安装时默认安装的是32的,但db2需要的是64位的 解决办法: 1.如果已经安装了`libnuma.so.1`,先`yum remove libnuma.so.1` 2.`yum install numactl.x86_64` 再执行命令,初始化数据库账号: ```shell /usr/sbin/mysqld --initialize --user=mysql ``` ### 打开日志文件查看密码(在最后一截,冒号后面都是) ```shell cat /var/log/mysqld.log 输出 2018-03-16T02:12:02.888395Z 1 [Note] A temporary password is generated for root@localhost: p%BI:ByyT37t ``` 我这里密码是:p%BI:ByyT37t ### 启动 mysql ```shell systemctl start mysqld ``` ### 查看启动状态 ```shell systemctl status mysqld 或者 ps -ef |grep mysqld ``` ### 登录 mysql,输入上面密码后就可以了 ```shell [root@VM_52_176_centos mysqld]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.21 Copyright (c) 2000, 2018, 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> ``` ### 执行命令看看,结果提示 还要修改密码 ```shell mysql> show databases; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. ``` ### 更改密码 ``` mysql> alter user 'root'@'localhost' identified by '123456'; Query OK, 0 rows affected (0.00 sec) ``` ### 再试一下,这下可以了。 ```shell mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) ``` ### 创建远程账户 ```shell mysql> GRANT ALL PRIVILEGES ON *.* TO 'test'@'%' IDENTIFIED BY '123456789' WITH GRANT OPTION; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec) ``` ### 查看下配置文件 ```shell cat /etc/my.cnf 主要配置如下,系统都初始化好了,记下就行,不用改 datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid ``` ### 开放3306端口 ```shell firewall-cmd --permanent --zone=public --add-port=3306/tcp firewall-cmd --reload ``` ### 也可以这样检查下端口 ```shell netstat -anlp | grep 3306 ``` ### 重要命令回顾 (启动,终止,查看状态) ```shell systemctl start mysqld systemctl stop mysqld systemctl status mysqld ``` # centos_7 安装 redis_4.0.6 ### 下载 redis4.0.6 ```shell wget http://download.redis.io/releases/redis-4.0.6.tar.gz ``` ### 调整路径 ```shell mv redis-4.0.6.tar.gz /usr/local/src cd /usr/local/src ``` ### 解压 ```shell tar -zxvf redis-4.0.6.tar.gz ``` ### 进入解压后的文件目录,之后直接编译即可(redis安装相对简单) ``` cd /usr/local/src/redis-4.0.6 make ``` ### 创建存储redis文件目录 ```shell mkdir -p /usr/local/redis ``` ### 复制redis-server redis-cli到新建立的文件夹 ```shell # cp /usr/local/src/redis-4.0.6/src/redis-server /usr/local/redis/ # cp /usr/local/src/redis-4.0.6/src/redis-cli /usr/local/redis/ ``` ### 复制redis的配置文件 ```shell cp /usr/local/src/redis-4.0.6/redis.conf /usr/local/redis/ ``` ### 编辑配置文件 ```shell cd /usr/local/redis vim redis.conf ``` ① 在`bind 127.0.0.1`前加“#”将其注释掉 ② 默认为保护模式,把 `protected-mode yes` 改为` protected-mode no` ③ 默认为不守护进程模式,把`daemonize no `改为`daemonize yes` ④ 将 `requirepass foobared`前的“#”去掉,密码改为你想要设置的密码(我为了练习用,设置为123456)  设置完,按“ESC”键,只有输入“:wq!”保存退出 ### 编辑redis开机启动redis脚本 ```shell vim /etc/init.d/redis ``` 在`/etc/init.d/redis`文件中添加入下面的部分: ```shell #!/bin/sh # chkconfig: 2345 80 90 # description: Start and Stop redis #PATH=/usr/local/bin:/sbin:/usr/bin:/bin REDISPORT=6379 EXEC=/usr/local/redis/redis-server REDIS_CLI=/usr/local/redis/redis-cli PIDFILE=/var/run/redis_6379.pid CONF="/usr/local/redis/redis.conf" AUTH="123456" case "$1" in start) if [ -f $PIDFILE ] then echo "$PIDFILE exists, process is already running or crashed" else echo "Starting Redis server..." $EXEC $CONF fi if [ "$?"="0" ] then echo "Redis is running..." fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist, process is not running" else PID=$(cat $PIDFILE) echo "Stopping ..." $REDIS_CLI -p $REDISPORT SHUTDOWN while [ -x ${PIDFILE} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi ;; restart|force-reload) ${0} stop ${0} start ;; *) echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2 exit 1 esac ``` ### 添加开机启动服务 在`rc.local`增加启动代码就可以了。 ```shell vim /etc/rc.local ``` 加入 ``` service redis start ``` ### 设置权限 ```shell chmod 755 /etc/init.d/redis ``` ### 注册系统服务 ```shell chkconfig --add redis ``` ### 添加端口外放 ```shell # 添加 6379端口 firewall-cmd --zone=public --add-port=6379/tcp --permanent # 重启防火墙 firewall-cmd --reload # 查看端口开放情况 firewall-cmd --list-ports ``` ### 测试redis服务 ```shell service redis start service redis stop ``` ### 检查是否安装成功 ```shell ps -ef|grep redis ``` ### 创建redis命令软连接 ```shell ln -s /usr/local/redis/redis-cli /usr/bin/redis ``` ### 测试redis  注意:因为redis登录时加了密码验证,所以用redis时需要加`auth 123456` 全部安装完成后,请跳转到 [Cenots7下添加php的Redis扩展](https://www.phpsix.com/word/89WZKRxZ.html "Cenots7下添加Php的Redis扩展")
编译安装,centos7.4,Nginx_1.13.8,php_7.2.1,mysql_5.7.21,redis_4.0.6
上一篇:
Centos7使用FireWall开放8888端口
下一篇:
Cenots7下添加Php的Redis扩展
栏目导航
【学】PHP
【学】前端
【学】Linux
【学】杂学
【学】支付
【学】Docker
相关文章
Centos7中 ntp 定时时间同步
2374 ℃
Centos7搭建LAMP
462 ℃
Centos7搭建Git及安装使用
603 ℃
Centos7搭建JDK+Tomcat
435 ℃
Centos7安装Redis
539 ℃
Centos7.3搭建LNMP
1121 ℃
Centos7.3搭建LNMP(文章二)
835 ℃
Centos7搭建SVN服务器
955 ℃
Centos 6.x 或 7.x yum安装php5.6.X(最新版)
714 ℃
Nginx-一个IP配置多个站点(无域名)
1786 ℃
Centos7使用FireWall开放8888端口
757 ℃
Centos7.4编译安装Nginx_1.13.8、php_7.2.1、mysql_5....
447 ℃
Cenots7下添加Php的Redis扩展
339 ℃
Redis引起的删库跑路加勒索事件
2224 ℃
创业耗费百万,为何DDoS如此要命Part 1
1267 ℃
记一次被黑后的经历
1880 ℃
(最新)CentOS7.4搭建LN(1.14.0)M(MariaDB)P(7....
1574 ℃
微信小程序
手机扫码访问