linux搭建Lnmp(centos7.4+nginx1.14+mysql5.7.20)

1 准备工作

yum -y update
mkdir /usr/local/service //软件安装目录
mkdir /usr/local/service/temp //软件临时文件目录
mkdir /usr/local/service/log //软件日志目录
mkdir code
cd code

2 安装nginx1.14

  参考资料 nginx 中文文档 http://www.nginx.cn/doc/

yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

//可以在https://nginx.org/en/download.html查看下载地址
wget -c https://nginx.org/download/nginx-1.14.1.tar.gz
tar -zxvf nginx-1.14.1
cd nginx-1.14.1

   进入目录进行安装

groupadd www
useradd -r -g www www
//./configure是对安装进行配置;
./configure \ --prefix=/usr/local/service/nginx \ --conf-path=/usr/local/service/nginx/conf/nginx.conf \ --pid-path=/usr/local/service/nginx/conf/nginx.pid \ --lock-path=/usr/local/service/nginx/conf/nginx.lock \ --error-log-path=/usr/local/service/nginx/log/error.log \ --http-log-path=/usr/local/service/nginx/log/access.log \ --with-http_gzip_static_module \ --http-client-body-temp-path=/usr/local/service/nginx/temp/client \ --http-proxy-temp-path=/usr/local/service/nginx/temp/proxy \ --http-fastcgi-temp-path=/usr/local/service/nginx/temp/fastcgi \ --http-uwsgi-temp-path=/usr/local/service/nginx/temp/uwsgi \ --http-scgi-temp-path=/usr/local/service/nginx/temp/scgi
make && make install
/usr/local/service/nginx/temp #指定的临时文件需要进行手动创建
 

   安装成功之后 /usr/local/service/ 会出现nginx目录

   配置安装启动 centos7自启项已不用chkconfig改为:systemctl

cd /usr/lib/systemd/system
vim nginx.service

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/service/nginx/sbin/nginx -c /usr/local/service/nginx/conf/nginx.conf
ExecReload=/usr/local/service/nginx/sbin/nginx -s reload
ExecStop=/usr/local/service/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

 启动配置详解

[Unit]:服务的说明
Description:描述服务
After:描述服务类别

