Linux搭建LNMP环境

必备工具

  • lrzsz

yum install -y lrzsz

lrzsz(Linux服务器和window互传文件工具)
sz /文件名:将选定的文件发送(send)到本地机器
rz:运行该命令会弹出一个文件选择窗口,从本地选择文件上传到服务器(receive)

  • VIM

rpm -qa|grep vim  #查询一个包是否被安装

如果 vim 已经正确安装,会返回下面的三行代码:

[root@centos]# rpm -qa |grep vim
vim-minimal-7.0.109-6.el5
vim-common-7.0.109-7.2.el5
vim-enhanced-7.0.109-7.2.el5

  • gcc-c++

yum -y install gcc-c++
  • wget

yum -y install wget

挂载磁盘

查看磁盘使用

fdisk -l

格式化磁盘使用

mkfs.xfs -f 磁盘名

查看磁盘使用情况

df -lh

挂载容量最大的磁盘到/data,挂载磁盘命令:mount 磁盘名称 目录,例:

mount /dev/vdb /data

自动在开机时挂载磁盘:

vim /etc/fstab

在末行添加

#磁盘名称            目录             磁盘格式      ----                 - - - -
/dev/vdb             /data                xfs        defaults              1 1

Nginx

创建软件目录,方便管理

mkdir -p /application/tools

下载Nginx

cd /application/tools
wget http://nginx.org/download/nginx-1.14.0.tar.gz
tar xf nginx-1.14.0.tar.gz

安装Nginx依赖

yum install -y pcre-devel openssl-devel

pcre-devel 让nginx拥有正则的能力,rewrite等都需要正则匹配
openssl-devel 让nginx拥有开启https的能力。

创建Nginx管理用户

useradd -s /sbin/nologin -M nginx

编译配置Nginx

cd /application/tools/nginx-1.14.0
./configure --prefix=/application/nginx-1.14.0 --user=nginx --group=nginx --with-http_stub_status_module  --with-http_ssl_module

