LNMP Web服务搭建

数据库:

1,创建用户
[root@web02 ~]# useradd mysql -s /sbin/nologin -M
[root@web02 ~]# id mysql
uid=1002(mysql) gid=1002(mysql) 组=1002(mysql)
2,上传软件到指定的目录
[root@web02 ~]# cd /server/tools/
[root@web02 /server/tools]# 
[root@web02 /server/tools]# rz
rz waiting to receive.

http://mirrors.163.com/mysql/Downloads/MySQL-5.7/ (下载地址,这里直接上传压缩包)

[root@web02 /server/tools]# ls -lsh
总用量 616M
 615M -rw-r--r-- 1 root root  615M 5月   4 20:48 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
    0 drwxr-xr-x 9 www  www    204 4月  30 12:04 nginx-1.16.0
1012K -rw-r--r-- 1 root root 1009K 4月  23 21:58 nginx-1.16.0.tar.gz

[root@web02 /server/tools]# tar xf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
[root@web02 /server/tools]# mv mysql-5.7.26-linux-glibc2.12-x86_64 /application/mysql-5.7.26
[root@web02 /server/tools]# ln -s /application/mysql-5.7.26/  /application/mysql
[root@web02 /server/tools]# ls /application/mysql/
bin  COPYING  docs  include  lib  man  README  share  support-files
3,配置配置文件
yum安装mariadb的默认的my.cnf(mysql的配置文件)
[root@web02 /server/tools]# ls -l /etc/my.cnf
-rw-r--r--. 1 root root 570 8月  16 2018 /etc/my.cnf
卸载依赖包yum remove mariadb(禁用),rpm -e --nodeps mariadb-libs(用这个卸载)
[root@web02 /server/tools]# rpm -e --nodeps mariadb-libs
[root@web02 /server/tools]# ls -l /etc/my.cnf
ls: 无法访问/etc/my.cnf: 没有那个文件或目录
[root@web02 /server/tools]# vim /etc/my.cnf 
[mysqld]
basedir = /application/mysql/
datadir = /application/mysql/data
socket = /tmp/mysql.sock
server_id = 1
port = 3306
log_error = /application/mysql/data/oldboy_mysql.err

[mysql]
socket = /tmp/mysql.sock
prompt = oldboy [\\d]>
4,初始化数据库
[root@web02 /server/tools]# rpm -qa mariadb-libs
[root@web02 /server/tools]# yum install libaio-devel -y

[root@web02 /server/tools]# mkdir -p /application/mysql/data
[root@web02 /server/tools]# chown -R mysql.mysql /application/mysql/

[root@web02 /server/tools]# /application/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/application/mysql/ --datadir=/application/mysql/data
当初始化失败提示时执行以下步骤,error类似的字符串,清空/data
cd /application/mysql/data
                rm -fr *