[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:启动、重启、停止命令全部要求使用绝对路径

[Install]服务安装的相关设置,可设置为多用户

保存,退出

systemctl daemon-reload   //使文件生效
systemctl enable nginx.service //添加开机启动
systemctl start nginx.service //启动服务

 

2 安装php 7.0.32

  安装准备

yum -y install libxml* ncurses-devel ncurses libgcrypt* libtool* bison bison-devel unzip
yum -y install libmcrypt-devel mhash-devel libxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel
wget http://cn2.php.net/distributions/php-7.0.32.tar.gz
tar zxvf php-7.0.32.tar.gz
cd php-7.0.32
./configure --prefix=/usr/local/service/php --with-config-file-path=/usr/local/service/php/etc --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysql --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-xmlrpc --with-xsl --with-zlib --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-sockets --enable-sysvsem --enable-xml --enable-zip
make && make install
cp php.ini-production /usr/local/service/php/etc/php.ini
cd /usr/local/service/php/etc
cp php-fpm.conf.default php-fpm.conf
cd php-fpm.d/
cp www.conf.default www.conf

使用vim命令对www.conf的内容进行如下修改
user = www
group = www
listen = 127.0.0.1:9000
pm.max_children = 100
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35

vim php-fpm.conf
pid = /usr/local/service/php/etc/run/php-fpm.pid
error_log = /usr/local/service/log/php/php-fpm.log

//给予权限
chown -R www:www /usr/local/service/php
chmod -R 775 /usr/local/service/php/

 添加开机启动项

vim php-fpm.service
[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target

[Service]
Type=simple
PIDFile=/usr/local/service/php/etc/run/php-fpm.pid
ExecStart=/usr/local/service/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/service/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -SIGINT $MAINPID

[Install]
WantedBy=multi-user.target

 保存退出

systemctl enable mysqld.service
systemctl start php-fpm.service

 3 搭建mysql-5.7.20

   安装准备

yum -y install?gcc gcc-devel gcc-c++ gcc-c++-devel autoconf* automake* zlib* libxml*ncurses-devel ncurses libgcrypt* libtool* cmake openssl openssl-devel bisonbison-devel perl-Data-Dumper boost boost-doc boost-devel
groupadd mysql
useradd -r -g mysql mysql
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
tar zxvf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.20-linux-glibc2.12-x86_64 /usr/local/service/mysql/
mkdir /usr/local/service/mysql/data
chown -R mysql.mysql /usr/local/service/mysql/

   配置

/usr/local/mysql/bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data

# 如果出现以下错误:

2018-12-21 13:49:52 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2018-12-21 13:49:52 [ERROR]   Child process: /usr/local/service/mysql/bin/mysqldterminated prematurely with errno= 32
2018-12-21 13:49:52 [ERROR]   Failed to execute /usr/local/service/mysql/bin/mysqld --bootstrap --datadir=/usr/local/service/mysql/data --lc-messages-dir=/usr/local/service/mysql/share --lc-messages=en_US --basedir=/usr/local/service/mysql
-- server log begin --

-- server log end --

# 则使用以下命令

/usr/local/service/mysql/bin/mysqld --user=mysql --basedir=/usr/local/service/mysql/ --datadir=/usr/local/service/mysql/data

# 如果出现以下错误:

/usr/local/service/mysql/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

# 则执行以下命令:

yum -y install numactl
yum install libaio* -y

继续安装

[root@iZwz9isciehk4uy9n9ijcyZ code]# /usr/local/service/mysql/bin/mysqld --user=mysql --basedir=/usr/local/service/mysql/ --datadir=/usr/local/service/mysql/data --initialize
2018-12-21T05:55:39.904526Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-12-21T05:55:40.919936Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-12-21T05:55:41.043206Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-12-21T05:55:41.107398Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 0c404f57-04e5-11e9-8014-00163e0ed3c5.
2018-12-21T05:55:41.109529Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-12-21T05:55:41.110052Z 1 [Note] A temporary password is generated for root@localhost: xdgpH+eTp4HN

编辑mysql配置

vim /etc/my.cnf
[mysqld]
basedir=/usr/local/service/mysql
datadir=/usr/local/service/mysql/data
socket=/usr/local/service/log/mysql/mysql.sock
default-character-set=utf8
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
log-error=/usr/local/service/log/mysql/error.log
pid-file=/usr/local/service/log/mysql/mysql.pid
sql_mode ='NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
slow_query_log =1
long_query_time=1
slow_query_log_file=/usr/local/service/log/mysql/slow_query.lo
[mysqld_safe]
log-error=/usr/local/service/log/mariadb/mariadb.log
pid-file=/usr/local/service/log/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

添加开机启动

cd /usr/lib/systemd/system
vim mysql.service

[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/service/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
#Restart=on-failure
#RestartPreventExitStatus=1
#PrivateTmp=false

加入开机启动项

systemctl enable mysqld.service

cat /root/.mysql_secret  为mysql安装root 密码

进入mysql

/usr/local/service/mysql/bin/mysql -u root -p
mysql> SET PASSWORD FOR 'root'@localhost=PASSWORD('password');
use mysql
//开启远程链接
GRANT ALL PRIVILEGES ON *.* TO 'mysql'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
flush privileges;

4 配置nginx+php+mysql

cd /usr/local/service/nginx/conf
cp nginx.conf nginx.conf_back
mkdir vhosts
vim nginx.conf

user  www www;
worker_processes  1;

error_log  /usr/local/service/log/nginx/error.log crit;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;
pid       /usr/local/service/nginx/conf/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;
    access_log  /usr/local/service/log/nginx/access.log  main;
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types       text/plain application/x-javascript text/css application/xml;
    gzip_vary on;

    tcp_nodelay on;

    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;

    client_max_body_size 2m;


    include /usr/local/service/nginx/conf/vhosts/*.conf;
    #gzip  on;

}

 在vhosts新建服务配置

 

vim default.conf
server {
        listen       80;
        server_name  localhost;
        index index.php index.htm index.html;
        root /home/html/default;
        location / {
            #del index.php
            if ( -f $request_filename) {
                break;
            }
            if (!-e $request_filename) {
                rewrite  ^(.*)$  /index.php?s=/$1  last;
                break;
            }
        }
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include /usr/local/service/nginx/conf/fastcgi_params;
            fastcgi_split_path_info   ^(.+/.php)(/.+)$;
            fastcgi_param PATH_INFO  $fastcgi_path_info;
            fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            #include        fastcgi_params;
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
                expires 30d;
        }
        location ~ .*\.(js|css)?$
        {
                expires 1h;
        }
        # 配置页面静态化
        #include /alidata/server/nginx-1.12.2/conf/rewrite/default.conf;
        access_log  /usr/local/service/log/nginx/access_default.log;
        error_log /usr/local/service/log/nginx/error_default.log;
}

 

 

  

posted @ 2018-12-20 18:34  CandyChen  阅读(366)  评论(0)    收藏  举报