Centos编译安装lnmp环境

前期准备工作,更新&安装依赖包

yum update
yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make openssl openssl-devel pcre-devel

nginx(示例版本为 1.16.0)

创建nginx登陆用户
useradd -s /sbin/nologin -M nginx

下载源码包并解压安装
cd /usr/local
mkdir nginx
cd nginx
wget http://nginx.org/download/nginx-1.16.0.tar.gz
tar -xf nginx-1.16.0.tar.gz
cd /nginx-1.16.0
配置自己需要的模块
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module

编译安装
make && make install

开启服务&查看版本号&查看运行状态
/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -V
netstat -ntulp | grep nginx

Mysql(示例版本5.7.31)

根据自己的需要可以去官网选择(传送门)

创建mysql用户组以及用户
groupadd mysql
如果命令报:groupadd: group 'mysql' already exists这个问题,说明已经存在mysql用户组了,跳过此步骤
useradd -r -g mysql mysql

创建目录&下载源码包&解压&改名
cd /usr/local
mkdir mysql
cd mysql
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
tar -zxf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.31-linux-glibc2.12-x86_64 /usr/local/mysql

创建mysql数据文件data目录
cd /usr/local/mysql/
mkdir data

修改mysql目录权限
chown -R mysql:mysql /usr/local/mysql/

初始化mysql操作,并记录初始密码
cd mysql/bin/
./mysqld --user=mysql --initialize --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
如果报错 error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory,则使用如下命令
yum install libaio-devel.x86_64

password.png

修改mysql配置文件(my.cnf)
vim /etc/my.cnf

可根据自己的目录或需要删减或增加

[Client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
port = 3306
user=mysql
socket = /tmp/mysql.sock
basedir=/usr/local/mysql/
datadir=/usr/local/mysql/data
max_connections=200
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
default-storage-engine=INNODB
default_authentication_plugin=mysql_native_password
explicit_defaults_for_timestamp=true
[mysql]
default-character-set=utf8mb4
my.cnf文件

msyql加入开机自启
cd /usr/local/mysql/
cp support-files/mysql.server /etc/init.d/mysql

添加可执行权限
chmod +x /etc/init.d/mysql

添加开机自启
chkconfig --add mysql

查看自启是否成功
chkconfig --list

image.png

配置环境变量,在文件末尾加入配置(如果前面mysql存放路径不一致,请自行修改)
vim /etc/profile
export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib
source /etc/profile ---使文件生效

启动mysql & 查看mysql状态
systemctl restart mysql 或者 /etc/rc.d/init.d/mysql start
service mysql status 或者 ps -aux | grep mysql

登录mysql,这里的密码就是上面安装时的初始密码
mysql -uroot -p

image.png

修改密码&配置远程连接权限
ALTER user 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Your new password' ;
或者
set password for root@localhost = password('Your new password');
远程连接权限修改
update user set host='%' where user='root';
刷新
FLUSH PRIVILEGES;

远程连接mysql数据库报错的原因
1.查看服务器3306端口是否打开
netstat -talnp

image.png

如果没开,则开放,步骤如下
查看防火墙状态
systemctl status firewalld

image.png

开启防火墙
systemctl start firewalld
开放3306端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
重载配置
firewall-cmd --reload
关闭防火墙
systemctl stop firewalld

2.查看服务器 /etc/my.cnf中的bind-address=127.0.0.1,是否开启可以选择加‘#’注释,或者改成bind-address=0.0.0.0
3.查看数据当前用户的host列是%还是localhost

php(示例版本为7.3.23)

建议从官网下载安装包,再通过ftp上传
php下载传送门

下载&解压&编译安装
cd /usr/local/
tar -zxvf php-7.3.23.tar.gz
mv php-7.3.23 /usr/local/php
cd php

编译自己需要的库
./configure \--prefix=/usr/local/php \--with-config-file-path=/etc \--enable-fpm \--with-fpm-user=www \--with-fpm-group=www \--enable-inline-optimization \--disable-debug \--disable-rpath \--enable-shared \--enable-soap \--with-libxml-dir \--with-xmlrpc \--with-openssl \--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-jis-conv \--with-gettext \--with-gmp \--with-mhash \--enable-json \--enable-mbstring \--enable-mbregex \--enable-mbregex-backtrack \--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-opcache

出现这个则编译成功

安装
make && make install

链接文件
ln -s /usr/local/php /usr/local/php
ln -s /usr/local/php/bin/php /usr/local/bin
ln -s /usr/local/php/sbin/php-fpm /usr/local/sbin

对php-fpm运行用户进行配置
cd /usr/local/php
cp ./php.ini-development ./php.ini-production /usr/local/php/etc
cp /usr/local/php/etc/php.ini-development /usr/local/php/etc/php.ini
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

添加 www 用户
groupadd www
useradd -g www www

配置php-fpm
cd /usr/local/php/etc
vim php-fpm.conf
找到
error_log = log/php-fpm.log
include=/usr/local/php/etc/php-fpm.d/*.conf
如果有前面有“;”,则去掉前面的分号(";")

进入php-fpm.d
cd php-fpm.d(完整路径 cd /usr/local/php/etc)

修改 www.conf文件
vim www.conf
将配置文件中user和group改成www
user = www
group = www

image.png

启动php-fpm
sudo /usr/local/php/sbin/php-fpm -R

将php加入系统环境变量
修改/etc/profile文件
sudo vim/etc/profile
在文件结尾处加入
export PATH=$PATH:/usr/local/php/bin

image.png

wq! 保存修改
source /etc/profile 刷新配置

设置开机自启
cp /usr/local/php/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
赋予相应的权限
chmod 755 /etc/init.d/php-fpm
/etc/init.d/php-fpm start
chkconfig --add php-fpm

启动 -- 停止 -- 重启 命令
service php-fpm start
service php-fpm stop
service php-fpm reload

查看php版本
php -v

end

你可能感兴趣的:(Centos编译安装lnmp环境)