基础环境为阿里云ECS,操作系统为CentOS 7;下面分别完成MySQL(MariaDB)、Nginx、PHP的安装与配置;最后附zsh(oh-my-zsh)和redis的安装与配置。

MySQL

CentOS7的yum源中默认已经是没有MySQL了,取而代之的是MariaDB,两者关系类似于OracleJDK与OpenJDK;直接通过命令yum -y install mariadb*安装,具体版本为:5.5.52-MariaDB。

  • 设置开机启动

    systemctl enable mariadb
  • 启动MariaDB

    systemctl start mariadb
  • 移除安全隐患

    mysql_secure_installation

    大概会执行这几个操作:为root用户设置密码、删除匿名账号、取消root用户远程登录、删除test库以及对test库的访问权、刷新授权表使修改生效。

  • 配置字符集

    vi /etc/my.cnf,在[mysqld]标签下添加

    init_connect='SET collation_connection = utf8_unicode_ci' 
    init_connect='SET NAMES utf8' 
    character-set-server=utf8 
    collation-server=utf8_unicode_ci 
    skip-character-set-client-handshake

    vi /etc/my.cnf.d/client.cnf,在[client]中添加

    default-character-set=utf8

    vi /etc/my.cnf.d/mysql-clients.cnf,在[mysql]中添加

    default-character-set=utf8

    执行systemctl restart mariadb重启服务

  • 创建用户并授权远程登陆

    执行mysql -uroot -ppassword(password替换为实际密码)登陆MariaDB。

    GRANT ALL PRIVILEGES ON *.* TO 'wangxuesong'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;

    %表示匹配所有主机,localhost表示本地;

    执行以使配置生效:

    flush privileges;

如果是阿里云主机,还要设置安全组开放3306端口才能远程访问。

Nginx

CentOS7的yum源中默认没有Nginx,得手动添加;Nginx具体版本为:nginx/1.12.1。

  • 安装Nginx Repository

    rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
  • 安装Nginx

    yum install nginx
  • 设置开机启动

    systemctl enable nginx
  • 启动Nginx

    systemctl start nginx

浏览器访问,显示Nginx默认页:

PHP

PHP具体版本为:PHP 5.4.16。

  • 安装PHP

    yum install php  php-fpm php-mysql
  • 设置开机启动并启动

    systemctl enable php-fpm
    
    systemctl start php-fpm
  • 修改php-fmp配置

    vi /etc/php-fpm.d/www.conf,将user和group设置为nginx(与nginx用户组保持一致);执行chown -R nginx:nginx /var/lib/php/session/var/lib/php/session目录用户组更改为nginx。

  • 修改Nginx配置文件以支持php-fpm

    server {
    listen       80;
    server_name  wangxs.cn;
    root         /usr/share/nginx/html;    
    index        index.html index.htm index.php;
      
    if (!-e $request_filename) {
        rewrite ^(.*)$ /index.php$1 last;
        }   
    
    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;
        }
     
    access_log  /var/log/nginx/wangxs_cn_access.log combined;
    }
    
  • 建立index.php文件

    在/usr/share/nginx/html建立index.php文件,内容如下:

    <?php
        echo phpinfo();
    ?>
  • 重启Nginx和php-fpm

    systemctl restart php-fpm
    
    systemctl restart nginx

    再次访问页面,页面内容为PHP信息。

Redis

CentOS7的yum源中也没有Redis,这次通过源码编译安装Redis;具体版本为:4.0.1。

  • 安装gcc

    yum install gcc make
  • 下载源码包

    wget http://download.redis.io/releases/redis-4.0.1.tar.gz
  • 解压文件

    tar xzf redis-4.0.1.tar.gz
  • 编译Redis

    cd redis-4.0.1
    
    make
    
    make install
  • 创建配置文件、日志等文件夹

    mkdir /etc/redis
    
    mkdir /var/redis
    
    cd /var/redis
    
    mkdir data log run
  • 修改配置

    拷贝配置文件至/etc/redis/

    cp redis.conf /etc/redis/

    配置设置

    pidfile /var/redis/run/redis_6379.pid
        
    logfile /var/redis/log/redis.log
        
    dir /var/redis/data
        
    daemonize yes
  • 服务及开机自启动

    cp ~/redis-4.0.1/utils/redis_init_script /etc/init.d/
    mv redis_init_script redis
    REDISPORT=6379
    EXEC=/usr/local/bin/redis-server
    CLIEXEC=/usr/local/bin/redis-cli
    PIDFILE=/var/redis/run/redis_${REDISPORT}.pid
    CONF="/etc/redis/redis.conf"

    在启动脚本里加入redis启动优先级信息

    #!/bin/sh
    # chkconfig: 2345 90 10
    # Simple Redis init.d script conceived to work on Linux systems
    # as it does use of the /proc filesystem

    注册服务

    vim redis.service
    [Unit]
    Description=Redis on port 6379
    [Service]
    Type=forking
    ExecStart=/etc/init.d/redis start
    ExecStop=/etc/init.d/redis stop
    [Install]
    WantedBy=multi-user.target

    设置开机启动并启动服务

    systemctl start redis
    systemctl enable redis

Zsh

  • 安装Zsh

    yum -y install zsh

    修改bash为zsh

    chsh -s /bin/zsh
    reboot
    echo $SHELL
  • 下载安装oh-my-zsh

    如果没有git,先安装git

    yum install git
    wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh

    换一套拉风的主题

    vim .zshrc   
    ZSH_THEME="ys"
最后修改:2024 年 01 月 31 日
如果觉得我的文章对你有用,请随意赞赏