1.安装MYSQL
- 下载软件包,这里使用的是免编译安装包,建议存放在/usr/local/src/目录中,这里我的安装包是64位;可以使用uname -i 查看系统版本信息。
[root@localhost ~]# uname -i x86_64 [root@localhost ~]# cd /usr/local/src/ [root@localhost src]# wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
- 检查本地是否安装了mysql
[root@localhost src]# rpm -qa | grep mysql
- 如果已经安装了mysql,卸载mysql
rpm -e 已存在mysql全名
- 解压文件到/usr/local/下,重新命名mysql
[root@localhost src]# tar -zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz -C /usr/local/ [root@localhost src]# cd /usr/local/ [root@localhost local]# mv mysql-5.7.17-linux-glibc2.5-x86_64/ mysql
- 建立mysql用户,启动mysql需要用该用户
[root@localhost local]# useradd -s /sbin/nologin mysql
- 创建一个存放mysql数据文件的目录
[root@localhost local]# mkdir -p /data/mysql
- 更改权限,不更改后续录
[root@localhost local]# chown -R mysql:mysql /data/mysql/
- 初始化mysql数据库,basedir是mysql包所在路径,datadir是定义存放数据库的位置,默认情况下,错误日志也会记录在这个目录中;此时会自动生成数据库用户的登录密码。
[root@localhost bin]# cd /usr/local/mysql/bin/ [root@localhost bin]# ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql 2018-03-16T10:23:01.715827Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2018-03-16T10:23:42.354610Z 0 [Warning] InnoDB: New log files created, LSN=45790 2018-03-16T10:23:48.611642Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2018-03-16T10:23:48.867955Z 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: 1da3bfed-2904-11e8-bba0-000c295a487b. 2018-03-16T10:23:48.974095Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2018-03-16T10:23:49.049147Z 1 [Note] A temporary password is generated for root@localhost: hr%sHeAaw1/f
- 配置mysql;默认情况系统默认有/etc/my.cnf,所以会提示是否覆盖,直接按“y”即可。
root@localhost support-files]# cd /usr/local/mysql/support-files/ [root@localhost support-files]# cp my-default.cnf /etc/my.cnf cp: overwrite ‘/etc/my.cnf’? y
- 打开/etc/my.cnf修改文件;port定义mysql服务监听的端口,如果定义默认就是3306。server_id定义mysql服务监听该mysql服务的ID号,该参数用于主从配置的时候。socket定义mysql服务监听的套接字地址。
# For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the # *** default location during install, and will be replaced if you # *** upgrade to a newer version of MySQL. [mysqld] # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # These are commonly set, remove the # and set as required. basedir = /usr/local/mysql datadir = /data/mysql port = 3306 server_id = 128 socket = /tmp/mysql.sock # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. join_buffer_size = 128M sort_buffer_size = 2M read_rnd_buffer_size = 2M sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
- 复制mysql.server到/etc/init.d/下,修改启动脚本文件。修改处datadir=/data/mysql。修改其属性
[root@localhost support-files]# cp ./mysql.server /etc/init.d/mysqld [root@localhost support-files]# chmod 755 /etc/init.d/mysqld [root@localhost support-files]# vim /etc/init.d/mysqld # MySQL daemon start/stop script. # Usually this is put in /etc/init.d (at least on machines SYSV R4 based # systems) and linked to /etc/rc3.d/S99mysql and /etc/rc0.d/K01mysql. # When this is done the mysql server will be started when the machine is # started and shut down when the systems goes down. # Comments to support chkconfig on RedHat Linux # chkconfig: 2345 64 36 # description: A very fast and reliable SQL database engine. # Comments to support LSB init script conventions ### BEGIN INIT INFO # Provides: mysql # Required-Start: $local_fs $network $remote_fs # Should-Start: ypbind nscd ldap ntpd xntpd # Required-Stop: $local_fs $network $remote_fs # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: start and stop MySQL # Description: MySQL is a very fast and reliable SQL database engine. ### END INIT INFO # If you install MySQL on some other places than /usr/local/mysql, then you # have to do one of the following things for this script to work: # # - Run this script from within the MySQL installation directory # - Create a /etc/my.cnf file with the following information: # [mysqld] # basedir=<path-to-mysql-installation-directory> # - Add the above to any other configuration file (for example ~/.my.ini) # and copy my_print_defaults to /usr/bin # - Add the path to the mysql-installation-directory to the basedir variable # below. # # If you want to affect other MySQL variables, you should make your changes # in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files. # If you change base dir, you must also change datadir. These may get # overwritten by settings in the MySQL configuration files. basedir= datadir= /data/mysql
- 将mysqld服务加入到系统服务列表中,使其开机启动。
[root@localhost support-files]# chkconfig --add mysqld [root@localhost support-files]# chkconfig mysqld on
- 启动服务。
[root@localhost support-files]# systemctl start mysqld
查看mysql进程和端口监听是否正常,正常则成功安装和配置了mysql。
[root@192 bin]# ps aux | grep mysqld root 4837 0.0 0.0 11764 1592 pts/0 S 22:47 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/192.168.183.130.pid mysql 5043 0.2 10.4 1248636 195108 pts/0 Sl 22:47 0:05 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/192.168.183.130.err --pid-file=/data/mysql/192.168.183.130.pid --socket=/tmp/mysql.sock --port=3306 root 5917 0.0 0.0 112644 952 pts/0 S+ 23:24 0:00 grep --color=auto mysqld [root@192 bin]# netstat -lnp | grep 3306 tcp6 0 0 :::3306 :::* LISTEN 5043/mysqld
2.安装PHP
- 下载PHP源码包。
[root@192 ~]# cd /usr/local/src/ [root@192 src]# wget http://mirrors.sohu.com/php/php-7.2.3.tar.gz
- 解压源码包。
root@192 src]# tar -zxvf php-7.2.3.tar.gz -C /usr/local
- 创建帐号,该帐号用来运行php-fpm服务。在LNMP环境中,PHP以一个服务php-fpm的形式出现,独立存在于Linux系统中
[root@192 src]# useradd -s /sbin/nologin www
- 安装PHP依赖组件(包括Nignx依赖)
yum -y install wget vim pcre pcre-devel openssl openssl-devel libicu-devel gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses ncurses-devel curl curl-devel krb5-devel libidn libidn-devel openldap openldap-devel nss_ldap jemalloc-devel cmake boost-devel bison automake libevent libevent-devel gd gd-devel libtool* libmcrypt libmcrypt-devel mcrypt mhash libxslt libxslt-devel readline readline-devel gmp gmp-devel libcurl libcurl-devel openjpeg-devel
- 配置编译选项。
[root@192 php] ./configure --prefix=/usr/local/php \ --with-config-file-path=/usr/local/php/etc \ --enable-fpm \ --with-fpm-user=www \ --with-fpm-group=www \ --enable-mysqlnd \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd \ --enable-mysqlnd-compression-support \ --with-iconv-dir \ --with-freetype-dir \ --with-jpeg-dir \ --with-png-dir \ --with-zlib \ --with-libxml-dir \ --enable-xml \ --disable-rpath \ --enable-bcmath \ --enable-shmop \ --enable-sysvsem \ --enable-inline-optimization \ --with-curl \ --enable-mbregex \ --enable-mbstring \ --enable-intl \ --with-libmbfl \ --enable-ftp \ --with-gd \ --enable-gd-jis-conv \ --with-openssl \ --with-mhash \ --enable-pcntl \ --enable-sockets \ --with-xmlrpc \ --enable-zip \ --enable-soap \ --with-gettext \ --disable-fileinfo \ --enable-opcache \ --with-pear \ --enable-maintainer-zts \ --without-gdbm \
- 编译安装。
[root@192 php] make && make install
- 配置php.ini文件。
[root@192 php] cp php.ini-development /usr/local/php/etc/php.ini [root@192 php] cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf [root@192 php] cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf [root@192 php] vim /usr/local/php/etc/php.ini expose_php = Off short_open_tag = ON max_execution_time = 60 max_input_time = 60 memory_limit = 128M post_max_size = 32M date.timezone = Asia/Shanghai mbstring.func_overload=2
- 配置www.conf文件,取消以下注释并修改优化其参数。
[root@192 php]# vim /usr/local/php/etc/php-fpm.d/www.conf listen.owner = www listen.group = www listen.mode = 0660 listen.allowed_clients = 127.0.0.1 pm = dynamic listen.backlog = -1 pm.max_children = 50 pm.start_servers = 20 pm.min_spare_servers = 5 pm.max_spare_servers = 35
- 设置php-fpm自动启动。
[root@192 php]# cp /usr/local/php/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm [root@192 php]# chmod +x /etc/init.d/php-fpm [root@192 php]# chkconfig --add php-fpm [root@192 php]# chkconfig php-fpm on
- 启动php-fpm。
[root@192 php]# systemctl start php-fpm
3.安装Nignx
- 下载和解压Nignx。
[root@192 src]# wget http://nginx.org/download/nginx-1.13.9.tar.gz [root@192 src]# tar -zxvf nginx-1.13.9.tar.gz
- 编译配置选项。
[root@192 src]# cd nginx-1.13.9/ [root@192 nginx-1.13.9]# mkdir /usr/local/nginx [root@192 nginx-1.13.9]# ./configure --prefix=/usr/local/nginx
- 编译和安装。
[root@192 nginx-1.13.9]# make && make install
- 编辑Nignx启动脚本,加入系统服务。
[root@192 nginx]# vim /etc/init.d/nginx #!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/local/nginx/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval killall -9 nginx } restart() { configtest || return $? stop sleep 1 start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac
- 保存后更改该脚本权限,设置开机启动。
[root@192 nginx]# chmod 755 /etc/init.d/nginx [root@192 nginx]# chkconfig --add nginx [root@192 nginx]# chkconfig nginx on
- 清空Nginx配置文件,写入新的配置参数。
[root@192 nginx]# vim /usr/local/nginx/conf/nginx.conf user www www; worker_processes 2; #设置值和CPU核心数一致 error_log /usr/local/nginx/logs/nginx_error.log crit; #日志位置和日志级别 pid /usr/local/nginx/nginx.pid; #Specifies the value for maximum file descriptors that can be opened by this process. worker_rlimit_nofile 65535; events { use epoll; worker_connections 65535; } 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'; #charset gb2312; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 8m; sendfile on; tcp_nopush on; keepalive_timeout 60; 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; 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; #limit_zone crawler $binary_remote_addr 10m; #下面是server虚拟主机的配置 server { listen 80;#监听端口 server_name localhost;#域名 index index.html index.htm index.php; root /usr/local/nginx/html;#站点目录 location ~ .*\.(php|php5)?$ { #fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$ { expires 30d; # access_log off; } location ~ .*\.(js|css)?$ { expires 15d; # access_log off; } access_log off; } }
- 测试配置文件是否正确,出现下面内容表示正确。
[root@192 nginx]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
- 启动Nginx。
[root@192 nginx]# /usr/local/nginx/sbin/nginx
- 测试是否正确解析PHP。
[root@192 nginx]# vim /usr/local/nginx/html/test.php <?php echo "php scripts\n"; ?> 保存退出 [root@centos7 html]# curl localhost/test.php php scripts
- 到这里LNMP环境搭建结束。