LNMP 基础服务配置(运维笔记)

LNMP 基础服务配置(运维笔记)

#在集群中配置FQDN,有助于进行区分主机身份 server01 server01.lnmp.com 自己设置
echo "你自己的IP地址 server01.lnmp.com server01" >> /etc/hosts
#网卡配置/etc/sysconfig/network
vim /etc/sysconfig/network
#修改HOSTNAME的值为server01(自己设定)
HOSTNAME=server01

###########################################

#centos6.x系列 关闭iptables
service iptables stop
#关闭开机自启动
chkconfig iptables off

#关闭selinux
setenforce 0 &>>/dev/null
#修改配置文件  永久关闭

yum源配置

#建立光盘挂载目录文件夹
 mkdir /dvd1 /dvd2        
#手动挂载光盘 顺便调整光盘顺序  在vmware里虚拟机设置里添加
 mount /dev/sr0 /dvd2
 mount /dev/sr1 /dvd1
#lsblk查看是否挂载成功
#添加到开启加载脚本  开机自动挂载光盘
 echo "mount /dev/sr0 /dvd2" >> /etc/rc.local
 echo "mount /dev/sr1 /dvd1" >> /etc/rc.local


##############################
#配置本地yum源

cd /etc/yum.repos.d
mkdir bak
#移动默认源 备份并使其失效
 mv ./* ./bak
#按照挂载光盘位置,配置光盘源

本地yum源配置参考

软件管理 本地yum源配置(运维笔记)_Rkun18的博客-CSDN博客

安装vim

yum -y install vim


#配置vim默认显示行号
echo "set nu" >> /root/.vimrc
#搜索关键字高亮
sed -i "8calias grep='grep --color'" /root/.bashrc
#当前窗口重新加载配置
source /root/.bashrc

网络校时

#安装校时命令和服务
 yum -y install ntp
#开启ntpd服务
 service ntpd start
#开机自启ntpd
 chkconfig ntpd on

创建 soft目录 导入源码

[root@server01 soft]# ls 
mysql-5.6.33.tar.gz  nginx-1.14.2.tar.gz  php-7.2.12.tar.gz

mysql安装

安装脚本

#!/bin/bash
#源码编译安装MySQL
mysql_install() {
    #1、创建用户 自己设置
`id mysql` &>/dev/null
[ $? -ne 0 ] && useradd -s /sbin/nologin -M mysql
#2、解决依赖
yum install -y cmake
yum install -y ncurses-devel
#3、编译安装
cd /root/soft
tar zxvf mysql-5.6.33.tar.gz
cd mysql-5.6.33
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DSYSCONFDIR=/etc \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci\
-DWITH_SSL=bundled
make && make install
#配置文件
rm -rf /etc/my.cnf
cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
#授权并初始化数据库
chown -R mysql:mysql /usr/local/mysql
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
#配置服务、自启动和环境变量
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
service mysqld start
chkconfig --add mysqld
echo 'PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile
#删除匿名用户
#设置root域名的密码
rpm -qa|grep expect
if [ $? -ne 0 ];then
   yum -y install expect
fi
#导入环境变量PATH
export PATH=/usr/local/mysql/bin:$PATH
#初始化root密码 删除匿名用户
echo '#!/usr/bin/expect
set timeout 60
spawn mysql_secure_installation
expect {
"enter for none" { send "\r"; exp_continue}
"Y/n" { send "Y\r" ; exp_continue}
"password" { send "自己设置的密码\r"; exp_continue}
"Cleaning up" { send "\r"}
}
interact ' > mysql_secure_installation.exp
chmod +x mysql_secure_installation.exp
./mysql_secure_installation.exp
}
#脚本开始时间
start_time=`date +%s`
#执行的脚本代码
mysql_install
#脚本结束时间
end_time=`date +%s`
#脚本执行花费时间
const_time=$((end_time-start_time))
echo 'Take time is: '$const_time's'

Nginx安装

#进入安装目录/usr/local/nginx
[root@server01 nginx]# ls
conf  html  logs  sbin   
#conf 配置文件 html 网站默认目录logs 日志 sbin  可执行文件  [软件的启动 停止 重启等] 
#进入sbin执行nginx
[root@server01 nginx]# cd sbin
[root@server01 sbin]# ls
nginx
[root@server01 sbin]# ./nginx
[root@server01 sbin]# ps aux |grep nginx
root      19191  0.0  0.1  46872  1204 ?        Ss   19:48   0:00 nginx: master process ./nginx
www       19192  0.0  0.1  47304  1780 ?        S    19:48   0:00 nginx: worker process
root      19194  0.0  0.0 103340   892 pts/0    S+   19:49   0:00 grep --color nginx

开启nginx服务后,你可以访问你配置的IP地址,进入页面

Welcome to nginx!

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

说明开启成功

服务脚本配置

#nginx编译包里默认没有服务启动脚本模板 下载
https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/
[root@server01 sbin]# cd /etc/init.d
#上传脚本到/etc/init.d目录下
[root@server01 init.d]# ls
auditd            iptables      messagebus  nginx    restorecond  svnserve
blk-availability  kdump         mysqld      ntpd     rsyslog      udev-post
crond             killall       netconsole  ntpdate  sandbox
functions         lvm2-lvmetad  netfs       postfix  saslauthd
halt              lvm2-monitor  network     rdisc    single
ip6tables         mdmonitor     nfs-rdma    rdma     sshd
[root@server01 init.d]# chmod +x nginx

修改配置

 vim /etc/init.d/nginx
 #执行文件路径  第22行
 nginx="/usr/local/nginx/sbin/nginx"
#配置文件路径  第25行
 NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

添加自启动

 chmod +x /etc/init.d/nginx
 chkconfig --add nginx
 chkconfig nginx on

自定义nginx服务脚本

[root@server01 init.d]# vim mynginx
[root@server01 init.d]# cat  mynginx
#!/bin/bash
#nginx管理文件位置
EXEC=/usr/local/nginx/sbin/nginx
start(){
  $EXEC
}

stop(){
  $EXEC -s quit

}

restart(){
  stop
  start
}

reload(){
  $EXEC -s reload
}

configtest(){
  $EXEC -t

}

#调用执行

case "$1" in
    start)
       start;;
    stop)
       stop;;
    restart)
       restart;;
    reload)
      reload;;
    configtest)
      configtest;;
     *)
     echo '{start|stop|restart|reload|configtest}'
     ;;
esac
#测试命令
[root@server01 init.d]# service mynginx start
[root@server01 init.d]# service nginx status
nginx (pid 1828 1827) 正在运行.
[root@server01 init.d]# service nginx abc
Usage: /etc/init.d/nginx {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}


[root@server01 init.d]# service mynginx stop
[root@server01 init.d]# service nginx status
nginx 已停

PHP安装

#进入安装目录soft 解压
tar zxf php-7.2.12.tar.gz


cd php-7.2.12


#配置


 ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-ftp --with-gd --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --with-libzip --enable-soap --without-pear --with-gettext --disable-fileinfo --enable-maintainer-zts
#解决依赖
yum -y install libxml2-devel libjpeg-devel libpng-devel freetype-devel curl-devel openssl-devel
#编译安装
make && make install





#查看php安装目录
cd /usr/local/php


bin :php相关命令目录  php      phpize、php-config在源码编译扩展时用 
etc : 配置文件目录                                        
include : php默认类库                                      
lib   : php第三方扩展类库                                   
php  :   man文档文件                                       
sbin :php-fpm执行文件                                 
var  :log日志目录  run运行目录  保存pid文件     

#复制配置文件
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

cp /root/soft/php-7.2.12/php.ini-development /usr/local/php/etc/php.ini
#添加启动服务,添加环境变量

```bash
#启动服务
 cp /root/soft/php-7.2.12/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
 chmod +x /etc/init.d/php-fpm
 chkconfig --add php-fpm


#环境变量
 echo 'PATH=/usr/local/php/bin:$PATH' >> /etc/profile
source /etc/profile

php安装脚本执行配置

#!/bin/bash
php_install(){
#php编译安装
#和nginx使用相同的用户,如果没有就创建 
`id www` &> /dev/null
[ $? -ne 0 ] && useradd -s /sbin/nologin -M www
#解决依赖
yum -y install libxml2-devel libjpeg-devel libpng-devel freetype-devel curl-devel openssl-devel
#解压
tar xvf php-x.x.xx.tar.gz
cd php-x.x.xx   #注意你自己的源码版本
#编译安装php
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-ftp --with-gd --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --with-libzip --enable-soap --without-pear --with-gettext --disable-fileinfo --enable-maintainer-zts && make && make install
#配置文件初始化
cp php.ini-development /usr/local/php/etc/php.ini
#php-fpm服务配置文件
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
#php-fpm服务子配置文件
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
#配置服务及其环境变量
cp /root/soft/php-x.x.xx/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
service php-fpm start
chkconfig --add php-fpm
echo 'PATH=/usr/local/php/bin:$PATH' >> /etc/profile
}
#脚本开始时间
start_time=`date +%s`
#执行的脚本代码
php_install
#脚本结束时间
end_time=`date +%s`
#脚本执行花费时间
const_time=$((end_time-start_time))
echo 'Take time is: '$const_time's'

Nginx+php-fpm配置

#编写测试文件
vim /usr/local/nginx/html/index.php
[root@server01 ~]# cat  /usr/local/nginx/html/index.php
<?php
    phpinfo();



#在nginx.conf中配置  



[root@server01 ~]# vim /usr/local/nginx/conf/nginx.conf
[root@server01 ~]# cat  /usr/local/nginx/conf/nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        root html;
        location / {
           # root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
        #    root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}


重启服务

[root@server01 ~]# service nginx reload
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
重新载入 nginx:                                           [确定]

基础配置已完成

你可能感兴趣的:(运维,运维,笔记,vim,linux,nginx)