–prefix=/application/nginx-1.14.0 置编译安装目录
–user=nginx 配置nginx的管理用户为nginx用户
–group=nginx 配置nginx的管理组为nginx组
–with-http_stub_status_module 配置使用Nginx的监控模块
–with-http_ssl_module 配置应用认证模块(https需要此模块,需要openssl-devel依赖

编译安装Nginx

make && make install

为Nginx安装目录创建软链接(方便以后管理)

ln -s /application/nginx-1.14.0/ /application/nginx

为Nginx命令创建软链接加进root环境变量(方便使用)

ln -s /application/nginx/sbin/nginx /sbin/

精简化Nginx配置文件

sed -i "/^$/d;/#/d" /application/nginx/conf/nginx.conf

添加systemctl启动脚本

cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=nginx
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStartPre=/application/nginx/sbin/nginx -t -c /application/nginx/conf/nginx.conf
ExecStart=/application/nginx/sbin/nginx -c /application/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

启动Nginx

nginx
或者
systemctl start nginx

查看Nginx进程

 ps -ef | grep [n]ginx
 pgrep -lf nginx

测试Nginx服务端口(80)

ss -lnt | grep 80
lsof -i:80
netstat -lntup | grep nginx

curl测试Nginx首页

curl -s 本机IP

有看到Welcome to nginx!就是成功了

把Nginx添加到启动项

cd /etc/rc.d && chmod +x rc.local && vim rc.local

在末尾添加以下命令:

#开机启动Nginx
nginx

重新加载nginx配置
nginx -s reload

MySQL

安装MySQL依赖环境

yum install  -y  ncurses-devel libaio-devel autoconf numactl

ncurses-devel提供字符终端处理库
libaio-devel包的作用是为了支持同步I/O

下载MySQL

cd /application/tools
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz

删除mariadb组件

rpm -qa | grep mariadb
[结果1]
[结果2]
……
yum remove [结果1] [结果2] ……

下载依赖环境

yum install  -y  bison-devel ncurses-devel libaio-devel gcc gcc-c++ automake autoconf numactl

解压MySQL5.7

tar xf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz -C /application
mv /application/mysql-5.7.25-linux-glibc2.12-x86_64  /application/mysql-5.7.25
ln -s /application/mysql-5.7.25/ /application/mysql

创建管理用户&&授权

useradd -s /sbin/nologin -M mysql
chown -R mysql.mysql /application/mysql-5.7.25/

创建目录和文件并且分配属主和属组

mkdir -p /data/mysql/{data,logs}
touch /data/mysql/logs/{mysql-error.log,mysql-slow.log}
chown -R mysql.mysql /data/mysql
mkdir /var/run/mysql
chown -R mysql.mysql /var/run/mysql

初始化数据库

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

–initialize-insecure选项参数可以不自动生成初始root密码。

复制脚本及PATH变量赋值

\cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod 700 /etc/init.d/mysqld
echo 'PATH=$PATH:/application/mysql/bin' >> /etc/profile
source /etc/profile

编辑配置文件

vim /etc/my.cnf
[mysql]

# CLIENT #
port                           = 24824
socket                         = /tmp/mysql.sock

[mysqld]

# GENERAL #
user                           = mysql
default-storage-engine         = InnoDB
socket                         = /tmp/mysql.sock
pid-file                       = /var/run/mysql/mysql.pid
basedir                        = /application/mysql
server-id                      = 1
port                           = 24824
default-time-zone              = '+08:00'

# MyISAM #
key-buffer-size                = 32M
myisam-recover-options         = FORCE,BACKUP

# SAFETY #
max-allowed-packet             = 16M
max-connect-errors             = 1000000
skip-name-resolve
#sql-mode                       = STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,NO_AUTO_VALUE_ON_ZERO
sql-mode                       = ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,NO_AUTO_VALUE_ON_ZERO
sysdate-is-now                 = 1
innodb                         = FORCE
secure-file-priv               = ''

# DATA STORAGE #
datadir                        = /data/mysql/data/

# BINARY LOGGING #
log-bin                        = /data/mysql/data/mysql-bin
expire-logs-days               = 14
binlog-format                  = mixed
sync-binlog                    = 1
#GTID:
gtid-mode                      = on
enforce-gtid-consistency       = on

# CACHES AND LIMITS #
tmp-table-size                 = 32M
max-heap-table-size            = 32M
query-cache-type               = 0
query-cache-size               = 0
max-connections                = 500
thread-cache-size              = 50
open-files-limit               = 65535
table-definition-cache         = 1024
table-open-cache               = 200

# INNODB #
innodb-flush-method            = O_DIRECT
innodb-log-files-in-group      = 2
innodb-log-file-size           = 256M
innodb-flush-log-at-trx-commit = 1
innodb-file-per-table          = 1
innodb-buffer-pool-size        = 8G

# LOGGING #
log-error                      = /data/mysql/logs/mysql-error.log
log-queries-not-using-indexes  = 1
slow-query-log                 = 1
slow-query-log-file            = /data/mysql/logs/mysql-slow.log

启动MySQL

/etc/init.d/mysqld start

初始化root密码

mysql_secure_installation
==========================================================================
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: n

New password: <==此处输入新密码(如果低于八位数密码,稍后会有系统提示)

Re-enter new password: <==此处重复输入新密码

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 

把MySQL添加进启动项

cd /etc/rc.d && chmod +x rc.local && vim rc.local

在末尾添加以下命令

#开机启动mysql
mkdir -p /var/run/mysql
chown -R mysql.mysql /var/run/mysql
/etc/init.d/mysqld start

关闭防火墙

查看防火墙是否开启

firewall-cmd --state

如果是running的话,使用以下命令关闭防火墙

systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动

重启MySQL

systemctl restart mysqld.service ,或者
/etc/init.d/mysqld [stop|start|restart|reload]

PHP

添加webtatic的PHP的yum源

yum install epel-release
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

安装PHP及PHP常用模块

yum install -y php72w-fpm php72w-opcache php72w-mysql php72w-common php72w-cli php72w-devel php72w-mbstring php72w-xml

启动PHP

systemctl start php-fpm
或
php-fpm     ##使用了此方法启动,在使用systemctl关闭php-fpm就会报错,此时如果要重启php只能够使用kill

检查端口与进程
端口
php默认启用9000端口,如果使用使用socket,那么php就不会再监听端口

ss -lnt | grep 9000

进程

ps -ef | grep [p]hp
或
pgrep -lf php-fpm

修改PHP的管理用户

sed -i 's/apache/nginx/g' /etc/php-fpm.d/www.conf
systemctl restart php-fpm
ps -ef | grep [p]hp-fpm

此时再查看,就发现php已更改启动用户为nginx

把PHP添加到启动项

cd /etc/rc.d && chmod +x rc.local && vim rc.local

在末尾添加以下命令:

#开机启动PHP
systemctl start php-fpm.service   #centOS 7 使用
service php-fpm start   #centOS 6 使用

php.ini 参数参考

date.timezone = "Asia/Shanghai"	时区设置
max_execution_time = 1800	每个脚本的最大执行时间,单位秒
pdo_mysql.default_socket = /tmp/mysql.sock	

重启PHP

systemctl restart php-fpm.service 
或者
service php-fpm restart 

系统部署

核对时区
在进行下面的系统部署之前,务必使系统时间、MySQL时间、php时间与当地时间保持一致,即时区一致,比如都是东八区。
查看系统时间

date

登录MySQL,查看MySQL时间

mysql -uroot -p
输入密码
select now();

查看php时间

vim time.php
<?php
echo date('Y-m-d H:i:s',time());
保存退出,然后执行
php time.php

LNMP环境搭建完成

猜你喜欢

转载自blog.csdn.net/qq_41049126/article/details/92089706
今日推荐