Linux下安装nginx+php+mysql+redis

常用命令

查看系统版本:

head -n 1 /etc/issue

lsb_release -a    (centos 7 )

查看系统位数

getconf LONG_BIT

lsof -i 8080  查看8080端口的访问详情

内核升级

[root@localhost ~]# uname -a   ##旧版

Linux localhost.localdomain 2.6.32-279.el6.i686 #1 SMP Fri Jun 22 10:59:55 UTC 2012 i686 i686 i386 GNU/Linux

[root@localhost ~]# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org

[root@localhost ~]# rpm -ivh http://www.elrepo.org/elrepo-release-6-5.el6.elrepo.noarch.rpm

[root@localhost ~]# yum --enablerepo=elrepo-kernel install kernel-lt -y  ##此步会有点慢

[root@localhost ~]# vim /etc/grub.conf default=1 改为 default=0  ##设置默认以3.10核心启动,默认是按照旧的核心启动

[root@localhost ~]# reboot

[root@localhost ~]# uname -a  ##升级后的版本

CentOS 6.x 安装 php7.0

一:检查当前是否有安装php

rpm -qa|grep php

如果有安装PHP,那么请先删除这些安装包:

 yum remove php*

2.安装php源

Centos 5 安装php源:

rpm -ivh http://mirror.webtatic.com/yum/el5/latest.rpm

CentOs 6 安装php源:

  rpm -ivh http://mirror.webtatic.com/yum/el6/latest.rpm

CentOs 7 安装php源和epel扩展源:

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

rpm -ivh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

3.现在开始安装php

安装php5.5的基本安装包:

 yum install php55w php55w-gd php55w-mbstring php55w-mysql php55w-fpm

安装php5.6的基本安装包:

 yum install php56w php55w-gd php56w-mbstring php56w-mysql php56w-fpm

安装php7.0的基本安装包:

  yum install php70w php70w-gd php70w-mbstring php70w-mysql php70w-fpm

删除PHP:

#rpm -qa|grep php

删除依赖:

#rpm -e php-pdo-5.1.6-27.el5_5.3

二:nginx的yum在线安装

添加源

wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm

安装源库

chmod +x nginx-release-centos-6-0.el6.ngx.noarch.rpm

rpm -i nginx-release-centos-6-0.el6.ngx.noarch.rpm

安装nginx

yum -y install nginx

安装完成后的默认配置文件路径

默认nginx配置文件: /etc/nginx/nginx.conf 【nginx主要的配置文件】

默认nginx的ssl配置文件: /etc/nginx/conf.d/ssl.conf 【配置SSL证书的,也可以并入到nginx.conf文件里】

默认nginx的虚拟主机配置文件: /etc/nginx/conf.d/virtual.conf 【如同Apache的虚拟主机配置,也可以并入到nginx.conf文件里】

默认的web_root文件夹路径: /usr/share/nginx/html 【web目录夹,放置Magento主程序】

配置iptables

iptables -I INPUT 5 -p tcp --dport 80 -j ACCEPT

(这个步骤,我之前机器上已经装过apache所以就忽略了)

安装后的目录:


配置目录:

/etc/nginx/

项目目录:

/usr/share/nginx/html/

安装Composer

CentOS下Composer的安装和使用

1. 下载composer.phar

curl -sS https://getcomposer.org/installer | php

2.把composer.phar移动到环境下让其变成可执行

mv composer.phar /usr/local/bin/composer

3.测试

composer -V

中国全量镜像

composer config -g repo.packagist composer https://packagist.phpcomposer.com

安装 php扩展

apt-get install php7.0-mbstring

apt-get install php7.0-dom

yum install -ylibtool-ltdl php70w-dom php70w-mbstring php70w-mcrypt

yum install php70w-pear php70w-devel

压缩命令

zip -r etm-m-om.zip etm-m-om

解压目录

unzip  mydata.zip   -d  mydatabak    #压缩mydata目录

rpm -qa|grep php

yum install update -y

rpm -ivh http://mirror.webtatic.com/yum/el6/latest.rpm

rpm -ivh http://mirror.webtatic.com/yum/el6/latest.rpm

安装php7.0的基本安装包:

yum install php70w php70w-gd php70w-mbstring php70w-mysql php70w-fpm

安装nginx

wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm  

安装源库

