CentOS 7.2mini版本下编译安装php7.4.6+MySQL5.7.14+Nginx1.18.0
一、安装前的准备工作
1、更新系统、禁止SELINUX、安装php、MySQL、Nngix所依赖的包、删除系统自带的mysql配置文件
[root@localhost ~]# yum -y update
[root@localhost ~]# vi /etc/selinux/config # 设置SELINUX=disabled
[root@localhost ~]# yum -y install wget gcc gcc-c++ autoconf automake cmake bison ncurses-devel m4 libxml2 python-devel systemd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel libicu-devel sqlite-devel bzip2 bzip2-devel libtool
[root@localhost ~]# mv /etc/my.cnf /etc/my.cnf.bk
2、下载以下包 #我把所有源文件都下载在root目录,读者可自行修改源文件存放目录
3.1 libmcrypt-2.5.8.tar.gz
3.2 mcrypt-2.6.8.tar.gz
3.3 mhash-0.9.9.9.tar.gz
3.4 zlib-1.2.8.tar.gz
1 2 3 4 | curl https: //jaist .dl.sourceforge.net /project/mcrypt/MCrypt/2 .6.8 /mcrypt-2 .6.8. tar .gz -o /root/mcrypt-2 .6.8. tar .gz curl https: //zenlayer .dl.sourceforge.net /project/mcrypt/Libmcrypt/2 .5.8 /libmcrypt-2 .5.8. tar .gz -o libmcrypt-2.5.8. tar .gz curl https: //jaist .dl.sourceforge.net /project/mhash/mhash/0 .9.9.9 /mhash-0 .9.9.9. tar .gz -o mhash-0.9.9.9. tar .gz curl http: //www .zlib.net /zlib-1 .3.1. tar .gz -o zlib-1.3.1. tar .gz |
解压并安装如:
#tar -zvxf libmcrypt-2.5.8.tar.gz
#cd libmcrypt-2.5.8
#./configure
#make && make insatll
3、在安装软件时如果提示有什么依赖包没有安装的可以再执行yum install * -y (*表示相关包)
4、安装 mcrypt 报错时
1 2 3 4 5 6 7 8 9 10 11 12 | *** Could not run libmcrypt test program, checking why… *** The test program compiled, but did not run. This usually means *** that the run- time linker is not finding LIBMCRYPT or finding the wrong *** version of LIBMCRYPT. If it is not finding LIBMCRYPT, you’ll need to set your *** LD_LIBRARY_PATH environment variable, or edit /etc/ld .so.conf to point *** to the installed location <wbr> <wbr>Also, make sure you have run ldconfig if that *** is required on your system *** *** If you have an old version installed, it is best to remove it, although *** you may also be able to get things to work by modifying LD_LIBRARY_PATH *** configure: error: *** libmcrypt was not found |
修改 ~/.bash_profile,在文件最后面加上两行,然后再执行 source ~/.bash_profile
1 2 | LD_LIBRARY_PATH= /usr/local/lib export LD_LIBRARY_PATH |
二、编译安装Nginx
命令汇总
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | # 添加 nginx 组及用户 groupadd -r nginx useradd -r -g nginx -s /bin/false nginx # 官网下载最新稳定版 wget https: //nginx .org /download/nginx-1 .26.2. tar .gz -o /root/nginx-1 .26.2. tar .gz tar -xzf /root/nginx-1 .26.2. tar .gz -C /usr/scr/ cd /usr/scr/nginx-1 .26.2 # 编译安装 . /configure \ --prefix= /usr/local/nginx \ --modules-path= /usr/local/nginx/modules \ --with-http_ssl_module \ --pid-path= /usr/local/nginx/nginx .pid \ --user=nginx \ --group=nginx make && make install -j # 配置systemctrl管理 vim /usr/lib/systemd/system/nginx .service ``` [Unit] Description=The NGINX HTTP and reverse proxy server After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile= /usr/local/nginx/nginx .pid ExecStartPre= /usr/local/nginx/sbin/nginx -t ExecStart= /usr/local/nginx/sbin/nginx ExecReload= /usr/local/nginx/sbin/nginx -s reload ExecStop= /usr/local/nginx/sbin/nginx -s stop PrivateTmp= true [Install] WantedBy=multi-user.target ``` |
三、编译安装MySQL
1、去官网http://dev.mysql.com/Downloads/MySQL-5.7/mysql-boost-5.7.14.tar.gz下载带boost的5.7.14版本
1 | wget http: //dev .mysql.com /Downloads/MySQL-5 .7 /mysql-boost-5 .7.14. tar .gz |
2、编译步骤如下
1、用winSCP上传mysql-boost-5.7.14.tar.gz到/root目录下
2、groupadd mysql
3、useradd -r -g mysql -s /bin/false mysql
4、用cmake编译mysql, 相关参数可以参考https://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html,下列参数要写在一行
cmake \ -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \ -DSYSCONFDIR=/usr/local/mysql/etc \ -DSYSTEMD_PID_DIR=/usr/local/mysql \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \ -DMYSQL_DATADIR=/usr/local/mysql/data \ -DWITH_BOOST=boost \ -DWITH_SYSTEMD=1
5、make && make install -j
6、配置mysql并初始化数据库
6.1 cd /usr/local/mysql #进入编译目录
6.2 chown -R mysql . #修改目录所有者
6.3 chgrp -R mysql . #修改目录组
6.4 cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld #配置mysqld服务
6.5 cp /usr/local/mysql/support-files/my-default.cnf /usr/local/mysql/my.cnf #配置my.cnf 并编辑
1 2 3 4 5 6 | [mysqld]<br>user = msyql basedir = /usr/local/mysql datadir = /usr/local/mysql/data port = 3306 server_id = /usr/local/mysql/mysqld .pid socket = /usr/local/mysql/mysql .sock |
6.5 chkconfig mysqld on #设置mysqld开机自启
6.6 bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data #初始化数据库
6.7 bin/mysqld --user=mysql & #启动mysql, 如果报Please read "Security" section of the manual to find out how to run mysqld as root!,就在my.cnf中加入user=root, 表示以root用户启动
6.8 vim ~/.bash_profile # 设置环境变量
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin
7、修改root用户登录密码并允许root用户远程登录
7.1 mysql -u root --skip-password
7.2 ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
7.3 允许root用户远程登录
7.3.1 use mysql;
7.3.2 update user set host='%' where user='root' and host='localhost'; #允许
(update user set host='localhost' where user='root'; #禁用)
7.3.3 flush privileges;
7.3.4 service mysqld restart
8、解决service mysqld start|stop报MySQL server PID file could not be found!或者Couldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe), 其实可通过阅读此文件解决相关错误
8.1 chmod 777 /usr/local/mysql #因我设置mysqld.pid文件保存在/usr/local/mysql目录,所以保证其有可写权限
8.2 通过winSCP修改/etc/init.d/mysqld文件
8.2.1 basedir=/usr/local/mysql #手动指定
8.2.2 datadir=/usr/local/mysql/data #手动指定
8.2.3 mysqld_pid_file_path=/usr/local/mysql/mysqld.pid #手动指定
8.2.4 把此文件中所有未注释的含有mysqld_safe的字符替换成mysqld
命令汇总
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | # 下载安装 wget http: //dev .mysql.com /Downloads/MySQL-5 .7 /mysql-boost-5 .7.14. tar .gz tar -xzf mysql-boost-5.7.14. tar .gz -C /usr/src/ cd /usr/src/mysql-5 .7.14 <br>cmake \ -DCMAKE_INSTALL_PREFIX= /usr/local/mysql \ -DMYSQL_UNIX_ADDR= /usr/local/mysql/mysql .sock \ -DSYSCONFDIR= /usr/local/mysql/etc \ -DSYSTEMD_PID_DIR= /usr/local/mysql \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \ -DMYSQL_DATADIR= /usr/local/mysql/data \ -DWITH_BOOST=boost \ -DWITH_SYSTEMD=1 make && make install -j # 复制配置文件 cd /usr/local/mysql chown -R mysql . && chgrp -R mysql . cp /usr/local/mysql/support-files/mysql .server /etc/init .d /mysqld cp /usr/local/mysql/support-files/my-default .cnf /usr/local/mysql/my .cnf vim /usr/local/mysql/my .cnf ``` # [mysqld] user = msyql basedir = /usr/local/mysql datadir = /usr/local/mysql/data port = 3306 server_id = /usr/local/mysql/mysqld .pid socket = /usr/local/mysql/mysql .sock ``` # 初始化数据库 bin /mysqld --initialize-insecure --user=mysql --basedir= /usr/local/mysql --datadir= /usr/local/mysql/data # 配置环境变量 vim ~/.bash_profile ``` PATH=$PATH:$HOME /bin : /usr/local/mysql/bin ``` # 启动 bin /mysqld --user=mysql & # 允许远程登录 GRANT ALL PRIVILEGES ON *.* TO 'root' @ '%' IDENTIFIED BY 'YOUR_PASSWD' WITH GRANT OPTION; FLUSH PRIVILEGES; |
四、编译安装php
1、去官网http://php.net/downloads.php下载 php
1 | wget https: //www .php.net /distributions/php-7 .4.33. tar .gz |
2、编译步骤如下
1、用winSCP上传php-7.0.10.tar.gz到/root目录下
2、tar -zvxf php-7.0.10.tar.gz #解压
3、配置编译php参数, 可使用./configure --help命令查看所有编译配置项目, 下列参数要写在一行中
./configure \ --prefix=/usr/local/php \ --exec-prefix=/usr/local/php \ --datadir=/usr/local/php \ --with-config-file-path=/usr/local/php/etc \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd \ --with-fpm-user=nginx \ --with-fpm-group=nginx \ --enable-gd \
--with-iconv \
--enable-intl \
--enable-mbstring \
--enable-fpm \
--enable-pcntl \
--enable-mysqlnd
4、make && make install -j #编译并安装
5、cd /usr/local/php #进入编译目录
6、修改相关配置文件
6.1 cp /usr/local/php/etc/php.ini.default /usr/local/php/etc/php.ini #php.ini中相关配置依项目需要自行修改,配置nginx支持php参考http://php.net/manual/zh/install.unix.nginx.php
6.2 cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf #去掉[global]项下pid前的;
6.3 cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf #大致在23、24行修改user和group如:user = nginx,group = nginx
7、chmod 777 /usr/local/php/var/run #默认PID文件是写在/usr/local/php/var/run这个目录中,所以修改目录权限
8、sbin/php-fpm #启动php, 可通过sbin/php-fpm -h 查看相关操作命令列表
9、在/usr/lib/systemd/system目录下新建php-fpm.service文件,这样就可以通过systemctl stop|start|reload php-fpm.service来操作php-fpm,内容如下:
[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target
Before=nginx.service
[Service]
Type=forking
PIDFile=/usr/local/php/var/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm
ExecStop=/bin/kill -QUIT `cat /usr/local/php/var/run/php-fpm.pid`
ExecReload=/bin/kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`
PrivateTmp=true
[Install]
WantedBy=multi-user.target
10、安装php相关扩展, 下面以编译mysqli为例
10.1 cd /root/php-7.0.10/ext/mysqli #首先必须进入php源文件解压后所在目录,不然在执行phpize时会报Cannot find config.m4
10.2 /usr/local/php/bin/phpize #执行phpize,/usr/local/php/bin这个目录是编译后php所在目录
10.3 ./configure --with-php-config=/usr/local/php/bin/php-config
10.4 make && make install #执行完这一步后,会在/usr/local/php/lib/php/extensions/no-debug-non-zts-20151012目录下生成mysqli.so文件
10.5 vi /usr/local/php/etc/php.ini #在php.ini[Dynamic Extensions]下新增extension=mysqli.so
10.6 重启Nginx/Apache、重启php-fpm
五、安装redis及phpredis扩展
1、用winSCP上传redis-3.0.0.tar.gz到/root目录下
1 | wget https: //download .redis.io /redis-stable . tar .gz |
2、tar -zvxf redis-3.0.0.tar.gz -C /usr/local #解压到/usr/local目录下
3、yum install tcl -y #redis-test依赖此包
4、cd /usr/local/redis-3.0.0
5、chmod 777 . #当前目录可写,我把redis.pid文件指定到了/usr/local/redis-3.0.0下
6、make && make install
7、vi /usr/local/redis-3.0.0/redis.conf #修改redis配置文件
7.1 daemonize yes
7.2 pidfile /usr/local/redis/redis.pid
8、src/redis-server & #启动redis
src/redis-cli shutdown #关闭redis
src/redis-server --help #查看相关帮助命令
9、在/usr/lib/systemd/system目录下新建redis.service文件,这样就可以通过systemctl stop|start redis.service来操作redis,内容如下:
[Unit]
Description=The Redis 3.0.0 Service
After=syslog.target
[Service]
Type=forking
PIDFile=/usr/local/redis/redis.pid
ExecStartPre=/usr/local/redis/src/redis-server /usr/local/redis/redis.conf
ExecStart=/usr/local/redis/src/redis-server /usr/local/redis/redis.conf
ExecStop=/usr/local/redis/src/redis-cli shutdown
PrivateTmp=true
[Install]
WantedBy=multi-user.target
10、安装phpredis扩展,下载地址:https://github.com/phpredis/phpredis/releases
10.1 用winSCP上传phpredis-3.0.0.tar.gz到/root目录下
10.2 tar -zvxf phpredis-3.0.0.tar.gz -C /usr/local #解压到/usr/local目录下
10.3 cd /usr/local/phpredis-3.0.0
10.4 /usr/local/php/bin/phpize
10.5 ./configure --with-php-config=/usr/local/php/bin/php-config
10.6 make && make install
10.7 在php.ini文件中添加extension=redis.so
10.8 重启Nginx/Apache、重启php-fpm
六、至此在我的VirturBox中CentOS7.2下成功搭建了LNMP环境
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析