5,配置启动服务
[root@web02 /application/mysql/support-files]# cat /etc/systemd/system/mysqld.service
[Unit]
Description=MySQL Server by oldboy
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=/application/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
启动
[root@web02 /application/mysql/support-files]# systemctl start mysqld
[root@web02 /application/mysql/support-files]# systemctl enable mysqld
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /etc/systemd/system/mysqld.service.
[root@web02 /application/mysql/support-files]# systemctl status mysqld
● mysqld.service - MySQL Server by oldboy
   Loaded: loaded (/etc/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 一 2019-05-06 09:41:25 CST; 10s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 7285 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─7285 /application/mysql/bin/mysqld --defaults-file=/etc/my.cnf

5月 06 09:41:25 web02 systemd[1]: Started MySQL Server by oldboy.
[root@web02 /application/mysql/support-files]# netstat -lntup|grep mysql
tcp6       0      0 :::3306                 :::*                    LISTEN      7285/mysqld         
[root@web02 /application/mysql/support-files]# ps -ef|grep mysql|grep -v grep
mysql      7285      1  0 09:41 ?        00:00:00 /application/mysql/bin/mysqld --defaults-file=/etc/my.cnf
不能启动mysqld服务
[root@web02 /application/mysql/support-files]# systemctl status mysqld
● mysqld.service - MySQL Server by oldboy
   Loaded: loaded (/etc/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since 二 2019-05-07 18:25:07 CST; 1min 1s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 7477 (code=exited, status=1/FAILURE)

5月 07 18:25:05 web02 systemd[1]: Started MySQL Server by oldboy.
5月 07 18:25:07 web02 systemd[1]: mysqld.service: main process exited, code=exited, status=1/FAILURE
5月 07 18:25:07 web02 systemd[1]: Unit mysqld.service entered failed state.
5月 07 18:25:07 web02 systemd[1]: mysqld.service failed.
解决方法
[root@web02 /application/mysql-5.7.26]# cd ../mysql/data/
[root@web02 /application/mysql/data]# ls
auto.cnf  ib_buffer_pool  ibdata1  ib_logfile0  ib_logfile1  oldboy_mysql.err
[root@web02 /application/mysql/data]# rm -rf *
[root@web02 /application/mysql/data]# chown -R mysql.mysql /application/mysql/
[root@web02 /application/mysql/data]# cd ..
[root@web02 /application/mysql]# cd ..
[root@web02 /application]# cd mysql-5.7.26/
[root@web02 /application/mysql-5.7.26]# /application/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/application/mysql/ --datadir=/application/mysql/data
[root@web02 /application/mysql-5.7.26]# systemctl  restart mysqld
[root@web02 /application/mysql-5.7.26]# systemctl  status mysqld
● mysqld.service - MySQL Server by oldboy
   Loaded: loaded (/etc/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 二 2019-05-07 18:32:19 CST; 25s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 10323 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─10323 /application/mysql/bin/mysqld --defaults-file=/etc/my.cnf

5月 07 18:32:19 web02 systemd[1]: Started MySQL Server by oldboy.
6,配置环境变量登录
[root@web02 /application/mysql/support-files]# echo 'export PATH=/application/mysql/bin:$PATH' >>/etc/profile
[root@web02 /application/mysql/support-files]# tail -1 /etc/profile
export PATH=/application/mysql/bin:$PATH
[root@web02 /application/mysql/support-files]# . /etc/profile
[root@web02 /application/mysql/support-files]# echo $PATH
/application/mysql/bin:/application/nginx/sbin:/application/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@web02 /application/mysql/support-files]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
oldboy [(none)]>
成功登录。
oldboy [(none)]>quit 
Bye
[root@web02 /application/mysql/support-files]# 
如果出错就看错误日志
cat /application/mysql/data/oldboy_mysql.err 
7,修改密码
[root@web02 /application/mysql/support-files]# mysqladmin -u root password 'oldboy123'
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
交互式登录
[root@web02 /application/mysql/support-files]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

oldboy [(none)]>quit
Bye
非交互式登录(会显示密码)
[root@web02 /application/mysql/support-files]# mysql -uroot -poldboy123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

oldboy [(none)]>quit
Bye

安装PHP

yum安装:简单、方便、高效
编译安装PHP
1,安装PHP调用的库
yum install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel -y
yum install freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel libxslt-devel -y
cd /server/tools/   上传libiconv-1.16.tar.gz
tar zxf libiconv-1.16.tar.gz
cd libiconv-1.16
./configure --prefix=/application/libiconv
make && make install
cd ../
yum install libmcrypt-devel -y 
yum install mhash -y
yum install mcrypt -y
2,安装PHP
cd /server/tools/
上传php-7.3.5.tar
tar xf php-7.3.5.tar.gz
cd php-7.3.5/

./configure \
--prefix=/application/php-7.3.5 \
--enable-mysqlnd  \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir=/application/libiconv \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-fpm \
--enable-mbstring \
--with-gd \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--enable-short-tags \
--enable-static \
--with-xsl \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-ftp \
--enable-opcache=no

make && make install
[root@web02 /server/tools/php-7.3.5]# echo $?
0
将nginx的用户和PHP的用户统一:nginx
[root@web02 /server/tools/php-7.3.5]# useradd nginx -u 1111 -s /sbin/nologin -M
[root@web02 /server/tools/php-7.3.5]# id nginx
uid=1111(nginx) gid=1111(nginx) 组=1111(nginx)
前面安装nginx的时候如果是www就修改nginx配置文件(编译的时候就改用nginx,就不用这一步)
[root@web02 /server/tools/php-7.3.5]# vim /application/nginx/conf/nginx.conf
worker_processes  1;
user  nginx nginx;
创建软链接
[root@web02 ~]# ln -s /application/php-7.3.5/ /application/php
[root@web02 ~]# ls /application/php/
bin  etc  include  lib  php  sbin  var
3,配置php.ini(PHP解析器配置文件)
[root@web02 /application/php]# cd /server/tools/php-7.3.5/
[root@web02 /server/tools/php-7.3.5]# ls php.ini-*
php.ini-development  php.ini-production

[root@web02 /server/tools/php-7.3.5]# cp php.ini-development /application/php/lib/php.ini
[root@web02 /server/tools/php-7.3.5]# ls -l /application/php/lib/php.ini
-rw-r--r-- 1 root root 71648 5月   6 11:51 /application/php/lib/php.ini
4,配置PHP FPM
[root@web02 /server/tools/php-7.3.5]# cd /application/php/etc/
[root@web02 /application/php/etc]# ls
pear.conf  php-fpm.conf.default  php-fpm.d
[root@web02 /application/php/etc]# cp php-fpm.conf.default php-fpm.conf
[root@web02 /application/php/etc]# cd php-fpm.d/
[root@web02 /application/php/etc/php-fpm.d]# ls
www.conf.default
[root@web02 /application/php/etc/php-fpm.d]# cp www.conf.default www.conf
[root@web02 /application/php/etc/php-fpm.d]# ls
www.conf  www.conf.default
5,启动PHP服务
[root@web02 /application/php/etc/php-fpm.d]# /application/php/sbin/php-fpm 
[root@web02 /application/php/etc/php-fpm.d]# netstat -lntup|grep php-fpm
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      12214/php-fpm: mast 
6,开机自启动
[root@web02 /application/php/etc/php-fpm.d]# tail -2 /etc/rc.local
/application/nginx/sbin/nginx
/application/php/sbin/php-fpm
7,配置nginx转发PHP请求
        location ~ .*\.(php|php5)?$ {
            root html/blog;
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi.conf;
        }
[root@web02 /application/nginx/conf/extra]# cat 03_blog.conf
server {
        listen       80;
        server_name  blog.etiantian.org;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
        location ~ .*\.(php|php5)?$ {
            root html/blog;
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi.conf;
        }
    }
[root@web02 /application/nginx/conf]# nginx -t
nginx: the configuration file /application/nginx-1.16.0//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.16.0//conf/nginx.conf test is successful
[root@web02 /application/nginx/conf]# nginx -s reload
9,测试PHP连接mysql
[root@web02 /application/nginx/html/blog]# cat  /application/nginx/html/blog/test_mysql.php 
<?php
//$link_id=mysqli_connect('主机名','用户','密码');
    $link_id=mysqli_connect('localhost','root','oldboy123') or mysql_error();
    if($link_id){
        echo "mysql successful by oldboy.\n";
    }else{
        echo mysql_error();
    }
?>
[root@web02 /application/nginx/html/blog]# /application/php/bin/php /application/nginx/html/blog/test_mysql.php 
mysql successful by oldboy.
网页输入
http://blog.etiantian.org/test_mysql.php
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,294评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,493评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,790评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,595评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,718评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,906评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,053评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,797评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,250评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,570评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,711评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,388评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,018评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,796评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,023评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,461评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,595评论 2 350

推荐阅读更多精彩内容