chmod +x nginx-release-centos-6-0.el6.ngx.noarch.rpm

rpm -i nginx-release-centos-6-0.el6.ngx.noarch.rpm

安装nginx

yum -y install nginx

配置iptables 80端口 ;

配置目录:

/etc/nginx/

项目目录:

/usr/share/nginx/html/

安装 php扩展

apt-get install php70w-mbstring

apt-get install php70w-dom

nginx 设置

vi /etc/nginx/conf.d/default

server {

    listen       80;

    server_name  10.207.21.113;

    #charset koi8-r;

    #access_log  /var/log/nginx/log/host.access.log  main;

    root /var/www/html/etm-m/public ;

    index index.php index.html index.htm;

gzip on;

gzip_min_length 1k;

gzip_buffers 4 16k;

#gzip_http_version 1.0;

gzip_comp_level 8;

gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;

gzip_vary off;

gzip_disable "MSIE [1-6]\.";

    location / {

       #root   /var/www/html/etm-m/public;

       #index  index.php index.html index.htm;

       try_files $uri $uri/ /index.php?$query_string ;

    }

    #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   /usr/share/nginx/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           /var/www/html/etm-m/public;

        fastcgi_pass   127.0.0.1:9000;

        try_files $uri /index.php=404 ;

        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;

    #}

}

部署负载

nginx:

user  nginx;

worker_processes  1;

error_log  /var/log/nginx/error.log warn;

pid        /var/run/nginx.pid;

events {

    worker_connections  1024;

}

http {

    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    upstream myServer{

        ip_hash ;

        server  10.207.21.111:80;

        server  10.207.21.113:80;

   }

    server{

        listen 80;

        server_name etm ;

        location / {

           proxy_pass  http://myServer ;

           proxy_set_header Host  $host ;

           proxy_set_header X-Real-IP  $remote_addr ;

           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ;

       }

   }

    sendfile        on;

    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

}

~

负载的 conf

server {

    listen       80;

    server_name  localhost;

    #charset koi8-r;

    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {

        root   /usr/share/nginx/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   /usr/share/nginx/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  /scripts$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;

    #}

}

上传项目赋予项目运行权限

chown -R :nginx etm-m-om

cd etm-m-om

chmod -R 777 public

chmod -R 777 storage

chmod -R 775 bootstrap/cache

php artisan config:cache

php artisan route:cache

r

php artisan session:table

composer dump-autoload

php artisan migrate

部署告警

启动命令

nohup php artisan redis:subscribe &

安装MySQL

Step1: 检测系统是否自带安装mysql

# yum list installed | grep mysql

Step2: 删除系统自带的mysql及其依赖命令:

要删除干净mysql

# yum -y remove mysql-libs.x86_64

Step3: 给CentOS添加rpm源,并且选择较新的源命令:

# wget dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm

# yum localinstall mysql-community-release-el6-5.noarch.rpm

# yum repolist all | grep mysql

# yum-config-manager --disable mysql55-community

# yum-config-manager --disable mysql56-community

# yum-config-manager --enable mysql57-community-dmr

# yum repolist enabled | grep mysql

Step4:安装mysql 服务器命令:

# yum install mysql-community-server

Step5: 启动mysql命令:

# service mysqld start

Step6: 查看mysql是否自启动,并且设置开启自启动命令:

# chkconfig --list | grep mysqld

# chkconfig mysqld on

Step7: mysql安全设置命令:

# mysql_secure_instalation

修改mysql初始密码:

IK290yd7Dx2

方法一:

# /etc/init.d/mysqld stop

# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &

# mysql -u root mysql

mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root';

mysql> FLUSH PRIVILEGES;

mysql> quit

注:5.7 版本的 Password 字段已经改名为 authentication_string; 所以密码语句需要修改为:

UPDATE user SET authentication_string=PASSWORD('new') where USER='root';

赋权限

ALTER USER 'root'@'localhost' IDENTIFIED BY '45UIkdW230..';

grant all privileges on *.*  to  'root'@'%'  identified by '45UIkdW230..'  with grant option;

 grant all privileges on *.* to 'root'@'%' identified by '45UIkdW230..';

45UIkdW230

修改mysql时间:

rm -rf  /etc/localtime

cp  /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

-select now() 

MySQL 主主同步

机器A

server-id=101

user=mysql

log-bin=mysql-bin

log-slave-updates

slave-skip-errors=all

sync_binlog=1

auto_increment_increment=1

auto_increment_offset=1

binlog-do-db=test1

binlog-do-db=test1

binlog-ignore-db=db1

binlog-ignore-db=db2

机器B

server-id=102

user=mysql

log-bin=mysql-bin

log-slave-updates

slave-skip-errors=all

sync_binlog=1

auto_increment_increment=2

auto_increment_offset=1

binlog-do-db=test1

binlog-do-db=test1

binlog-ignore-db=db1

binlog-ignore-db=db2

在机器A MySQL Client中插入: 

change master to

master_host='机器B的IP',

master_user='数据库用户名',

master_password='数据库密码',

master_log_file='mysql-bin.000001',

master_log_pos=154;

在机器B MySQL Client中插入:

change master to

master_host='机器A的IP',

master_user='数据库用户名',

master_password='数据库密码',

master_log_file='mysql-bin.000001',

master_log_pos=154;

MySQL 主从同步

机器A

server-id=101

user=mysql

log-bin=mysql-bin

机器B

server-id=102

user=mysql

log-bin=mysql-bin

在机器B MySQL Client中插入:

change master to

master_host='机器A的IP',

master_user='数据库用户名',

master_password='数据库密码',

master_log_file='mysql-bin.000001',

master_log_pos=154;

安装Redis

IK290yd7Dx2

vi /etc/redis.conf

# find # requirepass foobared

#replace it with your password

requirepass somePassword

redis 127.0.0.1:6379[1]> config set requirepass my_redis 

OK 

redis 127.0.0.1:6379[1]> config get requirepass 

1) "requirepass" 

