5.6 课堂笔记

LNMP Web服务搭建

mysql与php安装

1、JAVA Web环境(企业更多)

[tomcat(jvm)]、resin、jboss、Weblogic

配合nginx proxy_pass代理功能

2、Python Web环境

配合nginx uwsgi_pass代理功能

3、PHP Web环境(也会用,二三四五线)

配合nginx fastcgi_pass代理功能

4、GO 语言环境

数据库产品:

MySQL(mariadb)、Oracle、SQL SERVER、PGSQL

单机安装LNMP

先装数据库:

1.创建用户

[root@web02 ~]# useradd mysql -s /sbin/nologin -M

[root@web02 ~]# id mysql

uid=1002(mysql) gid=1002(mysql) 组=1002(mysql)

上传mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz

解压mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz

[root@web02 /server/tools]# tar xf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz

[root@web02 /server/tools]# mv mysql-5.7.26-linux-glibc2.12-x86_64 /application/mysql-5.7.26

创建软连接

ln -s /application/mysql-5.7.26/  /application/mysql

2.卸载依赖包

rpm -e --nodeps mariadb-libs

3.编辑配置文件/etc/my.cnf

[root@web02 /server/tools]# vim /etc/my.cnf

[mysqld]

basedir = /application/mysql/

datadir = /application/mysql/data

socket = /tmp/mysql.sock

server_id = 1

port = 3306

log_error = /application/mysql/data/oldboy_mysql.err

[mysql]

socket = /tmp/mysql.sock

prompt = oldboy [\\d]>

4.初始化数据库

[root@web02 /server/tools]# rpm -qa mariadb-libs

[root@web02 /server/tools]# yum install libaio-devel -y

创建目录并授权

mkdir -p /application/mysql/data

chown -R mysql.mysql /application/mysql/

初始化

/application/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/application/mysql/ --datadir=/application/mysql/data

====

--user=mysql                        #<==mysql用户。

--basedir=/application/mysql/      #<==MySQL根目录。

--datadir=/application/mysql/data  #<==MySQL数据文件目录。

--initialize-insecure              #<==关闭MySQL安全策略,本书选择了关闭策略。

--initialize                        #<==开启MySQL安全策略,高安全环境采用。

5.配置MYSQL启动服务

[root@web02 /application/mysql/support-files]# vim /etc/systemd/system/mysqld.service

[Unit]

Description=MySQL Server by oldboy

Documentation=man:mysqld(8)

Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html

After=network.target

After=syslog.target

[Install]

WantedBy=multi-user.target

[Service]

User=mysql

Group=mysql

ExecStart=/application/mysql/bin/mysqld --defaults-file=/etc/my.cnf

LimitNOFILE = 5000

启动

[root@web02 /application/mysql/support-files]# systemctl start mysqld

[root@web02 /application/mysql/support-files]# systemctl enable mysqld

[root@web02 /application/mysql/support-files]# systemctl status mysqld

查看端口

[root@web02 /application/mysql/support-files]# netstat -lntup|grep mysql

查看进程

[root@web02 /application/mysql/support-files]# ps -ef|grep mysql|grep -v grep

6.配置环境变量登录

[root@web02 /application/mysql/support-files]# echo 'export PATH=/application/mysql/bin:$PATH' >>/etc/profile

[root@web02 /application/mysql/support-files]# tail -1 /etc/profile

[root@web02 /application/mysql/support-files]# . /etc/profile

[root@web02 /application/mysql/support-files]# echo $PATH

[root@web02 /application/mysql/support-files]# mysql

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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.

oldboy [(none)]>

7.如果出错 查看错误日志

日志文件  cat /application/mysql/data/oldboy_mysql.err

8.更改密码

mysqladmin -u root password 'oldboy123'

重新登录(交互式登录)

mysql -uroot -p

oldboy123

非交互登录:

mysql -uroot -poldboy123

====================================

编译安装PHP

1.安装PHP调用的库

yum install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel -y

yum install freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel libxslt-devel -y

检查

rpm -qa zlib-devel libxml2-devel libjpeg-turbo-devel libiconv-devel

rpm -qa freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel

安装libiconv

cd /server/tools

上传libiconv-1.16.tar.gz

tar zxf libiconv-1.16.tar.gz

cd libiconv-1.16

./configure --prefix=/application/libiconv

make

make install

cd ../

yum install libmcrypt-devel -y

yum install mhash -y

yum install mcrypt -y

2.安装PHP

cd /server/tools

