Centos7 LNMP 安装以及配置
一、安装前
1.VMware下载https://pan.baidu.com/s/10aH_sztamz8HxFjxbM6hBQ,下载linux源码安装包例如Centos7 redhat。
2.准备好Linux后安装常用软件,gcc,wget,cmake。
yum install wget
yum install gcc gcc-c++
yum install cmake
二、Nginx安装配置
1.安装nginx需要安装的依赖包:
//nginx的Rewrite模块和HTTP核心模块会使用到PCRE正则表达式语法:
yum -y install pcre pcre-devel
//nginx的各种模块中需要使用gzip压缩:
yum -y install zlib zlib-devel
//安全套接字层密码库:
yum -y install openssl openssl-devel
2.下载Nginx压缩包并解压
(解压至/usr/local/src/)
cd /usr/local/src
wget http://nginx.org/download/nginx-1.12.2.tar.gz
tar -zxvf nginx-1.12.2.tar.gz
3.编译安装(/usr/local/nginx)
cd nginx-1.12.2
// configure 参数根据自身需求 --with-http_ssl_module 为开启ssl模块,如不需则可删除
./configure --prefix=/usr/local/nginx --with-http_ssl_module
make
make install
4.创建设置Nginx运行账号
groupadd nginx
useradd -M -g nginx -s /sbin/nologin nginx
cd /usr/local/nginx/conf
vim nginx.conf //设置user参数如下:
user nginx nginx
5.设置Nginx为系统服务
vim /lib/systemd/system/nginx.service
//文件内容
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
6.设置开机自启动并开启服务
systemctl enable nginx.service
//开启服务
systemctl start nginx.service
7.访问http://localhost 验证Nginx时候安装成功。
8.防火墙设置
开放防火墙80端口(默认,提供给远程访问使用)
//永久开放80端口:
firewall-cmd --zone=public --add-port=80/tcp --permanent
//重启防火墙:
firewall-cmd --reload
//查看防火墙开启状态:
systemctl status firewalld
//查看80端口是否开放成功:
firewall-cmd --zone=public --query-port=80/tcp
成功之后可使用宿主主机浏览器访问linux虚拟机测试是否可以访问。
三、PHP安装
1.安装php所需要的扩展
yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel
注:若提示yum中没有可用的软件包libmcrypt和libmcrypt-devel,则需要手动编译安装,可以先在windows宿主主机中前往libmcrypt官网下载然后使用WinSCP上传至虚拟机进行安装编译。
tar -zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure --prefix=/usr/local/libmcrypt
make
make install
2.下载php包并解压
cd /usr/local/src
wget http://hk1.php.net/get/php-7.0.31.tar.gz/from/this/mirror
tar -zxvf mirror
cd php-7.0.31
//参数因为不同版本会有所不同,根据情况自取;
./configure --prefix=/usr/local/php --disable-fileinfo --enable-fpm --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-openssl --with-zlib --with-curl --enable-ftp --with-gd --with-xmlrpc --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-gd-native-ttf --enable-mbstring --with-mcrypt=/usr/local/libmcrypt --enable-zip --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysql-sock=/var/lib/mysql/mysql.sock --without-pear --enable-bcmath
make&&make install
3.将php包解压目录中的配置文件放置到正确位置(configure命令中的--with-config-file-path设置的位置)
cp php.ini-development /etc/php.ini
4.创建并设置php-fpm运行账号
groupadd www-data
useradd -M -g www-data -s /sbin/nologin www-data
cd /usr/local/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-data
group=www-data
5.配置nginx支持php
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name localhost;
root "/usr/local/nginx/html";
location / {
index index.html index.htm index.php;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
6.重启Nginx服务、设置php-fpm为系统服务
systemctl reload nginx.service
vim /etc/systemd/system/php-fpm.service
[Unit]
Description=php-fpm
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm
PrivateTmp=True
[Install]
WantedBy=multi-user.target
7.设置开机自启动并打开
systemctl enable php-fpm.service
systemctl start php-fpm.service
8.PHP安装结束
四.安装MySQL
准备工作:完全清除电脑内原有的mysql
查看是否安装,如果有则卸载
rpm -qa mysql
//卸载
rpm -e --nodeps 文件名称
//是否存在与mysql相关的文件或目录:
whereis mysql
//是则删除,查看是否存在mariadb:
rpm -qa | grep mariadb
//存在则卸载:
rpm -e --nodeps 文件名 //文件名是上一个命令查询结果
//存在/etc/my.cnf,则需要先删除:
rm /etc/my.cnf
1.安装编译mysql需要的依赖包
yum install libevent* libtool* autoconf* libstd* ncurse* bison* openssl*
2下载mysql包并解压(到/usr/local/src目录)
cd /usr/local/src
wget http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6...
tar -zxvf mysql-5.6.38.tar.gz
3.编译安装(到/usr/local/mysql目录)
cd mysql-5.6.38
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
make //(此过程需花费大概20-30分钟)
make install
4.配置mysql,同php、Nginx
groupadd mysql
useradd -M -g mysql -s /sbin/nologin mysql
chown -R mysql:mysql /usr/local/mysql
5.初始化配置
cd /usr/local/mysql/scripts
./mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
mkdir /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql
6.设置mysql为系统服务.
vim /lib/systemd/system/mysql.service
[Unit]
Description=mysql
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecRestart=/usr/local/mysql/support-files/mysql.server restart
ExecReload=/usr/local/mysql/support-files/mysql.server reload
PrivateTmp=true
[Install]
WantedBy=multi-user.target
7.完成后续配置
//设置mysql服务开机自启动
systemctl enable mysql.service
//启动mysql
systemctl start mysql.service
//登录mysql并设置root密码:
/usr/local/mysql/bin/mysql -u root
set password=password('root');
8.mysql安装结束。