说明:华南1
2核4G
磁盘100G
安装 CentOS6.8x64
查看CentOS版本【根据需要】
#lsb_release -a
(确认是6.8)
查看内核版本号
#uname -a
Linux Judian 2.6.32-696.6.3.el6.x86_64 #1 SMP Wed Jul 12 14:17:22 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
默认内核版本 2.6.32-696.6.3.el6.x86_64
关闭SELINUX (已默认关闭)
#vi /etc/selinux/config
将SELINUX=enforcing 修改为 SELINUX=disabled
关闭防火墙
暂时关闭
#service iptables stop
启动防火墙
#service iptables start
重启防火墙
#service iptables restart
永久关闭
#chkconfig iptables off
永久关闭后启用
#chkconfig iptables on
查看防火墙状态
#service iptables status
(重启,根据需要)
#reboot
检查是否存在已安装的php/mysql/php-fpm/redis/memcached等
#rpm -qa XXX
【都没安装有】
格式化云盘并加载
1、
#fdisk -l
【可看到有Disk /dev/vdb: 107.4 GB】
2、
#fdisk /dev/vdb
对数据盘进行分区。根据提示,依次输入 n,p,1,两次回车,wq,分区就开始了。
3、
#mkfs.ext3 /dev/vdb1
格式化
4、
#cd /
#mkdir data
#vi /etc/fstab
在最后添加 /dev/vdb /data ext3 defaults 0 0
系统自动挂载磁盘
(PS:手动加载 #mount /dev/vdb1 /data)
【重启】
#reboot
一、安装最新版mysql 5.6为稳定版本
参考:https://www.cnblogs.com/lzj0218/p/5724446.html
#cd /data
#mkdir tools
#cd tools
#yum install wget
【已安装】
#wget dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm .
#yum install mysql-community-release-el6-5.noarch.rpm
---------以下步骤可跳过-----------------
#vi /etc/yum.repos.d/mysql-community.repo
【可看到 Note: MySQL 5.7 is currently in development. 表示在开发中
如果愿意承担风险,可 5.7下的enable=1
】
【当前使用最新稳定版5.6】
【确定开启的MYSQL版本】
# yum repolist enabled | grep mysql
mysql-connectors-community MySQL Connectors Community 49
mysql-tools-community MySQL Tools Community 61
mysql56-community MySQL 5.6 Community Server 434
---------以上步骤可跳过----------------
【开始安装】
#yum install mysql-community-server
启动mysql
#service mysqld start
设置开机启动
#chkconfig mysqld on
初始化
#mysql_secure_installation
回车,根据提示输入Y
输入新密码
根据提示 除了这个是n
Disallow root login remotely? [Y/n] n
其他基本上是输入Y
最后出现:Thanks for using MySQL!
开放远程登录权限
#mysql -uroot -p
输入密码
mysql>use mysql;
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'YOURPASSWORD' WITH GRANT OPTION;
mysql>flush privileges;
mysql>exit;
修改root密码:
mysql>SET PASSWORD FOR 'root'@'localhost' = PASSWORD('YOURPASSWORD');
重启mysql
#service mysqld restart
【在安全组中打开对应的端口:3306】
查看mysql版本
#mysql -V
mysql Ver 14.14 Distrib 5.6.40, for Linux (x86_64) using EditLine wrapper
至此安装mysql完毕
---------------------------------------------------------
导入数据库
#mysql -uroot -p somedatabase</data/somedatabase.sql
二、安装PHP7
#service mysqld stop
#service nginx stop 【无】
#service php-fpm stop 【无】
#service iptables stop 【无】
【根据实际停止】
更新源
#rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
#rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm
可以删掉一些不用的源(此步跳过)
#ls /etc/yum.repos.d/
查看php版本列表
#yum list php*
安装php7及相关软件
#yum install php70w.x86_64 php70w-fpm.x86_64 php70w-opcache.x86_64 \
php70w-mysql.x86_64 php70w-pdo.x86_64 php70w-mcrypt.x86_64 \
php70w-mbstring.x86_64 php70w-pecl-redis.x86_64 \
php70w-pecl-memcached.x86_64 php70w-xml.x86_64 php70w-bcmath.x86_64 \
php70w-gd.x86_64
查看php版本
#php -v
--------------内容如下
PHP 7.0.30 (cli) (built: Apr 28 2018 10:41:40) ( NTS )
启动php-fpm
#service php-fpm start
自动启动php-fpm
#chkconfig php-fpm on
配置PHP
#vi /etc/php.ini
修改如下内容 (可根据情况修改)
upload_max_filesize = 8M
在 ;session.save_path = "/tmp" 下面增加
session.save_path = "/data/sessions"
#cd /data
#mkdir sessions
#chown -R daemon:daemon /data/sessions
#chmod -R 777 /data/sessions
#mkdir /data/logs
#chown -R daemon:daemon /data/logs
#chmod -R 777 /data/logs
三、安装 NginX
添加repo源
#vi /etc/yum.repos.d/nginx.repo
添加如下内容:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
查看nginx版本 (1.14.0-1.el6.ngx)
#yum list|grep nginx
安装
#yum install nginx
启动
#service nginx start
自动启动
#chkconfig nginx on
#mkdir /data/www
#mkdir /data/logs/nginx
#chown -R nginx:nginx /data/logs/nginx
配置
#vi /etc/nginx/nginx.conf
error_log /data/logs/nginx/error.log;
events {
worker_connections 1024;
use epoll; //增加此行 如果你使用Linux 2.6+,你应该使用epoll。
}
http {
access_log /data/logs/nginx/access.log main;
client_max_body_size 8m; //限制上传文件最大值,根据实际需要设置 此处设置与php.ini中的设置有关联
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
#support gzip
gzip on;
gzip_http_version 1.0;
gzip_disable "MSIE [1-6].";
gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php image/jpeg image/gif image/png;
#include /etc/nginx/conf.d/*.conf;
include /etc/nginx/vhost.conf;
配置vhost.conf
#vi /etc/nginx/vhost.conf
#some website
server {
listen 80;
server_name www.xxoo.com xxoo.com;
root /data/www/phplee/www;
index index.html index.htm index.php;
error_page 404 /404.html;
location = /404.html {
return 404 'Sorry, File not Found!';
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html; # windows dir
}
location / {
try_files $uri @rewrite;
}
location @rewrite {
set $static 0;
if ($uri ~ \.(css|js|jpg|jpeg|png|gif|ico|woff|eot|svg|css\.map|min\.map)$) {
set $static 1;
}
if ($static = 0) {
rewrite ^/(.*)$ /index.php?s=/$1;
}
}
location ~ /Uploads/.*\.php$ {
deny all;
}
location ~ \.php/ {
if ($request_uri ~ ^(.+\.php)(/.+?)($|\?)) { }
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_NAME $1;
fastcgi_param PATH_INFO $2;
fastcgi_param SCRIPT_FILENAME $document_root$1;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
重启生效
#service nginx restart
四、安装redis
更新源
#cd /data/tools
#wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm .
#rpm -ivh epel-release-6-8.noarch.rpm
安装
#yum install redis php70w-redis
配置
#vi /etc/redis.conf
maxmemory 500mb //在# maxmemory <bytes>下面添加一行 最大内存
appendonly yes //修改 启用持久化
启动及开机启动
#service redis start
#chkconfig redis on
进入测试
#redis-cli
127.0.0.1:6379>set foo bar
OK
127.0.0.1:6379>get foo
"bar"
127.0.0.1:6379>quit
PS:
ThinkPHP中这样引用就可以了
S(array('type'=>'redis','host'=>'127.0.0.1','port'=>'6379','prefix'=>'think','expire'=>600));
iptables设置
开机启动
#chkconfig iptables on
#service iptables start
---------如果没有配置文件,则-----------
#vi /etc/sysconfig/iptables
粘贴如下【默认配置文件】
# Generated by iptables-save v2.4.7 on Wed Jun 20 11:19:26 2018
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [29:3128]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 9000 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 6379 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A INPUT -p tcp -m tcp --dport 9000 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 9001 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 9002 -j ACCEPT
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
# Completed on Wed Jun 20 11:19:26 2018
查看状态
#service iptables status
保存更改
#service iptables save
重启防火墙以便生效
#service iptables restart
重启一遍看是否有异常
#service php-fpm restart
#service mysqld restart
#service redis restart
重启
#reboot
重启后测试一遍是否正常
-----------重启后修改数据库存放目录------------------
数据库存放位置修改
参考:
https://www.cnblogs.com/todosomeone/p/6025180.html
1. 停止数据库
#service mysqld stop
2、修改/etc/my.cnf
#vi /etc/my.cnf
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
依次改为(根据需要):
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/logs/mysql/mysqld.log
3、复制原来数据库目录到新目录并设置权限
#mkdir /data/mysql
#cp -rf /var/lib/mysql/* /data/mysql/
#chown -R mysql:mysql /data/mysql
#mkdir /data/logs/mysql
#cp /var/log/mysqld.log /data/logs/mysql/
#chown -R mysql:mysql /data/logs/mysql/mysqld.log
4、重启
#service mysqld restart
6、重启后测试看能不能正常用
至此,完成了服务器LNMP环境的搭配,觉得好别忘了关注我的博客哈~