上传 php-7.3.5.tar.gz

tar xf php-7.3.5.tar.gz

cd php-7.3.5/

编译

./configure \

--prefix=/application/php-7.3.5 \

--enable-mysqlnd  \

--with-mysqli=mysqlnd \

--with-pdo-mysql=mysqlnd \

--with-iconv-dir=/application/libiconv \

--with-freetype-dir \

--with-jpeg-dir \

--with-png-dir \

--with-zlib \

--with-libxml-dir=/usr \

--enable-xml \

--disable-rpath \

--enable-bcmath \

--enable-shmop \

--enable-sysvsem \

--enable-inline-optimization \

--with-curl \

--enable-mbregex \

--enable-fpm \

--enable-mbstring \

--with-gd \

--with-openssl \

--with-mhash \

--enable-pcntl \

--enable-sockets \

--with-xmlrpc \

--enable-soap \

--enable-short-tags \

--enable-static \

--with-xsl \

--with-fpm-user=nginx \

--with-fpm-group=nginx \

--enable-ftp \

--enable-opcache=no

检查

echo $? 为0

安装

make && make install

将nginx的用户和PHP的用户统一:nginx

[root@web02 /server/tools/php-7.3.5]# useradd nginx -u 1111 -s /sbin/nologin -M

[root@web02 /server/tools/php-7.3.5]# id nginx

uid=1111(nginx) gid=1111(nginx) 组=1111(nginx)

[root@web02 /server/tools/php-7.3.5]# vim /application/nginx/conf/nginx.conf

worker_processes  1;

user  nginx nginx;

(编译的时候就改用nginx)

[root@web02 ~]# ln -s /application/php-7.3.5/ /application/php

3.配置php.ixi(PHP解析器配置文件)

[root@web02 ~]# cd /server/tools/php-7.3.5/

[root@web02 /server/tools/php-7.3.5]# ls php.ini-*

php.ini-development  php.ini-production

[root@web02 /server/tools/php-7.3.5]# cp php.ini-development /application/php/lib/php.ini

4.配置php fpm

[root@web02 /server/tools/php-7.3.5]# cd /application/php/etc/

[root@web02 /application/php/etc]# ls

pear.conf  php-fpm.conf.default  php-fpm.d

[root@web02 /application/php/etc]# cp php-fpm.conf.default php-fpm.conf

[root@web02 /application/php/etc]# cd php-fpm.d/

[root@web02 /application/php/etc/php-fpm.d]# ls

www.conf.default

[root@web02 /application/php/etc/php-fpm.d]# cp www.conf.default www.conf

5.启动并检查端口

[root@web02 /application/php/etc/php-fpm.d]# /application/php/sbin/php-fpm

[root@web02 /application/php/etc/php-fpm.d]# netstat -lntup|grep php

tcp        0      0 127.0.0.1:9000          0.0.0.0:*              LISTEN      11257/php-fpm: mast

6.加入开机自启vim /etc/rc.local

/application/nginx/sbin/nginx

/application/php/sbin/php-fpm

7.配置nginx转发PHP请求

[root@web02 /application/nginx/conf]# vim extra/03_blog.conf

    server {

        listen      80;

        server_name  blog.etiantian.org;

        location / {

            root  html/blog;

            index  index.html index.htm;

        }

        location ~ .*\.(php|php5)?$ {

            root html/blog;

            fastcgi_pass  127.0.0.1:9000;

            fastcgi_index index.php;

            include fastcgi.conf;

        }

    }

编写测试文件

[root@web02 /application/nginx/conf]# echo "" > ../html/blog/test_info.php

[root@web02 /application/nginx/conf]# cat ../html/blog/test_info.php

[root@web02 /application/nginx/conf]# /application/php/bin/php

[root@web02 /application/nginx/conf]# nginx -t

[root@web02 /application/nginx/conf]# nginx -s reload

8、测试PHP连接mysql

[root@web02 /application/nginx/html/blog]# vim /application/nginx/html/blog/test_mysql.php

//$link_id=mysqli_connect('主机名','用户','密码');

$link_id=mysqli_connect('localhost','root','oldboy123') or mysql_error();

if($link_id){

echo "mysql successful by oldboy.\n";

}else{

echo mysql_error();

}

?>

[root@web02 /application/nginx/html/blog]# /application/php/bin/php /application/nginx/html/blog/test_mysql.php

mysql successful by oldboy.

LNMP环境搭建成功。

你可能感兴趣的:(5.6 课堂笔记)