LINUX——lnmp架构的搭建,与lamp架构类似
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LINUX——lnmp架构的搭建,与lamp架构类似相关的知识,希望对你有一定的参考价值。
关闭防火墙
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# systemctl disable firewalld
[[email protected] ~]# sed -ri ‘s/(SELINUX=).*/1disabled/g‘ /etc/selinux/config
[[email protected] ~]# setenforce 0
配置安装163的源
[[email protected] ~]# cd /etc/yum.repos.d/
//创建备份目录
[[email protected] yum.repos.d]# mkdir /etc/repo-bf
//将原yum仓库的文件备份到repo-bf
[[email protected] yum.repos.d]# mv * /etc/repo-bf
//下载163的源到yum仓库
[[email protected] yum.repos.d]# curl -o 163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
//将改为版本的7
[[email protected] yum.repos.d]# sed -i ‘s/$releasever/7/g‘ /etc/yum.repos.d/163.repo
[[email protected] yum.repos.d]# sed -i ‘s/enabled=0/enabled=1/g‘ /etc/yum.repos.d/163.repo
//自动配置yum的软件仓库,也可以自己配置
[[email protected] yum.repos.d]# yum -y install epel-release
[[email protected] yum.repos.d]# yum clean all
//安装编译环境
[[email protected] yum.repos.d]# yum -y install gcc gcc-c++
[[email protected] yum.repos.d]# yum -y install wget
安装依赖包
//创建用户
[[email protected] ~]# useradd -r -M -s /sbin/nologin nginx
//安装编译环境
[[email protected] ~]# yum -y groups list
[[email protected] ~]# yum -y install pcre-devel openssl openssl-devel gd-devel
文件中的包,如果如果在xshell中无法下载,可以先下载然后传入
//创建日志存放目录
[[email protected] ~]# mkdir -p /var/log/nginx
[[email protected] ~]# chown -R nginx.nginx /var/log/nginx
//下载nginx,编译安装
[[email protected] ~]# cd /usr/src/
[[email protected] src]# wget http://64.123.28.133/files/21490000000827F6/nginx.org/download/nginx-1.14.0.tar.gz
[[email protected] src]# tar xf nginx-1.14.0.tar.gz
[[email protected] src]# cd nginx-1.14.0
[[email protected] nginx-1.14.0]# yum -y install gcc gcc-c++
[[email protected] nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
[[email protected] nginx-1.14.0]# make -j 4 && make install
//设置环境变量
[[email protected] nginx-1.14.0]# echo ‘export PATH=/usr/local/nginx/sbin:$PATH‘ > /etc/profile.d/nginx.sh
[[email protected] nginx-1.14.0]# . /etc/profile.d/nginx.sh
//启动
[[email protected] nginx-1.14.0]# nginx
2.安装mysql
//安装依赖包
[[email protected] ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
创建用户和组
[[email protected] ~]# groupadd -r -g 306 mysql
[[email protected] ~]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql
下载二进制格式的mysql软件包
[[email protected] ~]# cd /usr/src/
[[email protected] src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
解压软件至/usr/local/
[[email protected] ~]# cd /usr/src/
[[email protected] src]# ls
debug kernels mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz nginx-1.14.0 nginx-1.14.0.tar.gz
[[email protected] src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[[email protected] src]# ls /usr/local/
bin etc games include lib lib64 libexec mysql-5.7.22-linux-glibc2.12-x86_64 nginx sbin share src
[[email protected] src]# cd /usr/local/
[[email protected] local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.22-linux-glibc2.12-x86_64/"
//修改属主
[[email protected] local]# chown -R mysql.mysql /usr/local/mysql
[[email protected] local]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 8月 24 16:13 /usr/local/mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
//设置环境变量
[[email protected] local]# echo ‘export PATH=/usr/local/mysql/bin:$PATH‘ > /etc/profile.d/mysql.sh
[[email protected] local]# . /etc/profile.d/mysql.sh
[[email protected] local]# echo $PATH
/usr/local/mysql/bin:/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
//建立数据存放目录
[[email protected] local]# cd /usr/local/mysql
[[email protected] mysql]# mkdir /opt/data
[[email protected] mysql]# chown -R mysql.mysql /opt/data/
//初始化数据库,最后生成的是一个临时密码
[[email protected] mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2018-08-24T08:20:10.891585Z 1 [Note] A temporary password is generated for [email protected]: nEU8fScMEm(M
配置mysql
[[email protected] mysql]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
"/usr/local/include/mysql" -> "/usr/local/mysql/include/"
[[email protected] mysql]# echo ‘/usr/local/mysql/lib‘ > /etc/ld.so.conf.d/mysql.conf
[[email protected] mysql]# ldconfig -v
//生成配置文件
[[email protected] ~]# cat > /etc/my.cnf <<EOF
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
EOF
配置启动脚本
[[email protected] mysql]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[[email protected] mysql]# sed -ri ‘s#^(basedir=).*#1/usr/local/mysql#g‘ /etc/init.d/mysqld
[[email protected] mysql]# sed -ri ‘s#^(datadir=).*#1/opt/data#g‘ /etc/init.d/mysqld
启动mysql
[[email protected] mysql]# service mysqld start
Starting MySQL.Logging to ‘/opt/data/localhost.localdomain.err‘.
SUCCESS!
[[email protected] mysql]# ps -ef|grep mysql
root 24348 1 0 16:25 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql 24526 24348 2 16:25 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=localhost.localdomain.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root 24556 2316 0 16:25 pts/0 00:00:00 grep --color=auto mysql
[[email protected] mysql]# ss -antl
修改临时密码
[[email protected] mysql]# mysql -uroot -p
Enter password:
mysql> set password = password(‘guohui123‘);
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
安装php
//安装依赖包
[[email protected] ~]# yum -y install bzip2-devel curl-devel freetype-devel gcc libjpeg-devel libpng-devel libxslt-devel libxml2-devel openssl-devel pcre-devel pcre-devel zlib-devel
//下载
[[email protected] ~]# wget http://cn.php.net/distributions/php-7.2.8.tar.xz
[[email protected] ~]# tar xf php-7.2.8.tar.xz
[[email protected] ~]# cd php-7.2.8
//配置安装变量,“/usr/local/php”是安装路径,可以改成自己喜欢的安装路径。
[[email protected] php-7.2.8]#./configure --prefix=/usr/local/php --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-jpeg-dir --with-xmlrpc --with-xsl --with-zlib --with-bz2 --with-mhash --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-sysvshm --enable-xml --enable-zip
//编译源码,在解压目录执行编译命令:make
[[email protected] php-7.2.8]# make
//安装php
[[email protected] php-7.2.8]# make install
//配置PHP
将PHP源码包(/root/php-7.2.8)中的php.ini-development文件复制到/usr/local/php/下,更名为php.ini
源码包在root下下载,所以在root下
[[email protected] php-7.2.8]# cp /root/php-7.2.8/php.ini-development /usr/local/php/php.ini
[[email protected] php-7.2.8]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[[email protected] php-7.2.8]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
配置php.ini “cgi.fix_pathinfo=0”
[[email protected] php-7.2.8]# vim /usr/local/php/php.ini
cgi.fix_pathinfo=0
启动php-fpm服务:
启动完毕之后,php-fpm服务默认使用9000端口
[[email protected] php-7.2.8]# /usr/local/php/sbin/php-fpm
[[email protected] php-7.2.8]# ss -anlt
LISTEN 0 128 127.0.0.1:9000 *:*
最后配置nginx
编辑nginx配置文件/usr/local/nginx/conf/nginx.conf ,主要修改nginx的server,{}配置块中的内容,修改location块,追加index.php让nginx服务器默认支持index.php为首页:
(在尾部追加了index.php)
2.更换为下方内容
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;
}
编辑更改
[[email protected] php-7.2.8]# vim /usr/local/nginx/conf/nginx.conf
1.
index index.html index.htm; 更改成 index index.html index.htm index.php;
2.
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;
}
//这里面很多都是默认的,root是配置PHP程序放置的根目录,主要修改的就是fastcgi_param中的/
scripts为$document_root
修改完成这些保存并退出,然后重启nginx:/usr/local/nginx/sbin/nginx -s reload
接下来编辑一个测试的php程序,在nginx下的html目录下创建test.php文件,打印一下php配置:
[[email protected] ~]# cd /usr/local/nginx/html
[[email protected] html]# touch test.php
<?php
phpinfo();
?>
然后打开浏览器输入对应的地址进行访问
http://192.168.56.11/test.php
以上是关于LINUX——lnmp架构的搭建,与lamp架构类似的主要内容,如果未能解决你的问题,请参考以下文章