[原] CentOS 7 安装 nginx, php mysql 套件

3,023 阅读1分钟

初始化系统

系统权限部分可以参考此文档, 保障安全, 避免踩坑 CentOS 进行服务器完善和升级

设置软件源

  • aliyun , remi, mysql
# 设置aliyun库
$ wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

# 设置 remi 库, 设置 mysql 库
$ yum install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

# 由于mysql 版权方面的限制, centos 7 没有内置mysql 服务器, 必须从mysql 官方进行安装
$ yum install http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
  • 设置 nginx 源

源地址: nginx: Linux packages 创建 vi /etc/yum.repos.d/nginx.repo , 并且填充以下内容来安装 yum repository 库

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/
gpgcheck=0
enabled=1

替换 OS 为 “rhel” 或 “centos” 替换 OSRELEASE 为 5”, “6”, 或“7”

更新元数据

# 更新元数据
$ yum makecache

安装软件

常用软件和nmp 软件

# 更新系统
$ yum update

# 安装常用软件
$ yum install vim git supervisor redis

# 安装 php 基于 remi , 所以需要安装 remi 源
# 如果需要安装其他版本, 则需要将 repo=remi-php7x 
$ yum install --enablerepo=remi-php70 php php-pdo php-fpm php-mbstring php-mcrypt php-gd php-mysqli php-zip

# 安装 nginx
$ yum install nginx --enablerepo=nginx

# 安装 mysql server
$ yum install mysql-server

启动服务

# 启动服务
$ systemctl start php-fpm mysqld nginx supervisord

开机自启动

# 开机启动
$ systemctl enable php-fpm mysqld nginx supervisord

初始化数据库

启动mysql并且获取密码

$ systemctl start mysqld

# mysql 5.7 在安装完成的时候会生成一个临时密码, 我们需要找到错误日志 `/var/log/mysqld.log`来获取这个临时密码
# use below command to see the password:
$ grep 'temporary password' /var/log/mysqld.log 
[Note] A temporary password is generated for root@localhost: _ab3BAKulW?r

**初始化 mysql **

$ mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root: ******

New password: ******

Re-enter new password: ******

The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : *

Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) :  Y

Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : N


... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N

 ... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y

Success.

All done! 

设置密码的方法

$ mysql -uroot -p******
mysql> set password for 'root'@'localhost' = password('markzhao123456');
mysql> exit

软件配置

配置nginx

配置 nginx 时候的运行组

# /etc/nginx/nginx.conf
user nginx nginx;

配置nginx虚拟主机

server{
    listen 80;
    # 如果这里是 IP, 则才会允许访问, 否则, 扯破牛蛋也访问不到
    server_name www.domain.com ;
    index index.php index.html index.htm default.html default.htm default.php;
    root /webdata/www/domain/public;

	# 这里注意和服务器自带不同的是
	# fastcgi_param  SCRIPT_FILENAME  /scripts/$fastcgi_script_name;
	# fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
	# 会导致 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ .*\.(js|css)?$ {
        expires 12h;
    }

    
    access_log /webdata/logs/domain_access.log;
    error_log /webdata/logs/domain_error.log;
}

配置php

配置 php-fpm

# /etc/php-fpm.d/www.conf
user = nginx
group = nginx

配置 php.ini

# 时区
date.timezone = Asia/Shanghai

配置 session 是可写状态

$ chown -R nginx:nginx /var/lib/php/session/

配置系统允许访问

# 配置 http
$ firewall-cmd --permanent --zone=public --add-service=http
# 配置 3306
$ firewall-cmd --permanent --zone=public --add-port=3306/tcp
# 重启 防火墙
$ firewall-cmd --reload

配置数据库用户

CREATE USER 'remote'@'%' IDENTIFIED WITH mysql_native_password AS 'U*OSy)iKk$XO9dMB';
GRANT ALL PRIVILEGES ON *.* TO 'remote'@'%' REQUIRE NONE WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;
GRANT ALL PRIVILEGES ON `1dailian\_v2`.* TO 'remote'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES

补充

下载安装mysql

获取最新下载地址: dev.mysql.com/downloads/m…

下载地址: dev.mysql.com/get/Downloa… dev.mysql.com/get/Downloa…

# 下载
$ wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-client-5.7.13-1.el7.i686.rpm
$ wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-server-5.7.13-1.el7.i686.rpm

# 安装
yum localinstall mysql-community-client**.rpm
yum localinstall mysql-community-server**.rpm

参考文章