LNMP搭建
LNMP即linux+Nginx + Mysql + PHP
环境说明
主机 | IP |
---|---|
localhost | 192.168.175.150 |
nginx安装
创建nginx的用户
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx
安装依赖环境
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
安装开发工具包
[root@localhost ~]# yum -y groups mark install 'Development Tools'
创建日志存放位置
[root@localhost ~]# mkdir /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx
下载nginx
[root@localhost ~]# wget -c http://nginx.org/download/nginx-1.18.0.tar.gz
解压并编译安装
[root@localhost ~]# ls
anaconda-ks.cfg nginx-1.18.0.tar.gz
[root@localhost ~]# tar xf nginx-1.18.0.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg nginx-1.18.0 nginx-1.18.0.tar.gz
[root@localhost ~]# cd nginx-1.18.0
[root@localhost nginx-1.18.0]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@localhost nginx-1.18.0]#./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
[root@localhost nginx-1.18.0]# make install
配置环境变量
[root@localhost nginx-1.18.0]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost nginx-1.18.0]# cd
[root@localhost ~]# source /etc/profile.d/nginx.sh
启动nginx服务
[root@localhost ~]# nginx
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25
mysql安装
安装依赖包
[root@localhost ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
创建用户和组
[root@localhost ~]# groupadd -r -g 306 mysql
[root@localhost ~]# useradd -r -M -s /sbin/nologin -g 306 -u 306 mysql
下载mysql的二进制包
[root@localhost ~]# cd /usr/src/
[root@localhost src]# ls
debug kernels mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
解压软件至/usr/local/
[root@localhost src]# tar xf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost src]# cd /usr/local/
[root@localhost local]# ls
bin games lib libexec nginx share
etc include lib64 mysql-5.7.30-linux-glibc2.12-x86_64 sbin src
创建软链接
[root@localhost local]# ln -sv mysql-5.7.30-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.30-linux-glibc2.12-x86_64/"
[root@localhost local]# ll
总用量 0
drwxr-xr-x. 2 root root 6 11月 5 2016 bin
drwxr-xr-x. 2 root root 6 11月 5 2016 etc
drwxr-xr-x. 2 root root 6 11月 5 2016 games
drwxr-xr-x. 2 root root 6 11月 5 2016 include
drwxr-xr-x. 2 root root 6 11月 5 2016 lib
drwxr-xr-x. 2 root root 6 11月 5 2016 lib64
drwxr-xr-x. 2 root root 6 11月 5 2016 libexec
lrwxrwxrwx. 1 root root 36 8月 6 19:26 mysql -> mysql-5.7.30-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 8月 6 19:24 mysql-5.7.30-linux-glibc2.12-x86_64
drwxr-xr-x. 11 root root 151 8月 6 19:20 nginx
drwxr-xr-x. 2 root root 6 11月 5 2016 sbin
drwxr-xr-x. 5 root root 49 7月 16 01:15 share
drwxr-xr-x. 2 root root 6 11月 5 2016 src
修改目录/usr/local/mysql的属主属组
[root@localhost ~]# chown -R mysql.mysql /usr/local/mysql
[root@localhost ~]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 8月 6 19:26 /usr/local/mysql -> mysql-5.7.30-linux-glibc2.12-x86_64/
添加环境变量
[root@localhost ~]# ls /usr/local/mysql
bin docs include lib LICENSE man README share support-files
[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost ~]# . /etc/profile.d/mysql.sh
[root@localhost ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
建立数据存放目录
[root@localhost mysql]# mkdir /opt/data
[root@localhost mysql]# chown -R mysql.mysql /opt/data/
[root@localhost mysql]# ll /opt/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 8月 6 19:28 data
初始化数据库
[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2020-08-06T11:29:33.967151Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-08-06T11:29:34.195074Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-08-06T11:29:34.233027Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-08-06T11:29:34.289257Z 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: 1a54c5a4-d7d8-11ea-ad1e-000c29fe26ad.
2020-08-06T11:29:34.289998Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-08-06T11:29:34.929423Z 0 [Warning] CA certificate ca.pem is self signed.
2020-08-06T11:29:35.019114Z 1 [Note] A temporary password is generated for root@localhost: -8Lkgj<XvTQ(
请注意,这个命令的最后会生成一个临时密码,此处密码是 -8Lkgj<XvTQ(
配置mysql
[root@localhost ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
"/usr/local/include/mysql" -> "/usr/local/mysql/include/"
[root@localhost ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@localhost ~]# ldconfig
生成配置文件
[root@localhost ~]# cat > /etc/my.cnf <<EOF
> [mysqld]
> basedir = /usr/local/mysql
> datadir = /opt/data
> socket = /tmp/mysql.sock
> port = 3306
> pid-file = /opt/data/mysql.pid
> user = mysql
> skip-name-resolve
> EOF
配置服务启动脚本
[root@localhost ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@localhost ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
启动mysql
[root@localhost ~]# service mysqld start
Unit mysqld.service could not be found.
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
SUCCESS!
[root@localhost ~]# ps -ef|grep mysql
root 39845 1 0 19:31 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql 40023 39845 3 19:31 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=localhost.localdomain.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root 40161 12764 0 19:31 pts/0 00:00:00 grep --color=auto mysql
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 80 :::3306 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
修改密码
[root@localhost ~]# mysql -uroot -p
Enter password: 此时密码是以上生成的临时密码
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30
Copyright (c) 2000, 2020, 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.
mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye
安装php
配置yum源
[root@localhost ~]# wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm
--2020-08-06 21:08:58-- http://rpms.remirepo.net/enterprise/remi-release-7.rpm
正在解析主机 rpms.remirepo.net (rpms.remirepo.net)... 195.154.241.117, 2001:bc8:33a1:100::1
正在连接 rpms.remirepo.net (rpms.remirepo.net)|195.154.241.117|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:20440 (20K) [application/x-rpm]
正在保存至: “remi-release-7.rpm”
100%[============================================================>] 20,440 49.2KB/s 用时 0.4s
2020-08-06 21:08:59 (49.2 KB/s) - 已保存 “remi-release-7.rpm” [20440/20440])
安装依赖包
[root@localhost ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php74-php-mysqlnd libsqlite3x-devel oniguruma-devel
下载php
[root@localhost ~]# cd /usr/src/
[root@localhost src]# ls
debug kernels mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz php-7.4.7.tar.xz
[root@localhost src]# tar xf php-7.4.7.tar.xz
[root@localhost src]# ls
debug kernels mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz php-7.4.7 php-7.4.7.tar.xz
编译安装
[root@localhost php-7.4.7]#./configure --prefix=/usr/local/php7 \
--with-config-file-path=/etc \
--enable-fpm \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-openssl \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif \
--enable-ftp \
--enable-gd \
--with-jpeg \
--with-zlib \
--with-freetype \
--with-gettext \
--enable-json \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl \
--enable-posix
[root@localhost php-7.4.7]# make && make install
安装后配置
[root@localhost php-7.4.7]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@localhost php-7.4.7]# source /etc/profile.d/php7.sh
[root@localhost php-7.4.7]# php -v
PHP 7.4.7 (cli) (built: Aug 6 2020 22:10:43) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
配置php-fpm
[root@localhost php-7.4.7]# cp php.ini-production /etc/php.ini
[root@localhost php-7.4.7]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-7.4.7]# chmod +x /etc/rc.d/init.d/php-fpm
[root@localhost php-7.4.7]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@localhost php-7.4.7]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
[root@localhost php-7.4.7]# service php-fpm start
Unit php-fpm.service could not be found.
Starting php-fpm done
[root@localhost php-7.4.7]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 127.0.0.1:9000 *:*
LISTEN 0 80 :::3306 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
配置虚拟主机
创建虚拟主机并生成php测试页面
[root@localhost ~]# vim /usr/local/nginx/html/index.php
[root@localhost ~]# cat /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
[root@localhost ~]# chown -R nginx.nginx /usr/local/nginx/html/
配置nginx的配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
index index.php index.htm; //添加一个php的后缀
...取消下面几行的注释并修改路径
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;
}
[root@localhost ~]#nginx -s reload
测试页面