2) "my_redis"  

不重启Redis设置密码:

在配置文件中配置requirepass的密码(当redis重启时密码依然有效)。

redis 127.0.0.1:6379> config set requirepass test123

查询密码:

redis 127.0.0.1:6379> config get requirepass

(error) ERR operation not permitted

密码验证:

redis 127.0.0.1:6379> auth test123

OK

HTTPS 设置


在使用git进行代码更新时,出现认证错误,起初认为是账户和密码输入错误,可是经过再三尝试,终于确定是git版本太低问题,因此将git升级到2.x版本。以下是升级步骤。

[root@test ~]# git --version

git version 1.7.1

 1、安装依赖

[root@test ~]# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel

[root@test ~]# yum install -y perl-ExtUtils-MakeMaker package

2、卸载系统原有git

[root@test ~]# rpm -e git

3、下载git源码包

[root@test ~]# wget https://www.kernel.org/pub/software/scm/git/git-2.0.0.tar.gz

4、安装git

[root@test ~]# tar xf git-2.0.0.tar.gz

[root@test tools]# cd git-2.0.0

[root@test git-2.0.0]# ./configure

[root@test git-2.0.0]# make

[root@test git-2.0.0]# make install

[root@test git-2.0.0]# cd

5、验证

[root@test ~]# git --version

git version 2.0.0

项目配置:

etm-m-link-check  8013

etc-m-warn-report 8012

etc-m-com         8010

etc-m-com-warn    8011

安装目录:

查看安装目录 # whereis php

ubuntu 安装软件

apt-cache search 软件名称

vi

gg           : 跳转到文件头

Shift+g   : 跳转到文件末尾

centos 安装扩展

CentOS 7 安装 PHP 7.0以及memcache和redis扩展

安装EPEL

EPEL 是 Extra Packages for Enterprise Linux 的缩写(EPEL),是用于 Fedora-based Red Hat Enterprise Linux (RHEL) 的一个高质量软件源,所以同时也适用于 CentOS 或者 Scientific Linux 等发行版。

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

yum install epel-release

安装PHP 采用webtatic的编译版本

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

yum install php70w php70w-devel php70w-bcmath php70w-fpm php70w-mbstring php70w-pdo php70w-soap php70w-mysql php70w-gd

安装memcached for php7

git clone https://github.com/php-memcached-dev/php-memcached.git

cd php-memcached

git checkout php7

phpize

./configure --disable-memcached-sasl

make && make install

add

extension=memcached.so

to php.ini

安装redis for php7

git clone https://github.com/phpredis/phpredis.git

cd phpredis

git checkout php7

phpize

./configure

make && make install

add

extension=redis.so

to php.ini

推荐阅读更多精彩内容