0基础自学linux运维-2.3-centOS7 安装LNMP
一、安装前操作
[root@localhost ~]#cat /etc/redhat-release
CentOS Linux
release 7.2.1511 (Core)
[root@localhost ~]#uname -r
3.10.0-327.el7.x86_64
Server: 118.x.x.178
1.1.centos7安装兼容包(已安装)
服务器端和客户端同时操作
#兼容centOS6之前的命令
yum install -y net-tools
1.2.关闭iptables(centos7默认没此项)
service iptables stop
chkconfig iptables off
不关闭也可以要让其端口通过防火墙
1.3.关闭selinux
#临时关闭:
setenforce off
#永久性关闭:
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
sed -n '/SELINUX=/p' /etc/selinux/config
shutdown -r now
1.4.关闭防火墙
#停止firewall IT网,http://www.it.net.cn
#禁止firewall开机启动
systemctl stop firewalld.service
systemctl disable firewalld.service
#或
service firewalld stop
chkconfig firewalld off
1.5.时间同步(最好要)
zabbix监控软件是一款时间性要求比较准确的的软件,所以最好点一下时间同步,如果时间相关太大有可能会出问题。
yum install ntp -y
/usr/sbin/ntpdate pool.ntp.org
echo '#time sync by hua'>>/var/spool/cron/root
echo '*/30 * * * * /usr/sbin/ntpdatepool.ntp.org >/dev/null 2>&1'>>/var/spool/cron/root
crontab -l
二、nginx安装
nginx、php用yum安装、mysql为自带的二进制安装包5.6安装
2.1.安装nginx
mkdir/disk1/tools
cd/disk1/tools
rpm -ih http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx -y
2.2.启动nginx
#启动/停止/重启nginx
service nginx start
service nginx stop
service nginx restart
service nginx status
#设置开机启动
chkconfig nginx on
#或者systemctl enable nginx.service
三、mysql安装(yum)
为了方便升级可以用yum源安装,提前是云服务器有快照,避免系统破坏mysql数据库也坏了。
一般情况下建议用二进制安装即“四、mysql安装(二进制)”
yum源安装一般为mysql最新版本除非是自己下载rpm包安装
mysql yum安装地址https://repo.mysql.com/
#下面为mysql5.7的
#https://repo.mysql.com/mysql57-community-release-el7-9.noarch.rpm
#下面为mysql5.6的(这里用mysql5.6)
https://repo.mysql.com/mysql-community-release-el7-7.noarch.rpm
注意:
yum安装mysql5.6或更高版本,用后面的remi源安装php5.4是不行的!只能安装php5.6更更高版本
3.1 mysql下载及安装(yum)
cd /disk1/tools/
wget https://repo.mysql.com/mysql-community-release-el7-7.noarch.rpm
rpm -ih mysql-community-release-el7-7.noarch.rpm
yum install mysql mysql-server mysql-devel-y
#yum install mysql-community-server mysql-community-devel mysql-community-client -y
#建立相关的数据和日志目录
mkdir -p /disk1/mysqlData/mysql5.6
mkdir -p /disk1/logs/mysql/mysql5.6/error
mkdir -p /disk1/logs/mysql/mysql5.6/slow
chown mysql.mysql -R /disk1/mysqlData
chown mysql.mysql -R /disk1/logs/mysql
3.2.修改mysql配置
#1)备份并修改my.cnf
cp /etc/my.cnf /etc/my.cnf.orig
vi /etc/my.cnf
[mysql]
default-character-set=utf8
[mysqld]
##add by hua
datadir = /disk1/mysqlData/mysql5.6
port = 3306
server_id =2
#socket = /tmp/mysql.sock
#设置默认编码,也可以取消
default-storage-engine=INNODB
character-set-server=utf8
collation-server=utf8_general_ci
log-bin=/disk1/logs/mysql/mysql5.6/mysql-bin
innodb_file_per_table=1
slow-query-log = on
slow_query_log_file =/disk1/logs/mysql/mysql5.6/slow/slowquery.log
long_query_time = 3
log-queries-not-using-indexes = on
log-error = /disk1/logs/mysql/mysql5.6/error/error.log
[mysqld_safe]
#log-error=/var/log/mysqld.log
log-error =/disk1/logs/mysql/mysql5.6/error/error.log
#建立相应的文件:
touch/disk1/logs/mysql/mysql5.6/error/error.log
chown mysql.mysql/disk1/logs/mysql/mysql5.6/error/error.log
#启动mysql
systemctl start mysqld
systemctl status mysqld
#开机启动
systemctl enable mysqld
3.3.授权
#3.修改登陆密码并授权外网段能使用hua登陆mysql,密码均为123456
mysql -uroot
set password for 'root'@'localhost' =password('123456');
GRANT ALL PRIVILEGES ON *.* TO 'hua'@'%'IDENTIFIED BY '123456' WITH GRANT OPTION;
flush privileges;
\q
#测试mysql登陆
mysql -uroot -p
#输入密码123456
show databases;
\q
#用本机win测试一下远程登陆情况,我这里用的是SQLyog客户端(也可以用Navicat for MySQL)
#提示“Connection successful”表示测试连接成功,如下图所示
四、mysql安装(二进制)
因为方便数据库的维护及迁移,centos系统推荐二进制安装,centos7开始用的是mariadb
4.1.mysql下载及安装(二进制)
#1)安装第三方remi源(可选)
cd /disk1/tools/
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
rpm --import http://rpms.famillecollet.com/RPM-GPG-KEY-remi
rpm -ih remi-release-7.rpm
#2)安装共享库
# yum --enablerepo=remi install libaio
yum install -y libaio
#3)添加用户和组
groupadd mysql
useradd -s /sbin/nologin -g mysql -M mysql
#4)下载mysql并安装
#建立相关目录,分区保放mysql二进制文件、日志、数据
mkdir /disk1/app
mkdir -p /disk1/mysqlData/mysql5.6
chown mysql.mysql -R /disk1/app
chown mysql.mysql -R/disk1/mysqlData/mysql5.6
#有人问最大的分区是home,怎么办?可以建立一个disk1以便将来当单独硬盘使用
#使用了软连接,以方便管理。一般情况系统安装在一个硬盘,数据库和网站是放在另一个硬盘
#如果有一新磁盘的话,可以把软链接删除,格式化一个/disk1出来,
#然后把/home/app和/home/mysqlData移到/disk1中就行了
#当然也可以不要软链接,直接用/home/app和/home/mysqlData,后面的目录做一下相应修改就行
mkdir /home/app
mkdir /home/mysqlData
mkdir /disk1
#建立软连接指向/home/app
ln -s /home/app /disk1/app
ln -s /home/mysqlData /disk1/mysqlData
mkdir -p /disk1/mysqlData/mysql5.6
chown mysql.mysql -R /disk1/app
chown mysql.mysql -R/disk1/mysqlData/mysql5.6
chown mysql.mysql -R /home/app
chown mysql.mysql -R /home/mysqlData
#建立相应的目录
mkdir -p/disk1/logs/mysql/mysql5.6/error
mkdir -p/disk1/logs/mysql/mysql5.6/slow
chown mysql.mysql-R /disk1/logs/mysql
#mysql下载安装
cd /tools
wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
tar -xfmysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.6.35-linux-glibc2.5-x86_64/disk1/app/mysql5.6.35
cd /disk1/app/
ln -s /disk1/app/mysql5.6.35/disk1/app/mysql
chown mysql.mysql -R mysql
chown mysql.mysql -R mysql5.6.35
ll
#4.安装mysql数据库
cdmysql5.6.35/
#centos7安装mysql要perl模块,centos6则不用
yum install -y perl-Module-Install.noarch
./scripts/mysql_install_db
--user=mysql --basedir=/disk1/app/mysql5.6.35 --datadir=/disk1/mysqlData/mysql5.6
#安装后如下图所示,提示有如下操作:
#1.在mysql二进制目录创建一个新的my.cnf,
#2.默认配置文件路径为/etc/my.cnf,如果你要使用指定的路径可以使用--defaults-file参数
#使用指定my.cnf文件路径,在安装多个mysql非常有用,有兴趣的可以看第五点知识扩展
#如有兴趣了解一机多实例(包括相机和不同版本)可以评论留言。
4.2.修改mysql配置
#1)备份并修改my.cnf
cp my.cnf my.cnf.orig
vi my.cnf
[mysql]
default-character-set=utf8
[mysqld]
##add by hua
basedir = /disk1/app/mysql5.6.35
datadir = /disk1/mysqlData/mysql5.6
port = 3306
server_id =12
#socket = /tmp/mysql.sock
#设置默认编码,也可以取消
default-storage-engine=INNODB
character-set-server=utf8
collation-server=utf8_general_ci
log-bin=/disk1/logs/mysql/mysql5.6/mysql-bin
innodb_file_per_table=1
slow-query-log = on
slow_query_log_file =/disk1/logs/mysql/mysql5.6/slow/slowquery.log
long_query_time = 3
log-queries-not-using-indexes = on
log-error =/disk1/logs/mysql/mysql5.6/error/error.log
#建立相应的文件:
touch /disk1/logs/mysql/mysql5.6/error/error.log
#2)替换默认/etc/my.cnf文件
mv /etc/my.cnf /etc/my.cnf.orig
cp my.cnf /etc/my.cnf
ll /etc/my.cnf
4.3.修改mysql启动文件
#1.复制mysql启动文件
cp support-files/mysql.server/etc/init.d/mysqld
#2.修改mysql启动文件,也可以不配置,
sed -i's#basedir=$#basedir=/disk1/app/mysql5.6.35#' /etc/init.d/mysqld
sed -i's#datadir=$#datadir=/disk1/mysqlData/mysql5.6#' /etc/init.d/mysqld
sed -n '/mysql5.6.35/p' /etc/init.d/mysqld
sed -n '/mysql5.6/p' /etc/init.d/mysqld
#5.6.35脚本需要修改,否则报如下错误:
#mysqld_safe The file
/usr/local/mysql/bin/mysqld
#does not exist or is not executable.
Please cd to the mysql installation
#解决方法一:
cd /disk1/app/mysql5.6.35/bin/
cp mysqld_safe mysqld_safe.orig
sed -i 's#/usr/local/mysql#/disk1/app/mysql5.6.35#g' mysqld_safe
sed -n '/mysql5.6.35/p' mysqld_safe
#解决方法二:直接建立软链接
#mkdir -p /usr/local/mysql/bin
#ln -s /disk1/app/mysql/bin/mysqld /usr/local/mysql/bin/mysqld
#如报下面错误,原因是my.cnf没有指定basedir和datadir
#/etc/init.d/mysqld start
#Starting MySQL... ERROR! The server quit
without updating PID file (/disk1/mysqlData/mysql5.6/vm5.pid)
4.4.启动服务 配置环境变量 授权
#启动mysql
systemctl start mysqld
ps -ef |grep mysql|grep -v grep
#设置开机启动
chkconfig mysqld on
#2.设置mysql环境变量
echo 'exportPATH=$PATH:/disk1/app/mysql/bin' >>/etc/profile.d/mysql.sh
cat /etc/profile.d/mysql.sh
#使环境变量生效
source /etc/profile.d/mysql.sh
#3.修改登陆密码并授权外网段能使用hua登陆mysql,密码均为123456
mysql -uroot
set password for 'root'@'localhost' =password('123456');
GRANT ALL PRIVILEGES ON *.* TO 'hua'@'%'IDENTIFIED BY '123456' WITH GRANT OPTION;
flush privileges;
\q
#测试mysql登陆
mysql -uroot -p
#输入密码123456
show databases;
\q
#用本机win测试一下远程登陆情况,我这里用的是SQLyog客户端(也可以用Navicat for MySQL)
#提示“Connection successful”表示测试连接成功,如下图所示
五、php安装
5.1.安装php
#安装第三方remi源(安装过的可以跳过)
cd /disk1/tools/
rpm -Uv hhttps://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
rpm --import http://rpms.famillecollet.com/RPM-GPG-KEY-remi
rpm -ih remi-release-7.rpm
#安装php依赖
yum --enablerepo=remi install -y curlcurl-devel
#安装php5.6版本,如果没有指定remi-php56,则默认安装php5.4版本(mysql5.6 yum安装不能用php5.4)
#remi源安装php5.6(gd+freetype)+imagick
yum --enablerepo=remi,remi-php56 -yinstall php-imap php-ldap php-odbcphp-pear
yum --enablerepo=remi,remi-php56 -y installphp php-devel php-mysql php-gd php-bcmath php-mbstring php-mcrypt php-mhashphp-pdo php-pecl-imagick php-fpm
yum --enablerepo=remi,remi-php56install -y php-pecl-redis php-pecl-memcache
#下面是根据需要安装
#yum --enablerepo=remi,remi-php56 install-y php-pecl-memcache php-pecl-proprophp-pecl-raphf php-cli php-magickwand
#yum --enablerepo=remi,remi-php56 install-y php-pecl-mongo php-pecl-memcached php-pecl-raphf php-tidy php-pecl-yar #
5.2.修改php相关配置(可选)
#1)修改php.ini
cp /etc/php.ini /etc/php.ini.orig
sed -i 's/max_execution_time = 30/max_execution_time= 300/' /etc/php.ini
sed -i 's/max_input_time = 60/max_input_time= 300/' /etc/php.ini
sed -i 's/memory_limit = 128M/memory_limit= 128M/' /etc/php.ini
sed -i 's/post_max_size = 8M/post_max_size= 16M/' /etc/php.ini
sed -i 's/upload_max_filesize = 2M/upload_max_filesize= 2M/' /etc/php.ini
sed -i 's/;date.timezone =/date.timezone =PRC/' /etc/php.ini
egrep "max_execution|max_input_time =|memory_limit|post_max|upload_max|date.timezone=" /etc/php.ini
#开启php简写及php跨页传参
sed -i 's/short_open_tag =Off/short_open_tag = On/' /etc/php.ini
sed -i 's/session.use_trans_sid =0/session.use_trans_sid = 1/' /etc/php.ini
egrep "short_open_tag|session.use_trans_sid" /etc/php.ini
#2)修改php-fpm.conf
#这里主要是修改yum安装后子配置文件/etc/php-fpm.d/www.conf
cd /etc/php-fpm.d/
cp www.conf www.conf.orig
egrep";listen.owner =|;listen.group =|user =|group =|pm.max_children=|pm.max_requests =" www.conf
sed -i 's/apache$/nginx/g' www.conf
sed -i 's/nobody/nginx/g' www.conf
sed -i 's/pm.max_children =50/pm.max_children = 70/' www.conf
sed -i 's/;pm.max_requests =500/pm.max_requests = 500/' www.conf
egrep ";listen.owner =|;listen.group =|user =|group =|pm.max_children=|pm.max_requests =" www.conf
egrep "request_slowlog_timeout|slowlog =|php_admin_value\[error_log\]=|php_value\[session.save_path\]" www.conf
sed -i 's#slowlog = /var/log/php-fpm/www-slow.log#slowlog =/disk1/logs/php5.6/www-slow.log#' www.conf
sed -i 's#;request_slowlog_timeout = 0#request_slowlog_timeout = 15#' www.conf
sed -i 's#/var/log/php-fpm/www-error.log#/disk1/logs/php5.6/www-error.log#' www.conf
egrep "request_slowlog_timeout|slowlog =|php_admin_value\[error_log\]=|php_value\[session.save_path\]" www.conf
#建立相当目录
mkdir -p /disk1/logs/php5.6
chown nginx.nginx -R
/disk1/logs/php5.6
#因为用yum安装session目录默认用户是apache,如果修改为nginx也需要相应地修改
#否则会出现网站登陆不了的情况
chown root.nginx -R /var/lib/php/session
5.3.建立网站目录并修改nginx配置让其支持php
#1)建立网站目录
mkdir -p /disk1/www/hualinux.com
echo '<?php echo "this is php test";?>' >/disk1/www/hualinux.com/index.php
cat /disk1/www/hualinux.com/index.php
#建立相关目录
mkdir -p /disk1/logs/nginx
chown nginx.nginx -R /disk1/logs/nginx
#2)修改nginx配置使其支持php
cd /etc/nginx/conf.d/
cp default.conf default.conf.orig
#让nginx支持php,编辑default.conf,第10行添加index.php
#再把30-36行注解去掉即可。
vimdefault.conf +10
注:如果配置34行的$document_root不行的话就改为/usr/share/nginx/html1 server {
2 listen 80;
3 server_name hualinux.com www.hualinux.com;
4
5 #charset koi8-r;
6 access_log /disk1/logs/nginx/hualinux.access.log main;
7 error_log /disk1/logs/nginx/hualinux.error.log;
8
9 location / {
10 root /disk1/www/hualinux.com;
11 index index.php index.html index.htm;
12 }
…
31 location ~ \.php$ {
32 root /disk1/www/hualinux.com;
33 fastcgi_buffer_size 128k;
34 fastcgi_buffers 32 32k;
35 fastcgi_pass 127.0.0.1:9000;
36 fastcgi_index index.php;
37 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
38 include fastcgi_params;
39 }
…
}
#语法检查
nginx -t
#重启nginx
service nginx restart
#修改php-fpm配置后一定要记得重启php-fpm
service php-fpm restart
#一定要确保进行用户是nginx
ps -ef |grep php-fpm
5.4.网站测试
#1)域名解析
因为我在新网购买的是hualinux.com域名,故先登陆新网,添加域名解析A记录到这台主机IP
大概过5分钟后ping www.hualinux.com,能ping对应的域名即可#2)浏览器访问
打开浏览器输入www.hualinux.com,如下图则表示测试成