day 46 期中架构-LNMP web服务搭建
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
为什么选择MySQL数据库:
原因可能有以下几点:
1)性能卓越,服务稳定,很少出现异常宕机。
2)开放源代码且无版权制约, 自主性强,使用成本低。
3)历史悠久,社区及用户非常活跃,遇到问题,可以很快获取到帮助。
4)软件体积小,安装使用简单,并且易于维护,安装及维护成本低。
5)支持多种操作系统,提供多种API接口,支持多种开发语言,特别对流行的PHP语言无缝支持
6)品牌口碑效应,使得企业无需考虑就直接用之。
单机安装LNMP
先装数据库:
1、创建用户
[root@web02 ~]# useradd mysql -s /sbin/nolpgin -M
[root@web02 ~]# id mysql
uid=1004(mysql) gid=1004(mysql) 组=1004(mysql)
2、上传软件到指定目录
软件工具里LNMP下的 mysql-5.7.26
[root@web02 ~]# cd /server/tools
[root@web02 /server/tools]# rz -E
rz waiting to receive.
[root@web02 /server/tools]# ls -lsh
总用量 616M
615M -rw-r--r-- 1 root root 615M 5月 4 20:48 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
0 drwxr-xr-x 9 rsync rsync 206 4月 30 12:04 nginx-1.16.0
1012K -rw-r--r-- 1 root root 1009K 4月 23 21:58 nginx-1.16.0.tar.gz
解压:
[root@web02 /server/tools]# tar xf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
[root@web02 /server/tools]# ll
总用量 630768
drwxr-xr-x 9 root root 129 5月 6 09:13 mysql-5.7.26-linux-glibc2.12-x86_64
-rw-r--r-- 1 root root 644869837 5月 4 20:48 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
drwxr-xr-x 9 rsync rsync 206 4月 30 12:04 nginx-1.16.0
-rw-r--r-- 1 root root 1032345 4月 23 21:58 nginx-1.16.0.tar.gz
[root@web02 /server/tools]# mv mysql-5.7.26-linux-glibc2.12-x86_64 /application/mysql-5.7.26
[root@web02 /server/tools]# ll
总用量 630768
-rw-r--r-- 1 root root 644869837 5月 4 20:48 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
drwxr-xr-x 9 rsync rsync 206 4月 30 12:04 nginx-1.16.0
-rw-r--r-- 1 root root 1032345 4月 23 21:58 nginx-1.16.0.tar.gz
[root@web02 /server/tools]# ln -s /application/mysql-5.7.26/ /application/mysql
[root@web02 /server/tools]# ls -l /application/
总用量 0
lrwxrwxrwx 1 root root 26 5月 6 09:14 mysql -> /application/mysql-5.7.26/
drwxr-xr-x 9 root root 129 5月 6 09:13 mysql-5.7.26
lrwxrwxrwx 1 root root 26 4月 30 12:05 nginx -> /application/nginx-1.16.0/
drwxr-xr-x 11 root root 171 5月 5 09:40 nginx-1.16.0
[root@web02 /server/tools]# ls /application/mysql
bin COPYING docs include lib man README share support-files
相当于完成以下三步:
./configure;make;make install
[root@web02 /server/tools]# ls /application/mysql/
bin COPYING docs include lib man README share support-files
3、配置配置文件:/etc/my.cnf
[root@web02 /server/tools]# ls -l /etc/my.cnf
-rw-r--r--. 1 root root 570 8月 16 2018 /etc/my.cnf
卸载依赖包:
[root@web02 /server/tools]# rpm -e --nodeps mariadb-libs
[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
[root@web02 /server/tools]# mkdir -p /application/mysql/data
[root@web02 /server/tools]# chown -R mysql.mysql /application/mysql/
--user=mysql #<==mysql用户。
--basedir=/application/mysql/ #<==MySQL根目录。
--datadir=/application/mysql/data #<==MySQL数据文件目录。
--initialize-insecure #<==关闭MySQL安全策略,本书选择了关闭策略。
--initialize #<==开启MySQL安全策略,高安全环境采用。
[root@web02 /server/tools]# /application/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/application/mysql/ --datadir=/application/mysql/data
[root@web02 /server/tools]# ls /application/mysql/data
auto.cnf ibdata1 ib_logfile1 oldboy_mysql.err sys
ib_buffer_pool ib_logfile0 mysql performance_schema
5、配置启动服务:
[root@web02 /server/tools]# cd /application/mysql/support-files/
[root@web02 /application/mysql/support-files]# ll
总用量 24
-rw-r--r-- 1 mysql mysql 773 4月 13 21:32 magic
-rwxr-xr-x 1 mysql mysql 1061 4月 13 22:10 mysqld_multi.server
-rwxr-xr-x 1 mysql mysql 894 4月 13 22:10 mysql-log-rotate
-rwxr-xr-x 1 mysql mysql 10576 4月 13 22:10 mysql.server
[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
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /etc/systemd/system/mysqld.service.
[root@web02 /application/mysql/support-files]# systemctl status mysqld
● mysqld.service - MySQL Server by oldboy
Loaded: loaded (/etc/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since 一 2019-05-06 09:42:17 +08; 21s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Main PID: 7653 (mysqld)
CGroup: /system.slice/mysqld.service
└─7653 /application/mysql/bin/mysqld --defaults-file=/etc/my.cnf
5月 06 09:42:17 web02 systemd[1]: Started MySQL Server by oldboy.
[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
export PATH=/application/mysql/bin:$PATH
[root@web02 /application/mysql/support-files]# export PATH=/application/mysql/bin:$PATH
[root@web02 /application/mysql/support-files]# . /etc/profile
[root@web02 /application/mysql/support-files]# echo $PATH
/application/mysql/bin:/application/nginx/sbin:/application/mysql/bin:/application/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[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)]>^DBye
成功登录
如果出错就看错误日志:
日志文件
cat /application/mysql/data/oldboy_mysql.err
7、修改密码:
[root@web02 /application/mysql/support-files]# mysqladmin -u root password '111111'
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
交互式登录:
[root@web02 ~]# mysql -uroot -p
Enter password:
非交互登录:
mysql -uroot -p111111
安装PHP:
YUM安装:简单、方便、高效
编译安装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
[root@web02 /application/mysql/support-files]# rpm -qa zlib-devel libxml2-devel libjpeg-turbo-devel libiconv-devel
zlib-devel-1.2.7-18.el7.x86_64
libjpeg-turbo-devel-1.2.90-6.el7.x86_64
libxml2-devel-2.9.1-6.el7_2.3.x86_64
[root@web02 /application/mysql/support-files]# rpm -qa freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel
gd-devel-2.0.35-26.el7.x86_64
libpng-devel-1.5.13-7.el7_2.x86_64
libxslt-devel-1.1.28-5.el7.x86_64
freetype-devel-2.8-12.el7_6.1.x86_64
libcurl-devel-7.29.0-51.el7.x86_64
[root@web02 /home/oldboy/tools]# cd /server/tools/
把软件包拽进来:libiconv-1.16.tar.gz
[root@web02 /server/tools]# rz -E
rz waiting to receive.
[root@web02 /server/tools]# ll
总用量 635816
-rw-r--r-- 1 root root 5166734 5月 6 10:54 libiconv-1.16.tar.gz
-rw-r--r-- 1 root root 644869837 5月 4 20:48 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
drwxr-xr-x 9 rsync rsync 206 4月 30 12:04 nginx-1.16.0
-rw-r--r-- 1 root root 1032345 4月 23 21:58 nginx-1.16.0.tar.gz
tar zxf libiconv-1.16.tar.gz
cd libiconv-1.16
[root@web02 /server/tools/libiconv-1.16]# ./configure --prefix=/application/libiconv
make
make install
cd ../
yum install libmcrypt-devel -y
yum install mhash -y
yum install mcrypt -y
安装PHP7.3.5
cd /server/tools/
把PHP7.3.5拽进来
[root@web02 /server/tools]# rz -E
rz waiting to receive.
解压:
[root@web02 /server/tools]# tar xf php-7.3.5.tar.gz
[root@web02 /server/tools]# ll
总用量 654808
drwxrwxr-x 20 root root 4096 5月 6 11:00 libiconv-1.16
-rw-r--r-- 1 root root 5166734 5月 6 10:54 libiconv-1.16.tar.gz
-rw-r--r-- 1 root root 644869837 5月 4 20:48 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
drwxr-xr-x 9 rsync rsync 206 4月 30 12:04 nginx-1.16.0
-rw-r--r-- 1 root root 1032345 4月 23 21:58 nginx-1.16.0.tar.gz
drwxr-xr-x 14 oldboy oldboy 4096 4月 30 16:37 php-7.3.5
-rw-r--r-- 1 root root 19439026 5月 6 11:05 php-7.3.5.tar.gz
[root@web02 /server/tools]# cd php-7.3.5/
[root@web02 /server/tools/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
[root@web02 /server/tools/php-7.3.5]# echo $?
0
[root@web02 /server/tools/php-7.3.5]#make
[root@web02 /server/tools/php-7.3.5]#make install
将nginx的用户和PHP的用户统一:nginx
[root@web02 /server/tools/php-7.3.5]# id nginx
uid=998(nginx) gid=996(nginx) 组=996(nginx)
[root@web02 /server/tools/php-7.3.5]# id www
uid=1003(www) gid=1003(www) 组=1003(www)
[root@web02 /server/tools/php-7.3.5]# vim /application/nginx/conf/nginx.conf
worker_processes 1;
user nginx nginx;
(编译的时候就改用nginx)
[root@web02 /server/tools/php-7.3.5]# ln -s /application/php-7.3.5/ /application/php
[root@web02 ~]# ls /application/php/
bin etc include lib php sbin var
3、配置php.ini(PHP解析器配置文件)
[root@web02 /application/php]# 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
[root@web02 /server/tools/php-7.3.5]# ls -l /application/php/lib/php.ini
-rw-r--r-- 1 root root 71648 5月 6 11:59 /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
[root@web02 /application/php/etc/php-fpm.d]# ls
www.conf www.conf.default
5、启动PHP服务
[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 12290/php-fpm: mast
6、开机自启动
[root@web02 /application/php/etc/php-fpm.d]# tail -2 /etc/rc.local
/application/nginx/sbin/nginx
/application/php/sbin/php-fpm
7、配置nginx转发PHP请求
[root@web02 /application/nginx/conf]# cd extra/
[root@web02 /application/nginx/conf/extra]# ll
总用量 16
-rw-r--r-- 1 root root 506 5月 6 08:41 01_www.conf
-rw-r--r-- 1 root root 244 5月 5 20:26 01_www.conf.ori
-rw-r--r-- 1 root root 214 5月 5 17:55 02_bbs.conf
-rw-r--r-- 1 root root 205 5月 5 17:56 03_blog.conf
[root@web02 /application/nginx/conf/extra]# vim 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;
}
}
~
"03_blog.conf" 15L, 397C 已写入
这个配置不行:
[root@web02 /application/nginx/conf]# cat extra/03_blog.conf
server {
listen 80;
server_name blog.etiantian.org;
root html/blog;
location / {
index index.html index.htm;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web02 /application/nginx/conf/extra]# nginx -t
nginx: the configuration file /application/nginx-1.16.0//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.16.0//conf/nginx.conf test is successful
[root@web02 /application/nginx/conf/extra]# nginx -s reload
8、测试NGINX连接PHP
编写测试文件
[root@web02 /application/nginx/conf/extra]# cd ..
[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 /application/nginx/html/blog/test_info.php
成功的配置:
[root@web02 /application/nginx/conf/extra]# cat 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;
}
}
解析本地hosts文件
百度搜索 blog.etiantian.org/test_info.php
9、测试PHP连接mysql:
[root@web02 /application/nginx/html/blog]# vim test_mysql.php
//$link_id=mysqli_connect('主机名','用户','密码');
$link_id=mysqli_connect('localhost','root','111111') or mysql_error();
if($link_id){
echo "mysql successful by oldboy.\n";
}else{
echo mysql_error();
}
?>
~
"test_mysql.php" 9L, 227C 已写入
[root@web02 /application/nginx/html/blog]# /application/php/bin/php /application/nginx/html/blog/test_mysql.php
mysql successful by oldboy.
http://blog.etiantian.org/test_mysql.php
LNMP环境搭建成功。