centos7.8搭建lnmp环境

it2024-12-18  15

centos7.8搭建lnmp环境,nginx-1.8.0+php-7.4.9+mariadb-10.3.11:

安装依赖

yum install -y epel-release yum install -y gcc gcc-c++ pcre* openssl* gd-devel* zlib-devel pcre-devel wget net-tools yum install -y bzip2 ncurses-devel cmake boost boost-devel openssl openssl-devel yum install -y libaio libaio-devel bison bison-devel libcurl-devel libarchive-devel lsof perl kernel-headers kernel-devel yum install -y libxml2 libxml2-devel bzip2-devel curl-devel php-mcrypt libmcrypt libmcrypt-devel postgresql-devel libxslt-devel autoconf libicu-devel yum install -y libjpeg-devel libpng-devel freetype-devel libzip-devel

安装Nginx 1.解压:

cd /usr/local/ tar -zxvf nginx-1.8.0.tar.gz

2.配置:

cd /usr/local/nginx-1.8.0 ./configure --prefix=/usr/local/nginx --with-pcre --with-http_ssl_module --with-http_spdy_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_auth_request_module --with-http_stub_status_module --with-http_image_filter_module --with-http_gzip_static_module

3.编译安装:

make -j 4 && make install

./configure 配置环境 make 是编译的意思。就是把源码包编译成二进制可执行文件。 make install 就是安装的意思。

make && make install 的意思是: make 与 make install 是两个命令,在你 ./configuration 生成了Makefile之后执行编译安装;与 &&一起的还有||,不过意思不一样,&&是与,||是或;make && make install 的意思就是在执行完 make后,如果没有发生错误就执行make install -j 4 四个CPU编译

make && make install 报错:[ ]case1 src/core/ngx_murmurhash.c: In function ‘ngx_murmur_hash2’: src/core/ngx_murmurhash.c:37:11: error: this statement may fall through [-Werror=implicit-fallthrough=] h ^= data[2] << 16; src/core/ngx_murmurhash.c:38:5: note: here case 2: src/core/ngx_murmurhash.c:39:11: error: this statement may fall through [-Werror=implicit-fallthrough=] h ^= data[1] << 8; src/core/ngx_murmurhash.c:40:5: note: here case 1: 原因,是将警告当成了错误处理,打开/usr/local/nginx-1.8.0/objs/Makefile, 去掉CFLAGS中的-Werror 再重新make -Wall 表示打开gcc的所有警告 -Werror,它要求gcc将所有的警告当成错误进行处理

3.配置nginx: #修改nginx配置文件

/usr/local/nginx-1.8.0/conf/nginx.conf

#添加vhost目录

cd /usr/local/nginx/conf/ mkdir vhost

#添加站点配置文件(*.conf)

cd /usr/local/nginx/conf/vhost 添加*.conf配置文件

#添加nginx为系统服务、开机启动

cd /usr/lib/systemd/system/ vi nginx.service systemctl enable nginx.service (设置nginx服务开机自启动)

#启动nginx服务

systemctl start nginx

#开启防火墙80端口

cd /usr/lib/systemd/system/ firewall-cmd --zone=public --add-port=80/tcp --permanent firewall-cmd --reload(更新防火墙规则)

centos7 默认是FirewallD 提供支持网络/防火墙区域(zone)定义网络链接以及接口安全等级的动态防火墙管理工具,利用FirewallD开启80端口操作如下: 开启80端口 firewall-cmd --zone=public --add-port=80/tcp --permanent 出现success表明添加成功 命令含义: –zone #作用域 –add-port=80/tcp #添加端口,格式为:端口/通讯协议 –permanent #永久生效,没有此参数重启后失效

