架构师一定得会,LNMP服务的搭建(从入门到高手的第四步)
Posted 遙遙背影暖暖流星
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了架构师一定得会,LNMP服务的搭建(从入门到高手的第四步)相关的知识,希望对你有一定的参考价值。
LNMP的搭建
一,安装nginx
1、环境安装
可参考::https://blog.csdn.net/lv74134/article/details/118100816?spm=1001.2014.3001.5502
yum -y install gcc gcc-c++ make pcre-devel zlib-devel
2、软件编译安装
tar zxf nginx-1.12.2.tar.gz -C /opt/
cd /opt/nginx-1.12.2/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
make && make install
3、service和systemctl的优化
ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
useradd -M -s /sbin/nologin nginx
nginx -t #检查语法
nginx #启动
(1)接下进一步优化管理:systemctl
vim /etc/init.d/nginx
----------------------------------------------------------------------------------
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
COM="/usr/local/nginx/sbin/nginx"
PID="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$COM
;;
stop)
kill -s QUIT $(cat $PID)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PID)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
----------------------------------------------------------------------------
chmod +x /etc/init.d/nginx
chkconfig --add nginx
systemctl stop nginx
systemctl start nginx #可以用该命令进行管理了
(2)优化管理:service
vim /usr/lib/systemd/system/nginx.service
----------------------------------------------------------------------------------------
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile =/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.targe
-----------------------------------------------------------------------------------
chmod 754 /lib/systemd/system/nginx.service #设置754权限,安全优化
systemctl start nginx.service
systemctl enable nginx.service
现在nginx的初步安装完成,且可以用systemctl 和service进行管理
killall -3 nginx
systemctl start nginx
netstat -anpt | grep nginx
如上图,输入该主机的ip能出现该页面则证明能正常工作
这里介绍一个小工具,也可用这个验证是否nginx网页正常运行
yum -y install lynx
lynx 127.0.0.1
二、安装mysql
1、环境安装
yum -y install ncurses ncurses-devel bison cmake
2、编译安装
useradd -s /sbin/nolog mysql #添加管理的用户
tar zxf mysql-boost-5.7.20.tar.gz -C /opt
cd /opt/mysql-5.7.20 #安装版本为5.7
cmake \\
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \\
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \\
-DSYSCONFDIR=/etc \\
-DSYSTEMD_PID_DIR=/usr/local/mysql \\
-DDEFAULT_CHARSET=utf8 \\
-DDEFAULT_COLLATION=utf8_general_ci \\
-DWITH_INNOBASE_STORAGE_ENGINE=1 \\
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \\
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \\
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \\
-DMYSQL_DATADIR=/usr/local/mysql/data \\
-DWITH_BOOST=boost \\
-DWITH_SYSTEMD=1
make && make install
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \\ #指定安装目录
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \\ #指定通信文件,连接数据库和通讯的载体
-DSYSCONFDIR=/etc \\ #指定配置文件位置
-DSYSTEMD_PID_DIR=/usr/local/mysql \\ #指定存放pid文件位置
-DDEFAULT_CHARSET=utf8 \\ #支持中文等字符串
-DDEFAULT_COLLATION=utf8_general_ci \\
-DWITH_INNOBASE_STORAGE_ENGINE=1 \\ #下面三行均为存储引擎ENGINE
-DWITH_ARCHIVE_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1
-DMYSQL_DATADIR=/usr/local/mysql/data \\ #指定数据存放位置
-DWITH_BOOST=boost \\ #底层c++的运营库
-DWITH_SYSTEMD=1 #守护进程id
3,命令管理优化
chown -R mysql:mysql /usr/local/mysql
vim /etc/my.cnf #删除原来的内容设置,重新设置
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
设置环境变量
echo 'PATH=/usr/local/mysql/bin:/usr/local/lib:$PATH' >> /etc/profile
echo 'export PATH' >> /etc/profile
source /etc/profile
cd /usr/local/mysql
./bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
先便可以用systemctl 进行管理了
systemctl start mysqld
systemctl status mysqld
netstat -antp | grep 3306
4、数据库的初始化
cd /usr/local/mysql/
bin/mysqld \\
--initialize-insecure \\
--user=mysql \\
--basedir=/usr/local/mysql \\
--datadir=/usr/local/mysql/data
cp /usr/lib/systemd/system/mysqld.service /usr/lib/systemd/ #复制启动脚本
5,设置密码
mysqladmin -u root -p password #先回车,在输入两次要设置的新密码
mysql -u root -p #以root身份登录,输之前的密码
登录成功
三、安装php
1、环境安装
yum -y install \\
libjpeg \\
libjpeg-devel \\
libpng libpng-devel \\
freetype freetype-devel \\
libxml2 \\
libxml2-devel \\
zlib zlib-devel \\
curl curl-devel \\
openssl openssl-devel
libjpeg \\ #支持jpg图片
libjpeg-devel \\
libpng libpng-devel \\ #支持png图片
freetype freetype-devel \\ #字体
libxml2 \\ #支持xml
libxml2-devel \\
zlib zlib-devel \\ #压缩格式
curl curl-devel \\ #识别url
openssl openssl-devel ##xml支持扩展性标记语言,用于承载数据url 网址 openssl安全性访问
2、编译安装
cd /opt
tar jxf php-7.1.10.tar.bz2 -C /opt
cd /opt/php-7.1.10/
./configure \\
--prefix=/usr/local/php \\
--with-mysql-sock=/usr/local/mysql/mysql.sock \\
--with-mysqli \\
--with-zlib \\
--with-curl \\
--with-gd \\
--with-jpeg-dir \\
--with-png-dir \\
--with-freetype-dir \\
--with-openssl \\
--enable-fpm \\
--enable-mbstring \\
--enable-xml \\
--enable-session \\
--enable-ftp \\
--enable-pdo \\
--enable-tokenizer \\
--enable-zip
make && make install
–prefix=/usr/local/php \\ #指定安装路径
–with-mysql-sock=/usr/local/mysql/mysql.sock \\ #指定通信文件,连接数据库和通讯的载体
–with-mysqli \\
–with-zlib
–with-curl
–with-gd
–with-jpeg-dir
–with-png-dir
–with-freetype-dir
–with-openssl
–enable-fpm \\ #开启了php-fpm模块
–enable-mbstring \\ #支持多字段模块
–enable-xml \\ #支持拓展性标记语言模块
–enable-session
–enable-ftp \\ #文本协议
–enable-pdo \\ #函数库
–enable-tokenizer \\ #解释器
–enable-zip #压缩格式
3、设置配置文件
php的三个配置文件:php.ini核心配置文件,php-fpm.conf进程服务配置文件,www . conf扩展配置文件
cp php.ini-development /usr/local/php/lib/php.ini
vim /usr/local/php/lib/php.ini
939 date.timezone = Asia/Shanghai #第939行分号去掉
mysqli.default_socket =/usr/local/mysql/mysql.sock #1170添加目录位置
/usr/local/php/bin/php -m #验证/查看开启的模块
4、配置及优化FPM模块
cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf
cd ./php-fpm.d/
cp www.conf.default www.conf
cd ../
vim php-fpm.conf
-------------------------------
pid = run/php-fpm.pid # 第17行取消注释 ,开启pid支持
-------------------------------------------
/usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini #-c代表conf
netstat -natp | grep 9000 #php的端口是9000
对命令进行优化
ln -s /usr/local/php/bin/* /usr/local/bin/
5、nginx与php相关联
这一步是重点,怎么让让nginx支持PHP功能(动静页面分工)
vim /usr/local/nginx/conf/nginx.conf #nginx主配置文件中
location ~ \\.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; #修改站点目录位置
include fastcgi_params;
}
vim /usr/local/nginx/html/index.php #x=修改网络配置文件
<?php
phpinfo()
?>
systemctl stop nginx
systemctl start nginx
重启服务后,登录192.168.100.7/index.php页面,php成功
6,测试数据库是否工作正常
登录数据库并创建对象
mysql -u root -p
create database bbs;
grant all ON bbs.* TO 'bbsuser'@'%' IDENTIFIED BY 'admin123 ';
grant all ON bbs.* To 'bbsuser' @ 'localhost' IDENTIFIED BY 'admin123 ' ;
flush privileges;
vim /usr/local/nginx/html/index.php #对测试内容进行修改页面设置
<?php
$link=mysqli_connect('192.168.100.7','bbsuser','123456');
if ($link) echo "<h1>success!!</h1>";
else echo "Fall!!";
?>
systemctl restart nginx
再登录192168.100.7/index.php,出现了if判断成功,数据库正常
7,安装数据库
cd /opt
unzip Discuz_X3.4_SC_UTF8.zip -d /tmp
cd /tmp/dir_SC_UTF8/
cp -r upload/ /usr/local/nginx/html/bbs/
cd /usr/local/nginx/html/bbs/
chown -R root:nginx ./config/
chown -R root:nginx ./data/
chown -R root:nginx ./uc_client/
chown -R root:nginx ./uc_server/
chmod -R 777 ./config/
chmod -R 777 ./data/
chmod -R 777 ./uc_client/
chmod -R 777 ./uc_server/
登录http://192.168.100.7/bbs/install/index.php
搭建现在初步完成!下一篇继续优化
以上是关于架构师一定得会,LNMP服务的搭建(从入门到高手的第四步)的主要内容,如果未能解决你的问题,请参考以下文章
架构师都会,lamp下的nginx的优化(从入门到高手的第五步)
架构师必会的nginx和tomcat集群高可用动静分离(从入门到高手的第七步)