报错:执行firewall-cmd --permanent --zone=public –add-port=3306/tcp,提示FirewallD is not running。[ ]通过systemctl status firewalld查看firewalld状态,发现当前是dead状态,即防火墙未开启。 通过systemctl start firewalld开启防火墙,没有任何提示即开启成功。 (如果提示Warning:allowzonedrifting is enabled.This is considered an insecure configuration option.It will be removed in a future release … ling it now. 可通过在 /etc/firewalld/firewalld.conf 中禁用它。在vim编辑器命令模式输入 /AllowZoneDrifting 进行搜索, 然后将yes更改为no,按ESC键退出编辑模式到命令模式,输入:wq 保存退出。

再执行

firewall-cmd --zone=public --add-port=80/tcp --permanent firewall-cmd --reload(更新防火墙规则)

提示success成功

安装PHP 1.解决依赖:

yum install -y libsqlite3x-devel yum install -y oniguruma-devel 提示错误 No match for argument: oniguruma-devel Error: Unable to find a match: oniguruma-devel [ ]运行命令 dnf config-manager --set-enabled PowerTools

2.编译libzip:

cd /usr/local yum remove -y libzip tar -zxvf libzip-1.2.0.tar.gz cd libzip-1.2.0 ./configure make -j 4 && make install

3.添加搜索路径到配置文件:

export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig/"

export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig/" 设置 pkg_config_path 环境变量 方法,只是想加上某库的pkg

4.解压

cd /usr/local/ tar -zxvf php-7.4.9.tar.gz

5.配置:

./configure --prefix=/usr/local/php --with-curl --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-mysqli --with-openssl --with-pdo-mysql --with-pdo-sqlite --with-pear --with-xmlrpc --with-xsl --with-zlib --with-bz2 --with-mhash --enable-fpm --enable-bcmath --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-sysvshm --enable-xml --enable-fpm --enable-pdo --enable-gd --with-iconv --with-zip --with-jpeg --with-freetype

6.编译安装:

make -j 4 && make install make test

7.安装PHP扩展: #intl(一些新框架要求安装)

cd php-7.4.9/ext/intl/ /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config make -j 4 && make install

#配置php #添加配置文件 /usr/local/php/lib/php.ini

#添加php-fpm配置文件 /usr/local/php/etc/php-fpm-site.conf

#添加pid文件 /usr/local/php/var/run/php-fpm-site.pid

#添加tmp目录,给权限

mkdir /var/php mkdir /var/php/tmp chmod 777 /var/php/tmp/

#添加php-fpm服务启动文件

cd /etc/init.d/php cd /etc/init.d/php-fpm chmod a+x /etc/init.d/php chmod a+x /etc/init.d/php-fpm

#添加环境变量 vi /etc/profile #在文件的最后面加入 #php path export PATH=$PATH:/usr/local/php/bin

#运行下面语句

source /etc/profile

#添加php用户

useradd webusers

#添加开机启动

chkconfig php on

#启动php

service php start 访问不了网站: 创建一个var/web目录

安装mariadb

1.解压:

cd /usr/local/ tar -zxvf mariadb-10.3.11.tar.gz

2.配置:

cd /usr/local/mariadb-10.3.11 cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_general_ci -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DENABLE_DOWNLOADS=1 -DEXTRA_CHARSETS=all -DSYSCONFDIR=/etc -DWITHOUT_TOKUDB=1 -DWITH_DEBUG=0 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_LOBWRAP=0 -DMYSQL_USER=mysql -DMYSQL_UNIX_ADDR=/var/run/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DMYSQL_MAINTAINER_MODE=0

#编译安装

make -j 4 && make install 报错时:主要是在编译过程中,内存不够造成的。 #使用swap创建临时分区 sudo dd if=/dev/zero of=/swapfile bs=64M count=16 #count的大小就是增加的swap空间的大小,64M是块大小,所以空间大小是bs*count=1024MB sudo mkswap /swapfile #把刚才空间格式化成swap格式 chmod 0600 /swapfile #该目录权限,不改的话,在下一步启动时会报“swapon: /swapfile: insecure permissions 0644, 0600 suggested.”错误 #使用刚才创建的swap空间 sudo swapon /swapfile 再运行make -j 4 && make install 把临时空间关闭swapoff -a

#配置mysql #添加mysql用户

useradd mysql

#添加my.cnf配置文件 /etc/my.cnf

#创建一些必要的目录

mkdir /usr/local/mysql/data mkdir /usr/local/mysql/logs mkdir /usr/local/mysql/pids

#把目录权限给mysql用户

chown -R mysql:mysql /usr/local/mysql

#添加服务启动文件

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld chmod a+x /etc/init.d/mysqld

#添加环境变量 vi /etc/profile #在文件的最后面加入 #mysql path export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib

source /etc/profile 报错:mysql -u root -p(mysql not found)[ ]运行:export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib

#开机自启动

chkconfig mysqld on

#初始化mysql数据

cd /usr/local/mysql/ ./scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data/

#启动mysql

service mysqld start

#初始化mysql

/usr/local/mysql/bin/mysql_secure_installation

#开启防火墙3306端口

firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="xxx.xxx.xxx.xxx" port protocol="tcp" port="3306" accept" firewall-cmd --reload

#安装phpmyadmin

tar -zxvf phpMyAdmin-5.0.2-all-languages.tar.gz mv phpMyAdmin-5.0.2-all-languages /var/web/phpmyadmin

————————————————小笔记

网站根目录/var/web添加站点配置文件cd /usr/local/nginx/conf/vhost/*.conf修改nginx配置文件/usr/local/nginx/conf/nginx.confphpmyadmin登陆不上 (错误:phpmyadmin mysqli_real_connect(): (HY000/2002):No such file or directory的错误

解决方法 : 把phpmyadmin目录中的配置文件 config.sample.inc.php复制成config.inc.php 打开编辑config.inc.php 找到: c f g [ ′ S e r v e r s ′ ] [ cfg['Servers'][ cfg[Servers][i][‘host’] = ‘localhost’; 改成: c f g [ ′ S e r v e r s ′ ] [ cfg['Servers'][ cfg[Servers][i][‘host’] = ‘127.0.0.1’

错误:mysqli_real_connect(): (HY000/1045): Access denied for user ‘root‘@‘localhost‘ (using password: YES)

解决方法: 打开phpmyadmin文件夹下面的config.inc.php文件并打开,找到 c f g [ ′ S e r v e r s ′ ] [ cfg['Servers'][ cfg[Servers][i][‘controluser’] = ‘pma’; c f g [ ′ S e r v e r s ′ ] [ cfg['Servers'][ cfg[Servers][i][‘controlpass’] = ‘’; 将其注释去掉,并改成你的数据库用户和密码,保存 c f g [ ′ S e r v e r s ′ ] [ cfg['Servers'][ cfg[Servers][i][‘controluser’] = ‘root’; c f g [ ′ S e r v e r s ′ ] [ cfg['Servers'][ cfg[Servers][i][‘controlpass’] = ‘123456’; 刷新或重新打开phpmyadmin

最新回复(0)